[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    fdisk - cleanup of longjmp
From:       Garrett Kajmowicz <gkajmowi () tbaytel ! net>
Date:       2006-02-25 4:11:30
Message-ID: 200602242311.30928.gkajmowi () tbaytel ! net
[Download RAW message or body]

The current code uses longjmp to escape from a terminating error-handler if we 
are scanning and printing partition tables. This is ugly, because it is 
trivial to have the utility functions return error codes if they break, 
instead of calling the terminating error-handler and escaping through the use 
of transporter pattern enhancers.   It saves a few bytes, but makes the code 
cleaner.

-     Garrett




["fdisk_no_longjmp.patch" (text/x-diff)]

--- fdisk.c.4	2006-02-24 22:29:47.000000000 -0500
+++ fdisk.c.5	2006-02-24 23:09:37.000000000 -0500
@@ -20,7 +20,6 @@
 #include <errno.h>
 #include <unistd.h>
 #include <fcntl.h>
-#include <setjmp.h>
 #include <assert.h>             /* assert */
 #include <getopt.h>
 #include <endian.h>
@@ -3351,7 +3350,6 @@ static int type_open = O_RDWR;
 
 
 static int ext_index;               /* the prime extended partition */
-static int listing;                    /* no aborts for fdisk -l */
 static int dos_compatible_flag = ~0;
 #ifdef CONFIG_FEATURE_FDISK_WRITABLE
 static int dos_changed;
@@ -3368,18 +3366,10 @@ static off_t extended_offset;           
 
 static unsigned long long total_number_of_sectors;
 
-
-static jmp_buf listingbuf;
-
 static void fdisk_fatal(enum failure why)
 {
 	const char *message;
 
-	if (listing) {
-		close(fd);
-		longjmp(listingbuf, 1);
-	}
-
 	switch (why) {
 	case unable_to_open:
 		message = "Unable to open %s\n";
@@ -3424,18 +3414,20 @@ write_sector(off_t secno, char *buf)
 #endif
 
 /* Allocate a buffer and read a partition table sector */
-static void
+static int
 read_pte(struct pte *pe, off_t offset)
 {
 	pe->offset = offset;
 	pe->sectorbuffer = (char *) xmalloc(sector_size);
 	seek_sector(offset);
-	if (read(fd, pe->sectorbuffer, sector_size) != sector_size)
-		fdisk_fatal(unable_to_read);
+	if (read(fd, pe->sectorbuffer, sector_size) != sector_size){
+		return 1;
+	}
 #ifdef CONFIG_FEATURE_FDISK_WRITABLE
 	pe->changed = 0;
 #endif
 	pe->part_table = pe->ext_pointer = NULL;
+	return 0;
 }
 
 static unsigned int
@@ -3801,7 +3793,7 @@ warn_cylinders(void)
 }
 #endif
 
-static void
+static int
 read_extended(int ext)
 {
 	int i;
@@ -3816,7 +3808,7 @@ read_extended(int ext)
 	if (!get_start_sect(p)) {
 		fprintf(stderr,
 			_("Bad offset in primary extended partition\n"));
-		return;
+		return 1;
 	}
 
 	while (IS_EXTENDED(p->sys_ind)) {
@@ -3834,10 +3826,12 @@ read_extended(int ext)
 			pre->changed = 1;
 #endif
 			clear_partition(pre->ext_pointer);
-			return;
+			return 0;
 		}
 
-		read_pte(pe, extended_offset + get_start_sect(p));
+		if(read_pte(pe, extended_offset + get_start_sect(p))){
+			return 1;
+		}
 
 		if (!extended_offset)
 			extended_offset = get_start_sect(p);
@@ -3895,6 +3889,7 @@ read_extended(int ext)
 		}
 	}
 #endif
+	return 0;
 }
 
 #ifdef CONFIG_FEATURE_FDISK_WRITABLE
@@ -4162,11 +4157,18 @@ get_boot(enum action what)
 		struct pte *pe = &ptes[i];
 
 		if (IS_EXTENDED(pe->part_table->sys_ind)) {
-			if (partitions != 4)
+			if (partitions != 4){
 				fprintf(stderr, _("Ignoring extra extended "
 					"partition %d\n"), i + 1);
-			else
-				read_extended(i);
+			}else{
+				if(read_extended(i)){
+					if(try_only == what){
+						return 1;
+					}else{
+						fdisk_fatal(unable_to_read);
+					}
+				}
+			}
 		}
 	}
 
@@ -5594,8 +5596,6 @@ try(const char *device, int user_specifi
 	int gb;
 
 	disk_device = device;
-	if (setjmp(listingbuf))
-		return;
 	if (!user_specified)
 		if (is_ide_cdrom_or_tape(device))
 			return;
@@ -5757,12 +5757,6 @@ int fdisk_main(int argc, char **argv)
 		type_open = O_RDONLY;
 		if (argc > optind) {
 			int k;
-#if __GNUC__
-			/* avoid gcc warning:
-			   variable `k' might be clobbered by `longjmp' */
-			(void)&k;
-#endif
-			listing = 1;
 			for (k = optind; k < argc; k++)
 				try(argv[k], 1);
 		} else {


_______________________________________________
busybox mailing list
busybox@busybox.net
http://busybox.net/cgi-bin/mailman/listinfo/busybox

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic