Skip to content

Commit

Permalink
Generate assertions in statuslookupgen to validate input
Browse files Browse the repository at this point in the history
Although inputs to this function have to come from an enum that is
generated by the same X-macro that is used to build the perfect hash
table, anything could theoretically be passed to it if casted from an
integer.  So assert that the returned string actually matches the
requested HTTP status code.

(The lookup function wouldn't crash otherwise, but could return either
a 999 Invalid code, or some code that is valid but has nothing to do
with the requested code.  Since this is just an assertion, this is
mostly here to ensure that issues found during debugging are actually
caught.)
  • Loading branch information
lpereira committed Apr 14, 2024
1 parent b9bd2b1 commit 3ec5f7e
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/bin/tools/statuslookupgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ int main(void)

printf("\n");
printf(" const uint32_t k = (uint32_t)status - %d;\n", best_subtract);
printf(" return table[((k << %d) | (k >> %d)) %% %d];\n", 32 - best_rot, best_rot, best_mod);
printf(" const char *ret = table[((k << %d) | (k >> %d)) %% %d];\n", 32 - best_rot, best_rot, best_mod);
printf(" assert((uint32_t)(ret[2] - '0') == ((uint32_t)status %% 10));\n");
printf(" assert((uint32_t)(ret[1] - '0') == ((uint32_t)(status / 10) %% 10));\n");
printf(" assert((uint32_t)(ret[0] - '0') == ((uint32_t)(status / 100) %% 10));\n");
printf(" return ret;\n");

printf("}\n");

Expand Down

0 comments on commit 3ec5f7e

Please sign in to comment.