Skip to content

Commit 3ec5f7e

Browse files
committed
Generate assertions in statuslookupgen to validate input
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.)
1 parent b9bd2b1 commit 3ec5f7e

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/bin/tools/statuslookupgen.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ int main(void)
9292

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

97101
printf("}\n");
98102

0 commit comments

Comments
 (0)