The sequential CALLS pass formats each argument into a 512-byte local before * copying it into the props buffer (calls_append_args, pass_calls.c). snprintf * reports the untruncated length, so an argument the local cannot hold must end * the array exactly like one the props buffer cannot take; copying that length * out of the local reads past it and plants the local's terminator inside the *
| 23424 | * bytes, and the props buffer is large enough to take it, so only the local's |
| 23425 | * own bound can stop the copy. 0 when the props came out right, 1 otherwise. */ |
| 23426 | static int calls_args_overlong_probe(void) { |
| 23427 | enum { CAP = 4 * CBM_SZ_1K, LONG_LEN = 250 }; |
| 23428 | static char expr[LONG_LEN + 1]; |
| 23429 | static char value[LONG_LEN + 1]; |
| 23430 | static char props[CAP]; |
| 23431 | memset(expr, 'a', LONG_LEN); |
| 23432 | expr[LONG_LEN] = '\0'; |
| 23433 | memset(value, 'b', LONG_LEN); |
| 23434 | value[LONG_LEN] = '\0'; |
| 23435 | CBMCallArg args[2] = {{"x", NULL, NULL, 0, NULL}, {expr, value, NULL, 1, NULL}}; |
| 23436 | CBMCall call = {0}; |
| 23437 | call.callee_name = "sink"; |
| 23438 | call.args = args; |
| 23439 | call.arg_count = 2; |
| 23440 | |
| 23441 | const char *base = "{\"line\":7}"; |
| 23442 | memset(props, 0x7f, CAP); /* a sentinel the formatter never writes */ |
| 23443 | memcpy(props, base, strlen(base) + 1); |
| 23444 | cbm_pipeline_calls_append_args_for_tests(props, CAP, &call); |
| 23445 | |
| 23446 | /* The short argument is kept; the overlong one ends the array cleanly. */ |
| 23447 | const char *expected = "{\"line\":7,\"args\":[{\"i\":0,\"e\":\"x\"}]}"; |
| 23448 | size_t expected_len = strlen(expected); |
| 23449 | if (strncmp(props, expected, expected_len + 1) != 0) { |
| 23450 | printf(" props: %.80s\n expected: %s\n", props, expected); |
| 23451 | return 1; |
| 23452 | } |
| 23453 | for (size_t i = expected_len + 1; i < CAP; i++) { |
| 23454 | if ((unsigned char)props[i] != 0x7f) { |
| 23455 | printf(" byte %zu past the terminator was written\n", i); |
| 23456 | return 1; |
| 23457 | } |
| 23458 | } |
| 23459 | return 0; |
| 23460 | } |
| 23461 | |
| 23462 | TEST(pipeline_calls_args_overlong_argument_stops_cleanly) { |
| 23463 | #if defined(_WIN32) |
no test coverage detected