correct octal conversion
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Tue, 9 Jan 2024 10:46:47 +0000 (11:46 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Tue, 9 Jan 2024 10:46:47 +0000 (11:46 +0100)
hexdump.c

index 77a413547e7936d7803a9d536527bb07ea3d9016..c65675b1acd1695ddf7eb016070e0104be1b3a4c 100644 (file)
--- a/hexdump.c
+++ b/hexdump.c
@@ -270,7 +270,7 @@ long int octal (char *s, int n) {
     int i;
     unsigned long int l = 0;
     for (i = 0; i < n; i++) {
-        if ((s[i] >= '0') && (s[i] <= '9')) {
+        if ((s[i] >= '0') && (s[i] < '8')) {
             l = l * 8 + s[i] - '0';
         } else {
             return -1;
@@ -331,23 +331,27 @@ int specialchar (char *s, char *b) {
         case '2':
         case '3':
             l = octal (s + i + 1, 3);
-            if (l != -1) {
-                i += 4;
+            if (l == -1) {
+                VERBOSE (WARNING, fprintf (stderr, "incorrect special char (\\%c%c%c)\n", s[i + 1], s[i + 2], s[i + 3]));
             }
+            i += 4;
             break;
         case 'x':
             l = hexa (s + i + 2, 2);
-            if (l != -1) {
-                i += 4;
+            if (l == -1) {
+                VERBOSE (WARNING, fprintf (stderr, "incorrect special char (\\x%c%c)\n", s[i + 2], s[i + 3]));
             }
+            i += 4;
             break;
         default:
+            VERBOSE (WARNING, fprintf (stderr, "incorrect special char (\\%c)\n", s[i + 1]));
+            i += 2;
             break;
         }
         if (l != -1) {
             VERBOSE (DEBUG, printf("l: 0x%02x '%c'\n", l, l));
+            b[j++] = l;
         }
-        b[j++] = (l != -1) ? l : s[i++];
     }
 
     return j;
@@ -489,7 +493,7 @@ int main (int argc, char *argv[])
                 } else {
                     char *tmp = (char *) malloc (strlen (arg) + 1);
                     strcat (strcat (commands, " "), strcpy (tmp, arg));
-                   free (tmp);
+                    free (tmp);
                 }
             }
             break;
@@ -571,7 +575,7 @@ int main (int argc, char *argv[])
                         VERBOSE (ERROR, fprintf (stderr, "can't find pattern '%s'\n", seq.sequence));
                     }
                 } else {
-                    VERBOSE (ERROR, fprintf (stderr, "erroneous pattern '%s'\n", seq.sequence));
+                    VERBOSE (ERROR, fprintf (stderr, "erroneous pattern \"%s'\n", seq.sequence));
                 }
                 break;
 
@@ -605,11 +609,16 @@ int main (int argc, char *argv[])
             case '9': /* read address */
                 commands--;
                 addr = strtol (commands, &commands, 10);
-                rc = gotoaddr (addr);
-                if (rc == 1) {
-                    VERBOSE (ERROR, fprintf (stderr, "can't find address (0x%0*lx)\n", getnbdigits (addr), addr));
+                if ((*commands != 0) && (*commands != ' ')) {
+                    VERBOSE (ERROR, fprintf (stderr, "erroneous address ()\n"));
+                    rc = 1;
+                } else {
+                    rc = gotoaddr (addr);
+                    if (rc == 1) {
+                        VERBOSE (ERROR, fprintf (stderr, "can't find address (0x%0*lx)\n", getnbdigits (addr), addr));
+                    }
+                    offset = 0;
                 }
-                offset = 0;
                 break;
 
             case 'a': /* append mode */
@@ -732,6 +741,7 @@ int main (int argc, char *argv[])
 // test: hexdump.exe -i hexdump.c -o test.c -e 'p 200' | tail -1 | grep -q '0x00c0:'
 // test: cmp hexdump.c test.c; x=$?; rm test.c; test x$x = x0
 // test: hexdump.exe -i hexdump.c -n 8 -e ' /cflags/ p 17 /debug/ p 8' | grep -q '0x0019: 2a 2f 0a 2f 2a 20 6c 69  \*\/\./\* li'
+// test: hexdump.exe -i hexdump.c -e ' /\099\411\xgg\y/' 2>&1 | grep -c 'incorrect special char' | xargs test 4 -eq
 // test: hexdump.exe -i hexdump.c -o test.c -e ' /cfl\x61gs/ p 16 /d\145bug/ p 8' | grep -q '0x0027: 64 65 62 75 67 2e 6f 20  debug.o'
 // test: cmp hexdump.c test.c; x=$?; rm test.c; test x$x = x0
 // test: hexdump.exe -i hexdump.c -e ' /\n/ p 8' | grep -q '0x000d: 0a 2f 2a 20 63 66 6c 61  \./\* cfla'
@@ -748,7 +758,9 @@ int main (int argc, char *argv[])
 // test: hexdump.exe -i hexdump.c -e '0xFFFFFF' 2>&1 | grep -q "can't find address"
 // test: hexdump.exe -i hexdump.c -e '99999999' 2>&1 | grep -q "can't find address"
 // test: hexdump.exe -i hexdump.c -e '+9999999' 2>&1 | grep -q "can't find address"
+// test: hexdump.exe -i hexdump.c -e '09' 2>&1 | grep -q 'erroneous address'
 // test: hexdump.exe -i hexdump.c -e '0xg' 2>&1 | grep -q 'erroneous address'
+// test: hexdump.exe -i hexdump.c -e '1a' 2>&1 | grep -q 'erroneous address'
 // test: hexdump.exe -i hexdump.c -o test.c -e ' /cflags/ a 414e5a /link/ i 2F333B'
 // test: grep -q '[A]NZcflags' test.c && grep -q '[l]ink/3;er' test.c; x=$?; rm test.c; test x$x = x0
 // test: hexdump.exe -i hexdump.c -e ' /cflags/ a 414e5' 2>&1 | grep -q 'erroneous sequence'