blockdev.c: getsize(): signed args, error checking

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello,

in getsize() the BLKGETSIZE and BLKGETSIZE64 ioctls are called
with signed args, see the patch below.


Another thing:

Since the EFBIG change in getsize() sz might contain garbage, so there
should be additional error checks, something like (yes, this is ugly):


static int
getsize(int fd, unsigned long long *sectors) {
	int err1, err2;
	unsigned long sz;
	unsigned long long b;

	err1 = ioctl (fd, BLKGETSIZE, &sz);
	if (err1) {
		if (errno != EFBIG)
			return err1;
	}
	err2 = ioctl(fd, BLKGETSIZE64, &b);
	if (!err1 && (err2 || b == 0 || b == sz))
		*sectors = sz;
	else if (!err2)
		*sectors = (b >> 9);
	else
		return err2;

	return 0;
}


What is the purpose of (b == 0 || b == sz)? I searched a little and most
getsize() functions seem to try BLKGETSIZE64 first. If that fails, they
try other methods.


Stefan Krah




========================================================================

diff --git a/disk-utils/blockdev.c b/disk-utils/blockdev.c
index d02ebbc..d0ddb1b 100644
--- a/disk-utils/blockdev.c
+++ b/disk-utils/blockdev.c
@@ -145,10 +145,10 @@ find_cmd(char *s) {
 }
 
 static int
-getsize(int fd, long long *sectors) {
+getsize(int fd, unsigned long long *sectors) {
 	int err;
-	long sz;
-	long long b;
+	unsigned long sz;
+	unsigned long long b;
 
 	err = ioctl (fd, BLKGETSIZE, &sz);
 	if (err) {
@@ -262,9 +262,9 @@ do_commands(int fd, char **argv, int d) {
 		}
 
 		if (!strcmp(argv[i], "--getsz")) {
-			res = getsize(fd, &llarg);
+			res = getsize(fd, &llu);
 			if (res == 0)
-				printf("%lld\n", llarg);
+				printf("%llu\n", llu);
 			else
 				exit(1);
 			continue;
@@ -401,7 +401,7 @@ report_device(char *device, int quiet) {
 	int fd;
 	int ro, ssz, bsz;
 	long ra, ss;
-	long long bytes;
+	unsigned long long sectors;
 	struct hd_geometry g;
 
 	fd = open(device, O_RDONLY | O_NONBLOCK);
@@ -419,9 +419,9 @@ report_device(char *device, int quiet) {
 	    ioctl (fd, BLKSSZGET, &ssz) == 0 &&
 	    ioctl (fd, BLKBSZGET, &bsz) == 0 &&
 	    ioctl (fd, HDIO_GETGEO, &g) == 0 &&
-	    getsize (fd, &bytes) == 0) {
-		printf("%s %5ld %5d %5d %10ld %10lld  %s\n",
-		       ro ? "ro" : "rw", ra, ssz, bsz, g.start, bytes, device);
+	    getsize (fd, &sectors) == 0) {
+		printf("%s %5ld %5d %5d %10ld %10llu  %s\n",
+		       ro ? "ro" : "rw", ra, ssz, bsz, g.start, sectors, device);
 	} else {
 		if (!quiet)
 			fprintf(stderr, _("%s: ioctl error on %s\n"),


-
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux