[PATCH] print format correctness for uid, gid, pid, mode in "util-linux/ipcs.c"

Joe Krahn krahn at niehs.nih.gov
Wed Mar 5 09:16:26 PST 2008


In "util-linux/ipcs.c", gid, uid, pid and mode values are all assumed to be default-size ints. I
have patched the formats to use long ints, and cast all arguments to longs. This seems to be the
common approach. The one exception is that I cast arguments with (mode&0777) as default-size ints.

In reality, most busybox target platforms have the same size for int and long-int, but there are
some 64-bit platforms where these values really are larger than default ints. The only way to be
exact is to use macros as in <inttypes.h>, which gets rather ugly. OTOH, that approach might avoid
unnecessary casting in some case, and might really be a better fit for the very-light-weight design
goal.

--- busybox-1.9.1.orig/util-linux/ipcs.c	2008-02-12 11:03:13.000000000 -0500
+++ busybox-1.9.1/util-linux/ipcs.c	2008-03-04 13:34:30.000000000 -0500
@@ -97,21 +97,21 @@
 	struct passwd *pw;
 	struct group *gr;

-	printf("%-10d %-10o", id, ipcp->mode & 0777);
+	printf("%-10d %-10o", id, (unsigned)ipcp->mode & 0777);

 	pw = getpwuid(ipcp->cuid);
 	if (pw)	printf(" %-10s", pw->pw_name);
-	else	printf(" %-10d", ipcp->cuid);
+	else	printf(" %-10ld", (long)ipcp->cuid);
 	gr = getgrgid(ipcp->cgid);
 	if (gr)	printf(" %-10s", gr->gr_name);
-	else	printf(" %-10d", ipcp->cgid);
+	else	printf(" %-10ld", (long)ipcp->cgid);

 	pw = getpwuid(ipcp->uid);
 	if (pw)	printf(" %-10s", pw->pw_name);
-	else	printf(" %-10d", ipcp->uid);
+	else	printf(" %-10ld", (long)ipcp->uid);
 	gr = getgrgid(ipcp->gid);
 	if (gr)	printf(" %-10s\n", gr->gr_name);
-	else	printf(" %-10d\n", ipcp->gid);
+	else	printf(" %-10ld\n", (long)ipcp->gid);
 }


@@ -201,7 +201,7 @@
 			if (pw)
 				printf("%-10d %-10.10s", shmid, pw->pw_name);
 			else
-				printf("%-10d %-10d", shmid, ipcp->uid);
+				printf("%-10d %-10ld", shmid, (long)ipcp->uid);
 			/* ctime uses static buffer: use separate calls */
 			printf(" %-20.16s", shmseg.shm_atime
 					  ? ctime(&shmseg.shm_atime) + 4 : "Not set");
@@ -214,8 +214,8 @@
 			if (pw)
 				printf("%-10d %-10.10s", shmid, pw->pw_name);
 			else
-				printf("%-10d %-10d", shmid, ipcp->uid);
-			printf(" %-10d %-10d\n", shmseg.shm_cpid, shmseg.shm_lpid);
+				printf("%-10d %-10ld", shmid, (long)ipcp->uid);
+			printf(" %-10ld %-10ld\n", (long)shmseg.shm_cpid, (long)shmseg.shm_lpid);
 			break;

 		default:
@@ -223,8 +223,8 @@
 			if (pw)
 				printf("%-10d %-10.10s", shmid, pw->pw_name);
 			else
-				printf("%-10d %-10d", shmid, ipcp->uid);
-			printf(" %-10o %-10lu %-10ld %-6s %-6s\n", ipcp->mode & 0777,
+				printf("%-10d %-10ld", shmid, (long)ipcp->uid);
+			printf(" %-10o %-10lu %-10ld %-6s %-6s\n", (unsigned)ipcp->mode & 0777,
 					  /*
 					   * earlier: int, Austin has size_t
 					   */
@@ -234,8 +234,8 @@
 					   * Austin has shmatt_t
 					   */
 					  (long) shmseg.shm_nattch,
-					  ipcp->mode & SHM_DEST ? "dest" : " ",
-					  ipcp->mode & SHM_LOCKED ? "locked" : " ");
+					  (unsigned long)ipcp->mode & SHM_DEST ? "dest" : " ",
+					  (unsigned long)ipcp->mode & SHM_LOCKED ? "locked" : " ");
 			break;
 		}
 	}
@@ -318,7 +318,7 @@
 			if (pw)
 				printf("%-8d %-10.10s", semid, pw->pw_name);
 			else
-				printf("%-8d %-10d", semid, ipcp->uid);
+				printf("%-8d %-10ld", semid, (long)ipcp->uid);
 			/* ctime uses static buffer: use separate calls */
 			printf("  %-26.24s", semary.sem_otime
 					  ? ctime(&semary.sem_otime) : "Not set");
@@ -333,8 +333,8 @@
 			if (pw)
 				printf("%-10d %-10.9s", semid, pw->pw_name);
 			else
