| 1029 | } |
| 1030 | |
| 1031 | static char *agent_json_escape(const char *value) { |
| 1032 | size_t length = strlen(value); |
| 1033 | if (length > (SIZE_MAX - 1U) / 6U) { |
| 1034 | return NULL; |
| 1035 | } |
| 1036 | char *escaped = (char *)malloc(length * 6U + 1U); |
| 1037 | if (!escaped) { |
| 1038 | return NULL; |
| 1039 | } |
| 1040 | size_t output = 0U; |
| 1041 | for (size_t i = 0U; i < length; i++) { |
| 1042 | unsigned char byte = (unsigned char)value[i]; |
| 1043 | if (byte == '"' || byte == '\\') { |
| 1044 | escaped[output++] = '\\'; |
| 1045 | escaped[output++] = (char)byte; |
| 1046 | } else if (byte == '\b') { |
| 1047 | escaped[output++] = '\\'; |
| 1048 | escaped[output++] = 'b'; |
| 1049 | } else if (byte == '\f') { |
| 1050 | escaped[output++] = '\\'; |
| 1051 | escaped[output++] = 'f'; |
| 1052 | } else if (byte == '\n') { |
| 1053 | escaped[output++] = '\\'; |
| 1054 | escaped[output++] = 'n'; |
| 1055 | } else if (byte == '\r') { |
| 1056 | escaped[output++] = '\\'; |
| 1057 | escaped[output++] = 'r'; |
| 1058 | } else if (byte == '\t') { |
| 1059 | escaped[output++] = '\\'; |
| 1060 | escaped[output++] = 't'; |
| 1061 | } else if (byte < 0x20U) { |
| 1062 | int written = snprintf(escaped + output, 7U, "\\u%04x", (unsigned)byte); |
| 1063 | if (written != 6) { |
| 1064 | free(escaped); |
| 1065 | return NULL; |
| 1066 | } |
| 1067 | output += 6U; |
| 1068 | } else { |
| 1069 | escaped[output++] = (char)byte; |
| 1070 | } |
| 1071 | } |
| 1072 | escaped[output] = '\0'; |
| 1073 | return escaped; |
| 1074 | } |
| 1075 | |
| 1076 | static char *agent_json_canonical(cbm_agent_client_id_t id, const char *binary_path) { |
| 1077 | char *escaped = agent_json_escape(binary_path); |
no outgoing calls
no test coverage detected