[PATCH] D126121: Remove undefined behavior in count.c test program [NFC]

Alf via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat May 21 09:55:27 PDT 2022


gAlfonso-bit created this revision.
gAlfonso-bit added a reviewer: MaskRay.
Herald added a subscriber: StephenFan.
Herald added a project: All.
gAlfonso-bit requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This was done by making the types consistent with each other, and returning an unsigned long instead of a long int.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D126121

Files:
  llvm/utils/count/count.c


Index: llvm/utils/count/count.c
===================================================================
--- llvm/utils/count/count.c
+++ llvm/utils/count/count.c
@@ -10,7 +10,7 @@
 #include <stdio.h>
 
 int main(int argc, char **argv) {
-  unsigned Count, NumLines, NumRead;
+  size_t Count, NumLines, NumRead;
   char Buffer[4096], *End;
 
   if (argc != 2) {
@@ -18,7 +18,7 @@
     return 2;
   }
 
-  Count = strtol(argv[1], &End, 10);
+  Count = strtoul(argv[1], &End, 10);
   if (*End != '\0' && End != argv[1]) {
     fprintf(stderr, "%s: invalid count argument '%s'\n", argv[0], argv[1]);
     return 2;
@@ -26,7 +26,7 @@
 
   NumLines = 0;
   do {
-    unsigned i;
+    size_t i;
 
     NumRead = fread(Buffer, 1, sizeof(Buffer), stdin);
 
@@ -34,14 +34,14 @@
       if (Buffer[i] == '\n')
         ++NumLines;
   } while (NumRead == sizeof(Buffer));
-    
+
   if (!feof(stdin)) {
     fprintf(stderr, "%s: error reading stdin\n", argv[0]);
     return 3;
   }
 
   if (Count != NumLines) {
-    fprintf(stderr, "Expected %d lines, got %d.\n", Count, NumLines);
+    fprintf(stderr, "Expected %lu lines, got %lu.\n", Count, NumLines);
     return 1;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126121.431147.patch
Type: text/x-patch
Size: 1162 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220521/544a4554/attachment.bin>


More information about the llvm-commits mailing list