-				printf("%-10d %-9d", semid, ipcp->uid);
-			printf(" %-10o %-10ld\n", ipcp->mode & 0777,
+				printf("%-10d %-9ld", semid, (long)ipcp->uid);
+			printf(" %-10o %-10ld\n", (unsigned)ipcp->mode & 0777,
 					  /*
 					   * glibc-2.1.3 and earlier has unsigned short;
 					   * glibc-2.1.91 has variation between
@@ -420,7 +420,7 @@
 			if (pw)
 				printf("%-8d %-10.10s", msqid, pw->pw_name);
 			else
-				printf("%-8d %-10d", msqid, ipcp->uid);
+				printf("%-8d %-10ld", msqid, (long)ipcp->uid);
 			printf(" %-20.16s", msgque.msg_stime
 					  ? ctime(&msgque.msg_stime) + 4 : "Not set");
 			printf(" %-20.16s", msgque.msg_rtime
@@ -432,7 +432,7 @@
 			if (pw)
 				printf("%-8d %-10.10s", msqid, pw->pw_name);
 			else
-				printf("%-8d %-10d", msqid, ipcp->uid);
+				printf("%-8d %-10ld", msqid, (long)ipcp->uid);
 			printf("  %5d     %5d\n", msgque.msg_lspid, msgque.msg_lrpid);
 			break;

@@ -441,8 +441,8 @@
 			if (pw)
 				printf("%-10d %-10.10s", msqid, pw->pw_name);
 			else
-				printf("%-10d %-10d", msqid, ipcp->uid);
-			printf(" %-10o %-12ld %-12ld\n", ipcp->mode & 0777,
+				printf("%-10d %-10ld", msqid, (long)ipcp->uid);
+			printf(" %-10o %-12ld %-12ld\n", (unsigned)ipcp->mode & 0777,
 					  /*
 					   * glibc-2.1.3 and earlier has unsigned short;
 					   * glibc-2.1.91 has variation between
@@ -467,13 +467,13 @@
 	}

 	printf("\nShared memory Segment shmid=%d\n"
-			  "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\n"
-			  "mode=%#o\taccess_perms=%#o\n"
-			  "bytes=%ld\tlpid=%d\tcpid=%d\tnattch=%ld\n",
+			  "uid=%ld\tgid=%ld\tcuid=%ld\tcgid=%ld\n"
+			  "mode=%#lo\taccess_perms=%#o\n"
+			  "bytes=%ld\tlpid=%ld\tcpid=%ld\tnattch=%ld\n",
 			  shmid,
-			  ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
-			  ipcp->mode, ipcp->mode & 0777,
-			  (long) shmds.shm_segsz, shmds.shm_lpid, shmds.shm_cpid,
+			  (long)ipcp->uid, (long)ipcp->gid, (long)ipcp->cuid, (long)ipcp->cgid,
+			  (unsigned long)ipcp->mode, (unsigned)ipcp->mode & 0777,
+			  (long) shmds.shm_segsz, (long)shmds.shm_lpid, (long)shmds.shm_cpid,
 			  (long) shmds.shm_nattch);
 	printf("att_time=%-26.24s\n",
 			  shmds.shm_atime ? ctime(&shmds.shm_atime) : "Not set");
@@ -494,9 +494,10 @@
 	}

 	printf("\nMessage Queue msqid=%d\n"
-			  "uid=%d\tgid=%d\tcuid=%d\tcgid=%d\tmode=%#o\n"
+			  "uid=%ld\tgid=%ld\tcuid=%ld\tcgid=%ld\tmode=%#lo\n"
 			  "cbytes=%ld\tqbytes=%ld\tqnum=%ld\tlspid=%d\tlrpid=%d\n",
-			  msqid, ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid, ipcp->mode,
+			  msqid, (long)ipcp->uid, (long)ipcp->gid, (long)ipcp->cuid,
+		      (long)ipcp->cgid, (unsigned long)ipcp->mode,
 			  /*
 			   * glibc-2.1.3 and earlier has unsigned short;
 			   * glibc-2.1.91 has variation between
@@ -528,13 +529,13 @@
 	}

 	printf("\nSemaphore Array semid=%d\n"
-			  "uid=%d\t gid=%d\t cuid=%d\t cgid=%d\n"
-			  "mode=%#o, access_perms=%#o\n"
+			  "uid=%ld\t gid=%ld\t cuid=%ld\t cgid=%ld\n"
+			  "mode=%#lo, access_perms=%#o\n"
 			  "nsems = %ld\n"
 			  "otime = %-26.24s\n",
 			  semid,
-			  ipcp->uid, ipcp->gid, ipcp->cuid, ipcp->cgid,
-			  ipcp->mode, ipcp->mode & 0777,
+			  (long)ipcp->uid, (long)ipcp->gid, (long)ipcp->cuid, (long)ipcp->cgid,
+			  (unsigned long)ipcp->mode, (unsigned)ipcp->mode & 0777,
 			  (long) semds.sem_nsems,
 			  semds.sem_otime ? ctime(&semds.sem_otime) : "Not set");
 	printf("ctime = %-26.24s\n"


More information about the busybox mailing list