Wed Dec 2 10:19:58 2015 UTC ()
- dereferencing NULL is generally bad, found by visual inspection
- automatically set nosync when opening a file to avoid spurious errors


(jnemeth)
diff -r1.54 -r1.55 src/sbin/gpt/gpt.c

cvs diff -r1.54 -r1.55 src/sbin/gpt/gpt.c (expand / switch to unified diff)

--- src/sbin/gpt/gpt.c 2015/12/02 04:17:25 1.54
+++ src/sbin/gpt/gpt.c 2015/12/02 10:19:58 1.55
@@ -25,27 +25,27 @@ @@ -25,27 +25,27 @@
25 * 25 *
26 * CRC32 code derived from work by Gary S. Brown. 26 * CRC32 code derived from work by Gary S. Brown.
27 */ 27 */
28 28
29#if HAVE_NBTOOL_CONFIG_H 29#if HAVE_NBTOOL_CONFIG_H
30#include "nbtool_config.h" 30#include "nbtool_config.h"
31#endif 31#endif
32 32
33#include <sys/cdefs.h> 33#include <sys/cdefs.h>
34#ifdef __FBSDID 34#ifdef __FBSDID
35__FBSDID("$FreeBSD: src/sbin/gpt/gpt.c,v 1.16 2006/07/07 02:44:23 marcel Exp $"); 35__FBSDID("$FreeBSD: src/sbin/gpt/gpt.c,v 1.16 2006/07/07 02:44:23 marcel Exp $");
36#endif 36#endif
37#ifdef __RCSID 37#ifdef __RCSID
38__RCSID("$NetBSD: gpt.c,v 1.54 2015/12/02 04:17:25 christos Exp $"); 38__RCSID("$NetBSD: gpt.c,v 1.55 2015/12/02 10:19:58 jnemeth Exp $");
39#endif 39#endif
40 40
41#include <sys/param.h> 41#include <sys/param.h>
42#include <sys/types.h> 42#include <sys/types.h>
43#include <sys/stat.h> 43#include <sys/stat.h>
44#include <sys/ioctl.h> 44#include <sys/ioctl.h>
45#include <sys/bootblock.h> 45#include <sys/bootblock.h>
46 46
47#include <err.h> 47#include <err.h>
48#include <errno.h> 48#include <errno.h>
49#include <fcntl.h> 49#include <fcntl.h>
50#include <paths.h> 50#include <paths.h>
51#include <stddef.h> 51#include <stddef.h>
@@ -457,27 +457,27 @@ gpt_gpt(gpt_t gpt, off_t lba, int found) @@ -457,27 +457,27 @@ gpt_gpt(gpt_t gpt, off_t lba, int found)
457 free(hdr); 457 free(hdr);
458 return (0); 458 return (0);
459} 459}
460 460
461gpt_t 461gpt_t
462gpt_open(const char *dev, int flags, int verbose, off_t mediasz, u_int secsz) 462gpt_open(const char *dev, int flags, int verbose, off_t mediasz, u_int secsz)
463{ 463{
464 int mode, found; 464 int mode, found;
465 off_t devsz; 465 off_t devsz;
466 gpt_t gpt; 466 gpt_t gpt;
467 467
468 468
469 if ((gpt = calloc(1, sizeof(*gpt))) == NULL) { 469 if ((gpt = calloc(1, sizeof(*gpt))) == NULL) {
470 if (!(gpt->flags & GPT_QUIET)) 470 if (!(flags & GPT_QUIET))
471 warn("Cannot allocate `%s'", dev); 471 warn("Cannot allocate `%s'", dev);
472 return NULL; 472 return NULL;
473 } 473 }
474 gpt->flags = flags; 474 gpt->flags = flags;
475 gpt->verbose = verbose; 475 gpt->verbose = verbose;
476 gpt->mediasz = mediasz; 476 gpt->mediasz = mediasz;
477 gpt->secsz = secsz; 477 gpt->secsz = secsz;
478 478
479 mode = (gpt->flags & GPT_READONLY) ? O_RDONLY : O_RDWR|O_EXCL; 479 mode = (gpt->flags & GPT_READONLY) ? O_RDONLY : O_RDWR|O_EXCL;
480  480
481 gpt->fd = opendisk(dev, mode, gpt->device_name, 481 gpt->fd = opendisk(dev, mode, gpt->device_name,
482 sizeof(gpt->device_name), 0); 482 sizeof(gpt->device_name), 0);
483 if (gpt->fd == -1) { 483 if (gpt->fd == -1) {
@@ -515,26 +515,27 @@ gpt_open(const char *dev, int flags, int @@ -515,26 +515,27 @@ gpt_open(const char *dev, int flags, int
515 gpt_warnx(gpt, "Media size can't be 0"); 515 gpt_warnx(gpt, "Media size can't be 0");
516 goto close; 516 goto close;
517 } 517 }
518 } 518 }
519 } else { 519 } else {
520 if (gpt->secsz == 0) 520 if (gpt->secsz == 0)
521 gpt->secsz = 512; /* Fixed size for files. */ 521 gpt->secsz = 512; /* Fixed size for files. */
522 if (gpt->mediasz == 0) { 522 if (gpt->mediasz == 0) {
523 if (gpt->sb.st_size % gpt->secsz) { 523 if (gpt->sb.st_size % gpt->secsz) {
524 errno = EINVAL; 524 errno = EINVAL;
525 goto close; 525 goto close;
526 } 526 }
527 gpt->mediasz = gpt->sb.st_size; 527 gpt->mediasz = gpt->sb.st_size;
 528 gpt->flags |= GPT_NOSYNC;
528 } 529 }
529 } 530 }
530 531
531 /* 532 /*
532 * We require an absolute minimum of 6 sectors. One for the MBR, 533 * We require an absolute minimum of 6 sectors. One for the MBR,
533 * 2 for the GPT header, 2 for the GPT table and one to hold some 534 * 2 for the GPT header, 2 for the GPT table and one to hold some
534 * user data. Let's catch this extreme border case here so that 535 * user data. Let's catch this extreme border case here so that
535 * we don't have to worry about it later. 536 * we don't have to worry about it later.
536 */ 537 */
537 devsz = gpt->mediasz / gpt->secsz; 538 devsz = gpt->mediasz / gpt->secsz;
538 if (devsz < 6) { 539 if (devsz < 6) {
539 gpt_warnx(gpt, "Need 6 sectorso, we have %ju", 540 gpt_warnx(gpt, "Need 6 sectorso, we have %ju",
540 (uintmax_t)devsz); 541 (uintmax_t)devsz);