Blame SOURCES/0003-Fixes-for-problems-discovered-by-coverity-scan.-8120.patch

1e69ac
From a3655b7bf64b7e016602d0b2bec450d27575816e Mon Sep 17 00:00:00 2001
1e69ac
From: Peter Jones <pjones@redhat.com>
1e69ac
Date: Mon, 15 Oct 2012 13:12:53 -0400
1e69ac
Subject: [PATCH 3/4] Fixes for problems discovered by coverity scan.
1e69ac
1e69ac
Related: rhbz#1085434
1e69ac
1e69ac
Signed-off-by: Peter Jones <pjones@fedoraproject.org>
1e69ac
---
1e69ac
 extlinux/main.c         |  9 ++++++++-
1e69ac
 libfat/fat.h            |  9 +++++++--
1e69ac
 libinstaller/advio.c    | 17 +++++++++--------
1e69ac
 libinstaller/syslxcom.c |  8 ++++++++
1e69ac
 libinstaller/syslxint.h | 10 +++++++---
1e69ac
 linux/syslinux.c        |  3 ---
1e69ac
 mtools/syslinux.c       |  1 +
1e69ac
 utils/Makefile          |  2 +-
1e69ac
 utils/isohybrid.c       | 20 ++++++++++++++------
1e69ac
 9 files changed, 55 insertions(+), 24 deletions(-)
