Hi,

The jump table used in dc/bcode.c is technically off by one, which leads
to the following harmless inconsistency:

        $ printf '\376' | dc
        dc: \xfe (0376) is unimplemented

        $ printf '\377' | dc
        dc: internal error: opcode 255

This could be fixed by making it hold 256 entries instead of 255;
however, since all commands are ASCII characters, it actually could
instead be reduced to 128 entries, with a small adjustment of the
relevant check in the eval() function.

Index: bcode.c
===================================================================
RCS file: /cvs/src/usr.bin/dc/bcode.c,v
retrieving revision 1.51
diff -u -p -r1.51 bcode.c
--- bcode.c     26 Feb 2017 11:29:55 -0000      1.51
+++ bcode.c     17 Nov 2017 02:38:12 -0000
@@ -137,7 +133,7 @@ struct jump_entry {
        opcode_function f;
 };
 
-static opcode_function jump_table[UCHAR_MAX];
+static opcode_function jump_table[128];
 
 static const struct jump_entry jump_table_data[] = {
        { ' ',  nop             },
@@ -238,7 +234,7 @@ init_bmachine(bool extended_registers)
        if (bmachine.reg == NULL)
                err(1, NULL);
 
-       for (i = 0; i < UCHAR_MAX; i++)
+       for (i = 0; i < 128; i++)
                jump_table[i] = unknown;
        for (i = 0; i < JUMP_TABLE_DATA_SIZE; i++)
                jump_table[jump_table_data[i].ch] = jump_table_data[i].f;
@@ -1746,10 +1716,10 @@ eval(void)
                (void)fprintf(stderr, "%zd =>\n", bmachine.readsp);
 #endif
 
-               if (0 <= ch && ch < UCHAR_MAX)
+               if (0 <= ch && ch < 128)
                        (*jump_table[ch])();
                else
-                       warnx("internal error: opcode %d", ch);
+                       unknown();
 
 #ifdef DEBUGGING
                stack_print(stderr, &bmachine.stack, "* ",

Regards,

kshe

Reply via email to