Encode one shell word without permitting expansion or command substitution. * POSIX single-quoted strings represent an apostrophe as: '\'' */
| 4850 | /* Encode one shell word without permitting expansion or command substitution. |
| 4851 | * POSIX single-quoted strings represent an apostrophe as: '\'' */ |
| 4852 | static int cbm_shell_quote_word(const char *value, char *out, size_t out_size) { |
| 4853 | if (!value || !out || out_size < CLI_PAIR_LEN) { |
| 4854 | return CLI_ERR; |
| 4855 | } |
| 4856 | size_t used = 0; |
| 4857 | out[used++] = '\''; |
| 4858 | for (const unsigned char *cursor = (const unsigned char *)value; *cursor; cursor++) { |
| 4859 | if (*cursor < 0x20 || *cursor == 0x7f) { |
| 4860 | out[0] = '\0'; |
| 4861 | return CLI_ERR; |
| 4862 | } |
| 4863 | if (*cursor == '\'') { |
| 4864 | static const char escaped_quote[] = "'\\''"; |
| 4865 | if (sizeof(escaped_quote) - CLI_SKIP_ONE > out_size - used - CLI_PAIR_LEN) { |
| 4866 | out[0] = '\0'; |
| 4867 | return CLI_ERR; |
| 4868 | } |
| 4869 | memcpy(out + used, escaped_quote, sizeof(escaped_quote) - CLI_SKIP_ONE); |
| 4870 | used += sizeof(escaped_quote) - CLI_SKIP_ONE; |
| 4871 | } else { |
| 4872 | if (used >= out_size - CLI_PAIR_LEN) { |
| 4873 | out[0] = '\0'; |
| 4874 | return CLI_ERR; |
| 4875 | } |
| 4876 | out[used++] = (char)*cursor; |
| 4877 | } |
| 4878 | } |
| 4879 | if (used >= out_size - CLI_SKIP_ONE) { |
| 4880 | out[0] = '\0'; |
| 4881 | return CLI_ERR; |
| 4882 | } |
| 4883 | out[used++] = '\''; |
| 4884 | out[used] = '\0'; |
| 4885 | return CLI_OK; |
| 4886 | } |
| 4887 | |
| 4888 | /* PowerShell single-quoted words are literal; an apostrophe is represented by |
| 4889 | * two apostrophes. This prevents $env expansion and command substitution. */ |
no outgoing calls
no test coverage detected