* @internal * This function acts the same as __json_snprintf(buf, len, "%s%s%s", prefix, str, suffix) * except that it does proper escaping of "str" as necessary. Prefix and suffix should be compile- * time constants, or values not needing escaping. * Drops any invalid characters we don't support */
| 131 | * Drops any invalid characters we don't support |
| 132 | */ |
| 133 | static inline int |
| 134 | __json_format_str(char *buf, const int len, const char *prefix, const char *str, const char *suffix) |
| 135 | { |
| 136 | int ret; |
| 137 | char saved[4] = ""; |
| 138 | char *tmp; |
| 139 | |
| 140 | if (strnlen(buf, sizeof(saved)) < sizeof(saved)) { |
| 141 | /* we have only a few bytes in buffer, so save them off to restore on error*/ |
| 142 | strcpy(saved, buf); |
| 143 | ret = __json_format_str_to_buf(buf, len, prefix, str, suffix); |
| 144 | if (ret == 0) |
| 145 | strcpy(buf, saved); /* restore */ |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | tmp = malloc(len); |
| 150 | if (tmp == NULL) |
| 151 | return 0; |
| 152 | |
| 153 | ret = __json_format_str_to_buf(tmp, len, prefix, str, suffix); |
| 154 | if (ret > 0) |
| 155 | strcpy(buf, tmp); |
| 156 | |
| 157 | free(tmp); |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | /* Copies an empty array into the provided buffer. */ |
| 162 | static inline int |
no test coverage detected