1e69ac
1e69ac
diff --git a/extlinux/main.c b/extlinux/main.c
1e69ac
index e574051..a461533 100755
1e69ac
--- a/extlinux/main.c
1e69ac
+++ b/extlinux/main.c
1e69ac
@@ -292,7 +292,12 @@ int patch_file_and_bootblock(int fd, const char *dir, int devfd)
1e69ac
     nsect = (boot_image_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT;
1e69ac
     nsect += 2;			/* Two sectors for the ADV */
1e69ac
     sectp = alloca(sizeof(sector_t) * nsect);
1e69ac
-    if (fs_type == EXT2 || fs_type == VFAT) {
1e69ac
+    if (sectp == NULL) {
1e69ac
+	perror("alloca");
1e69ac
+	exit(1);
1e69ac
+    }
1e69ac
+    memset(sectp, '\0', sizeof(sector_t) * nsect);
1e69ac
+    if (fd >= 0 && (fs_type == EXT2 || fs_type == VFAT)) {
1e69ac
 	if (sectmap(fd, sectp, nsect)) {
1e69ac
 		perror("bmap");
1e69ac
 		exit(1);
1e69ac
@@ -423,6 +428,8 @@ int ext2_fat_install_file(const char *path, int devfd, struct stat *rst)
1e69ac
 
1e69ac
     /* Map the file, and patch the initial sector accordingly */
1e69ac
     modbytes = patch_file_and_bootblock(fd, path, devfd);
1e69ac
+    if (modbytes < 0)
1e69ac
+	goto bail;
1e69ac
 
1e69ac
     /* Write the patch area again - this relies on the file being
1e69ac
        overwritten in place! */
1e69ac
diff --git a/libfat/fat.h b/libfat/fat.h
1e69ac
index b4e32f7..acafdb6 100644
1e69ac
--- a/libfat/fat.h
1e69ac
+++ b/libfat/fat.h
1e69ac
@@ -23,8 +23,13 @@
1e69ac
 
1e69ac
 /* The poor excuse FAT has for a superblock -- in the boot sector */
1e69ac
 struct fat_bootsect {
1e69ac
-    le8_t bsJump[3];		/* Jump to code */
1e69ac
-    char bsOemName[8];		/* Formatting program */
1e69ac
+    union {
1e69ac
+	struct {
1e69ac
+	    uint8_t bsJump[3];	/* Jump to code */
1e69ac
+	    char bsOemName[8];	/* Formatting program */
1e69ac
+	};
1e69ac
+	uint8_t bsHead[11];
1e69ac
+    };
1e69ac
     le16_t bsBytesPerSec;	/* Bytes/sector */
1e69ac
     le8_t bsSecPerClust;	/* Sectors/cluster */
1e69ac
     le16_t bsResSectors;	/* Reserved sectors */
1e69ac
diff --git a/libinstaller/advio.c b/libinstaller/advio.c
1e69ac
index 56f607d..01894f2 100644
1e69ac
--- a/libinstaller/advio.c
1e69ac
+++ b/libinstaller/advio.c
1e69ac
@@ -135,15 +135,16 @@ int write_adv(const char *path, const char *cfg)
1e69ac
 		       xst.st_dev != st.st_dev || xst.st_size != st.st_size) {
1e69ac
 		fprintf(stderr, "%s: race condition on write\n", file);
1e69ac
 		err = -2;
1e69ac
+	    } else {
1e69ac
+		/* Write our own version ... */
1e69ac
+		if (xpwrite(fd, syslinux_adv, 2 * ADV_SIZE,
1e69ac
+			    st.st_size - 2 * ADV_SIZE) != 2 * ADV_SIZE) {
1e69ac
+		    err = -1;
1e69ac
+		}
1e69ac
+
1e69ac
+		sync();
1e69ac
+		set_attributes(fd);
1e69ac
 	    }
1e69ac
-	    /* Write our own version ... */
1e69ac
-	    if (xpwrite(fd, syslinux_adv, 2 * ADV_SIZE,
1e69ac
-			st.st_size - 2 * ADV_SIZE) != 2 * ADV_SIZE) {
1e69ac
-		err = -1;
1e69ac
-	    }
1e69ac
-
1e69ac
-	    sync();
1e69ac
-	    set_attributes(fd);
1e69ac
 	}
1e69ac
     }
1e69ac
 
1e69ac
diff --git a/libinstaller/syslxcom.c b/libinstaller/syslxcom.c
1e69ac
index a6a8339..dae81bc 100644
1e69ac
--- a/libinstaller/syslxcom.c
1e69ac
+++ b/libinstaller/syslxcom.c
1e69ac
@@ -87,6 +87,9 @@ ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset)
1e69ac
     ssize_t rv;
1e69ac
     ssize_t done = 0;
1e69ac
 
1e69ac
+    if (fd < 0)
1e69ac
+	die(strerror(EBADF));
1e69ac
+
1e69ac
     while (count) {
1e69ac
 	rv = pwrite(fd, bufp, count, offset);
1e69ac
 	if (rv == 0) {
1e69ac
@@ -279,6 +282,11 @@ static int sectmap_fib(int fd, sector_t *sectors, int nsectors)
1e69ac
  */
1e69ac
 int sectmap(int fd, sector_t *sectors, int nsectors)
1e69ac
 {
1e69ac
+    if (fd < 0) {
1e69ac
+	errno = EBADF;
1e69ac
+	return -1;
1e69ac
+    }
1e69ac
+
1e69ac
     if (!sectmap_fie(fd, sectors, nsectors))
1e69ac
 	return 0;
1e69ac
 
1e69ac
diff --git a/libinstaller/syslxint.h b/libinstaller/syslxint.h
1e69ac
index 7c9da51..8d39f74 100644
1e69ac
--- a/libinstaller/syslxint.h
1e69ac
+++ b/libinstaller/syslxint.h
1e69ac
@@ -193,8 +193,13 @@ struct syslinux_extent {
1e69ac
 
1e69ac
 /* FAT bootsector format, also used by other disk-based derivatives */
1e69ac
 struct boot_sector {
1e69ac
-    uint8_t bsJump[3];
1e69ac
-    char bsOemName[8];
1e69ac
+    union {
1e69ac
+	struct {
1e69ac
+	    uint8_t bsJump[3];
1e69ac
+	    char bsOemName[8];
1e69ac
+	};
1e69ac
+	uint8_t bsHead[11];
1e69ac
+    };
1e69ac
     uint16_t bsBytesPerSec;
1e69ac
     uint8_t bsSecPerClust;
1e69ac
     uint16_t bsResSectors;
1e69ac
@@ -241,7 +246,6 @@ struct boot_sector {
1e69ac
     uint16_t bsSignature;
1e69ac
 } __attribute__ ((packed));
1e69ac
 
1e69ac
-#define bsHead      bsJump
1e69ac
 #define bsHeadLen   offsetof(struct boot_sector, bsBytesPerSec)
1e69ac
 #define bsCode	    bs32.Code	/* The common safe choice */
1e69ac
 #define bsCodeLen   (offsetof(struct boot_sector, bsSignature) - \
1e69ac
diff --git a/linux/syslinux.c b/linux/syslinux.c
1e69ac
index c7a9ecc..6e23a7a 100755
1e69ac
--- a/linux/syslinux.c
1e69ac
+++ b/linux/syslinux.c
1e69ac
@@ -335,9 +335,6 @@ int main(int argc, char *argv[])
1e69ac
 	    snprintf(mntname, sizeof mntname, "syslinux.mnt.%lu.%d",
1e69ac
 		     (unsigned long)mypid, i);
1e69ac
 
1e69ac
-	    if (lstat(mntname, &dst) != -1 || errno != ENOENT)
1e69ac
-		continue;
1e69ac
-
1e69ac
 	    rv = mkdir(mntname, 0000);
1e69ac
 
1e69ac
 	    if (rv == -1) {
1e69ac
diff --git a/mtools/syslinux.c b/mtools/syslinux.c
1e69ac
index ac189c6..4bec0e3 100755
1e69ac
--- a/mtools/syslinux.c
1e69ac
+++ b/mtools/syslinux.c
1e69ac
@@ -208,6 +208,7 @@ int main(int argc, char *argv[])
1e69ac
 	!mtools_conf)
1e69ac
 	die_err(tmpdir);
1e69ac
 
1e69ac
+    umask(077);
1e69ac
     mtc_fd = mkstemp(mtools_conf);
1e69ac
     if (mtc_fd < 0 || !(mtc = fdopen(mtc_fd, "w")))
1e69ac
 	die_err(mtools_conf);
1e69ac
diff --git a/utils/Makefile b/utils/Makefile
1e69ac
index 44cb54f..4fabe04 100644
1e69ac
--- a/utils/Makefile
1e69ac
+++ b/utils/Makefile
1e69ac
@@ -51,7 +51,7 @@ isohdpfx.c: $(ISOHDPFX) isohdpfxarray.pl
1e69ac
 	$(PERL) isohdpfxarray.pl $(ISOHDPFX) > $@
1e69ac
 
1e69ac
 isohybrid: isohybrid.o isohdpfx.o
1e69ac
-	$(CC) $(LDFLAGS) -luuid -o $@ $^
1e69ac
+	$(CC) $(LDFLAGS) -fshort-wchar -luuid -o $@ $^
1e69ac
 
1e69ac
 gethostip: gethostip.o
1e69ac
 	$(CC) $(LDFLAGS) -o $@ $^
1e69ac
diff --git a/utils/isohybrid.c b/utils/isohybrid.c
1e69ac
index ac04bfd..865c114 100644
1e69ac
--- a/utils/isohybrid.c
1e69ac
+++ b/utils/isohybrid.c
1e69ac
@@ -357,6 +357,8 @@ check_option(int argc, char *argv[])
1e69ac
         case ':':
1e69ac
             errx(1, "option `-%c' takes an argument", optopt);
1e69ac
 
1e69ac
+            printh();
1e69ac
+            exit(0);
1e69ac
         default:
1e69ac
         case '?':
1e69ac
             if (optopt)
1e69ac
@@ -618,7 +620,7 @@ initialise_mbr(uint8_t *mbr)
1e69ac
     bsect = (offset % sector) + 1;
1e69ac
     bcyle = offset / (head * sector);
1e69ac
 
1e69ac
-    bsect += (bcyle & 0x300) >> 2;
1e69ac
+    bsect += bcyle >> 2;
1e69ac
     bcyle  &= 0xFF;
1e69ac
 
1e69ac
     ehead = head - 1;
1e69ac
@@ -792,7 +794,7 @@ initialise_gpt(uint8_t *gpt, uint32_t current, uint32_t alternate, int primary)
1e69ac
     memcpy(part->partTypeGUID, basic_partition, sizeof(uuid_t));
1e69ac
     part->firstLBA = lendian_64(0);
1e69ac
     part->lastLBA = lendian_64(psize);
1e69ac
-    memcpy(part->name, "ISOHybrid ISO", 28);
1e69ac
+    memcpy(part->name, L"ISOHybrid ISO", 28);
1e69ac
 
1e69ac
     gpt += sizeof(struct gpt_part_header);
1e69ac
     part++;
1e69ac
@@ -801,7 +803,7 @@ initialise_gpt(uint8_t *gpt, uint32_t current, uint32_t alternate, int primary)
1e69ac
     memcpy(part->partTypeGUID, basic_partition, sizeof(uuid_t));
1e69ac
     part->firstLBA = lendian_64(efi_lba * 4);
1e69ac
     part->lastLBA = lendian_64(part->firstLBA + efi_count - 1);
1e69ac
-    memcpy(part->name, "ISOHybrid", 20);
1e69ac
+    memcpy(part->name, L"ISOHybrid", 20);
1e69ac
 
1e69ac
     gpt += sizeof(struct gpt_part_header);
1e69ac
 
1e69ac
@@ -814,7 +816,7 @@ initialise_gpt(uint8_t *gpt, uint32_t current, uint32_t alternate, int primary)
1e69ac
 	memcpy(part->partTypeGUID, hfs_partition, sizeof(uuid_t));
1e69ac
 	part->firstLBA = lendian_64(mac_lba * 4);
1e69ac
 	part->lastLBA = lendian_64(part->firstLBA + mac_count - 1);
1e69ac
-	memcpy(part->name, "ISOHybrid", 20);
1e69ac
+	memcpy(part->name, L"ISOHybrid", 20);
1e69ac
 
1e69ac
 	part--;
1e69ac
     }
1e69ac
@@ -891,7 +893,11 @@ main(int argc, char *argv[])
1e69ac
     size_t orig_gpt_size, free_space, gpt_size;
1e69ac
     struct iso_primary_descriptor descriptor;
1e69ac
 
1e69ac
-    prog = strcpy(alloca(strlen(argv[0]) + 1), argv[0]);
1e69ac
+    prog = alloca(strlen(argv[0]) + 1);
1e69ac
+    if (!prog)
1e69ac
+	err(1, "");
1e69ac
+    strcpy(prog, argv[0]);
1e69ac
+
1e69ac
     i = check_option(argc, argv);
1e69ac
     argc -= i;
1e69ac
     argv += i;
1e69ac
@@ -1097,7 +1103,9 @@ main(int argc, char *argv[])
1e69ac
 
1e69ac
 	initialise_apm(buf, APM_OFFSET);
1e69ac
 
1e69ac
-	fseek(fp, APM_OFFSET, SEEK_SET);
1e69ac
+	if (fseek(fp, APM_OFFSET, SEEK_SET))
1e69ac
+	    err(1, "%s: seek error - 7", argv[0]);
1e69ac
+
1e69ac
 	fwrite(buf, sizeof(char), apm_size, fp);
1e69ac
     }
1e69ac
 
1e69ac
-- 
1e69ac
1.9.3
1e69ac