Encode one shell word without permitting expansion or command substitution. * POSIX single-quoted strings represent an apostrophe as: '\'' */
| 5314 | /* Encode one shell word without permitting expansion or command substitution. |
| 5315 | * POSIX single-quoted strings represent an apostrophe as: '\'' */ |
| 5316 | static int cbm_shell_quote_word(const char *value, char *out, size_t out_size) { |
| 5317 | if (!value || !out || out_size < CLI_PAIR_LEN) { |
| 5318 | return CLI_ERR; |
| 5319 | } |
| 5320 | size_t used = 0; |
| 5321 | out[used++] = '\''; |
| 5322 | for (const unsigned char *cursor = (const unsigned char *)value; *cursor; cursor++) { |
| 5323 | if (*cursor < 0x20 || *cursor == 0x7f) { |
| 5324 | out[0] = '\0'; |
| 5325 | return CLI_ERR; |
| 5326 | } |
| 5327 | if (*cursor == '\'') { |
| 5328 | static const char escaped_quote[] = "'\\''"; |
| 5329 | if (sizeof(escaped_quote) - CLI_SKIP_ONE > out_size - used - CLI_PAIR_LEN) { |
| 5330 | out[0] = '\0'; |
| 5331 | return CLI_ERR; |
| 5332 | } |
| 5333 | memcpy(out + used, escaped_quote, sizeof(escaped_quote) - CLI_SKIP_ONE); |
| 5334 | used += sizeof(escaped_quote) - CLI_SKIP_ONE; |
| 5335 | } else { |
| 5336 | if (used >= out_size - CLI_PAIR_LEN) { |
| 5337 | out[0] = '\0'; |
| 5338 | return CLI_ERR; |
| 5339 | } |
| 5340 | out[used++] = (char)*cursor; |
| 5341 | } |
| 5342 | } |
| 5343 | if (used >= out_size - CLI_SKIP_ONE) { |
| 5344 | out[0] = '\0'; |
| 5345 | return CLI_ERR; |
| 5346 | } |
| 5347 | out[used++] = '\''; |
| 5348 | out[used] = '\0'; |
| 5349 | return CLI_OK; |
| 5350 | } |
| 5351 | |
| 5352 | /* PowerShell single-quoted words are literal; an apostrophe is represented by |
| 5353 | * two apostrophes. This prevents $env expansion and command substitution. */ |
no outgoing calls
no test coverage detected