Graph names and paths originate in the repository/index and are data, never * hook instructions. Keep valid UTF-8 sequences, collapse ASCII controls to a * space, and bound each field without splitting a multibyte sequence. */
| 307 | * hook instructions. Keep valid UTF-8 sequences, collapse ASCII controls to a |
| 308 | * space, and bound each field without splitting a multibyte sequence. */ |
| 309 | static void ha_sanitize_metadata(const char *input, char *output, size_t output_size) { |
| 310 | if (!output || output_size == 0U) { |
| 311 | return; |
| 312 | } |
| 313 | output[0] = '\0'; |
| 314 | if (!input) { |
| 315 | return; |
| 316 | } |
| 317 | size_t used = 0U; |
| 318 | bool previous_space = false; |
| 319 | size_t input_length = strlen(input); |
| 320 | for (size_t pos = 0U; pos < input_length && used + 1U < output_size;) { |
| 321 | unsigned char ch = (unsigned char)input[pos]; |
| 322 | if (ch < 0x20U || ch == 0x7fU) { |
| 323 | if (!previous_space && used > 0U) { |
| 324 | output[used++] = ' '; |
| 325 | previous_space = true; |
| 326 | } |
| 327 | pos++; |
| 328 | continue; |
| 329 | } |
| 330 | size_t sequence = |
| 331 | ha_utf8_sequence_length((const unsigned char *)input + pos, input_length - pos); |
| 332 | if (sequence == 0U) { |
| 333 | output[used++] = '?'; |
| 334 | previous_space = false; |
| 335 | pos++; |
| 336 | continue; |
| 337 | } |
| 338 | if (used + sequence >= output_size) { |
| 339 | break; |
| 340 | } |
| 341 | memcpy(output + used, input + pos, sequence); |
| 342 | used += sequence; |
| 343 | pos += sequence; |
| 344 | previous_space = ch == ' '; |
| 345 | } |
| 346 | while (used > 0U && output[used - 1U] == ' ') { |
| 347 | used--; |
| 348 | } |
| 349 | output[used] = '\0'; |
| 350 | } |
| 351 | |
| 352 | #ifdef CBM_CLI_ENABLE_TEST_API |
| 353 | void cbm_hook_sanitize_metadata_for_testing(const char *input, char *output, size_t output_size) { |
no test coverage detected