Encode one shell word without permitting expansion or command substitution. * POSIX single-quoted strings represent an apostrophe as: '\'' */
| 5446 | /* Encode one shell word without permitting expansion or command substitution. |
| 5447 | * POSIX single-quoted strings represent an apostrophe as: '\'' */ |
| 5448 | static int cbm_shell_quote_word(const char *value, char *out, size_t out_size) { |
| 5449 | if (!value || !out || out_size < CLI_PAIR_LEN) { |
| 5450 | return CLI_ERR; |
| 5451 | } |
| 5452 | size_t used = 0; |
| 5453 | out[used++] = '\''; |
| 5454 | for (const unsigned char *cursor = (const unsigned char *)value; *cursor; cursor++) { |
| 5455 | if (*cursor < 0x20 || *cursor == 0x7f) { |
| 5456 | out[0] = '\0'; |
| 5457 | return CLI_ERR; |
| 5458 | } |
| 5459 | if (*cursor == '\'') { |
| 5460 | static const char escaped_quote[] = "'\\''"; |
| 5461 | if (sizeof(escaped_quote) - CLI_SKIP_ONE > out_size - used - CLI_PAIR_LEN) { |
| 5462 | out[0] = '\0'; |
| 5463 | return CLI_ERR; |
| 5464 | } |
| 5465 | memcpy(out + used, escaped_quote, sizeof(escaped_quote) - CLI_SKIP_ONE); |
| 5466 | used += sizeof(escaped_quote) - CLI_SKIP_ONE; |
| 5467 | } else { |
| 5468 | if (used >= out_size - CLI_PAIR_LEN) { |
| 5469 | out[0] = '\0'; |
| 5470 | return CLI_ERR; |
| 5471 | } |
| 5472 | out[used++] = (char)*cursor; |
| 5473 | } |
| 5474 | } |
| 5475 | if (used >= out_size - CLI_SKIP_ONE) { |
| 5476 | out[0] = '\0'; |
| 5477 | return CLI_ERR; |
| 5478 | } |
| 5479 | out[used++] = '\''; |
| 5480 | out[used] = '\0'; |
| 5481 | return CLI_OK; |
| 5482 | } |
| 5483 | |
| 5484 | /* PowerShell single-quoted words are literal; an apostrophe is represented by |
| 5485 | * two apostrophes. This prevents $env expansion and command substitution. */ |
no outgoing calls
no test coverage detected