Skip to contentSkip to conversation

quota: fix generic/244 on 32-bit platforms

PATCH In review Theodore Ts'o 2 participants 2 messages Jul 4, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 quota: fix generic/244 on 32-bit platforms

The test program src/test-nextquota.c relies on atoi() to convert a string to an *unsigned* int. If the string represents an integer which is greater than INT_MAX, it is undefined how atoi(3) works, and it turns out that:

       uint id = atoi("2147483649");

results in id == 2147483649 on x86_64, and id == 2147483647 on a 32-bit x86 platform.

So use strtoul(3) instead, which is portable and technically correct.

---
 src/test-nextquota.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
src/test-nextquota.c +6 -1
index a2bbad9..6eec1f9 100644
@@ -74,6 +74,7 @@ int main(int argc, char *argv[])
 	int verbose = 0;
 	uint id = 0, idflag = 0;
 	char *device = NULL;
+	char *tmp;
 	struct nextdqblk dqb;
 	struct fs_disk_quota xqb;
 
@@ -92,7 +93,11 @@ int main(int argc, char *argv[])
 			typeflag++;
 			break;
 		case 'i':
-			id = atoi(optarg);
+			id = (int) strtoul(optarg, &tmp, 0);
+			if (*tmp) {
+				fprintf(stderr, "Bad id: %s\n", optarg);
+				exit(1);
+			}
 			idflag++;
 			break;
 		case 'd':
Signature · 1 line
2.5.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Theodore Ts'o

The test program src/test-nextquota.c relies on atoi() to convert a string to an *unsigned* int. If the string represents an integer which is greater than INT_MAX, it is undefined how atoi(3) works, and it turns out that:

       uint id = atoi("2147483649");
Quoted 7 lines

results in id == 2147483649 on x86_64, and id == 2147483647 on a 32-bit x86 platform.

So use strtoul(3) instead, which is portable and technically correct.

Yup, thanks. I bet that wasn't fun to track down, sorry. :(

Though wouldn't it be more correct to cast it to a (uint) ?

But with or without that change this should work with the test script, so

Thanks,
-Eric

Quoted 30 lines
---
 src/test-nextquota.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
src/test-nextquota.c +6 -1
index a2bbad9..6eec1f9 100644
@@ -74,6 +74,7 @@ int main(int argc, char *argv[])
 	int verbose = 0;
 	uint id = 0, idflag = 0;
 	char *device = NULL;
+	char *tmp;
 	struct nextdqblk dqb;
 	struct fs_disk_quota xqb;
 
@@ -92,7 +93,11 @@ int main(int argc, char *argv[])
 			typeflag++;
 			break;
 		case 'i':
-			id = atoi(optarg);
+			id = (int) strtoul(optarg, &tmp, 0);
+			if (*tmp) {
+				fprintf(stderr, "Bad id: %s\n", optarg);
+				exit(1);
+			}
 			idflag++;
 			break;
 		case 'd':