xonecas.io
7814
Ta

tiny-c-agent / commit ed67a1e

fix: memory fixes

xonecas2026-10-03 ed67a1e0c17a75040702351065115a81aaf112b7 parent 8cc899e

6 files changed

src/api/anthropic_messages.c+7−2
@@ -192,6 +192,10 @@ typedef struct {
192192 void *user;
193193 } anth_state;
194194
195+// An untrusted stream index is a hint, never a size. Anything past this is
196+// treated like a missing index so a hostile chunk cannot force an allocation.
197+#define MAX_STREAM_INDEX 10000
198+
195199 static block_acc *block_at(anth_state *st, size_t index) {
196200 if (index >= st->blocks_len) {
197201 st->blocks = realloc(st->blocks, (index + 1) * sizeof *st->blocks);
@@ -227,11 +231,12 @@ static void on_data(const char *json, size_t len, void *user) {
227231 }
228232 } else if (strcmp(type, "content_block_start") == 0) {
229233 yyjson_val *iv = yyjson_obj_get(root, "index");
230- if (!yyjson_is_int(iv)) {
234+ long raw = yyjson_is_int(iv) ? (long)yyjson_get_int(iv) : -1;
235+ if (raw < 0 || raw > MAX_STREAM_INDEX) {
231236 yyjson_doc_free(doc);
232237 return;
233238 }
234- block_acc *b = block_at(st, (size_t)yyjson_get_int(iv));
239+ block_acc *b = block_at(st, (size_t)raw);
235240 yyjson_val *cb = yyjson_obj_get(root, "content_block");
236241 yyjson_val *bt = yyjson_is_obj(cb) ? yyjson_obj_get(cb, "type") : NULL;
237242 const char *bts = yyjson_is_str(bt) ? yyjson_get_str(bt) : "";
src/api/openai_completions.c+6−1
@@ -183,6 +183,10 @@ typedef struct {
183183 void *user;
184184 } sse_state;
185185
186+// An untrusted stream index is a hint, never a size. Anything past this is
187+// treated like a missing index so a hostile chunk cannot force an allocation.
188+#define MAX_STREAM_INDEX 10000
189+
186190 static void grow_calls(sse_state *st, size_t count) {
187191 if (count <= st->calls_len) return;
188192 st->calls = realloc(st->calls, count * sizeof *st->calls);
@@ -215,7 +219,8 @@ static void handle_delta(sse_state *st, yyjson_val *delta) {
215219 yyjson_val *tc;
216220 yyjson_arr_foreach(tcs, idx, max, tc) {
217221 yyjson_val *iv = yyjson_obj_get(tc, "index");
218- size_t index = yyjson_is_int(iv) ? (size_t)yyjson_get_int(iv) : st->calls_len;
222+ long raw = yyjson_is_int(iv) ? (long)yyjson_get_int(iv) : -1;
223+ size_t index = raw >= 0 && (size_t)raw <= MAX_STREAM_INDEX ? (size_t)raw : st->calls_len;
219224 grow_calls(st, index + 1);
220225 tool_acc *a = &st->calls[index];
221226 yyjson_val *id = yyjson_obj_get(tc, "id");
src/api/openai_responses.c+17−4
@@ -147,6 +147,10 @@ typedef struct {
147147 void *user;
148148 } resp_state;
149149
150+// An untrusted stream index is a hint, never a size. Anything past this is
151+// treated like a missing index so a hostile chunk cannot force an allocation.
152+#define MAX_STREAM_INDEX 10000
153+
150154 static void grow_calls(resp_state *st, size_t count) {
151155 if (count <= st->calls_len) return;
152156 st->calls = realloc(st->calls, count * sizeof *st->calls);
@@ -155,6 +159,15 @@ static void grow_calls(resp_state *st, size_t count) {
155159 st->calls_len = count;
156160 }
157161
162+// A stream index from the wire, or `fallback` when absent, negative, or past
163+// the bound. An untrusted index is a hint, never a size, so it cannot force an
164+// allocation.
165+static size_t stream_index(yyjson_val *iv, size_t fallback) {
166+ if (!yyjson_is_int(iv)) return fallback;
167+ long i = (long)yyjson_get_int(iv);
168+ return i < 0 || i > MAX_STREAM_INDEX ? fallback : (size_t)i;
169+}
170+
158171 static void on_data(const char *json, size_t len, void *user) {
159172 resp_state *st = user;
160173 yyjson_doc *doc = yyjson_read(json, len, 0);
@@ -180,7 +193,7 @@ static void on_data(const char *json, size_t len, void *user) {
180193 yyjson_val *it = yyjson_is_obj(item) ? yyjson_obj_get(item, "type") : NULL;
181194 if (yyjson_is_str(it) && strcmp(yyjson_get_str(it), "function_call") == 0) {
182195 yyjson_val *iv = yyjson_obj_get(root, "output_index");
183- size_t index = yyjson_is_int(iv) ? (size_t)yyjson_get_int(iv) : st->calls_len;
196+ size_t index = stream_index(iv, st->calls_len);
184197 grow_calls(st, index + 1);
185198 call_acc *a = &st->calls[index];
186199 yyjson_val *id = yyjson_obj_get(item, "call_id");
@@ -210,14 +223,14 @@ static void on_data(const char *json, size_t len, void *user) {
210223 }
211224 } else if (strcmp(type, "response.function_call_arguments.delta") == 0) {
212225 yyjson_val *iv = yyjson_obj_get(root, "output_index");
213- size_t index = yyjson_is_int(iv) ? (size_t)yyjson_get_int(iv) : st->calls_len;
226+ size_t index = stream_index(iv, st->calls_len);
214227 if (index < st->calls_len) {
215228 yyjson_val *d = yyjson_obj_get(root, "delta");
216229 if (yyjson_is_str(d)) buf_add(&st->calls[index].args, yyjson_get_str(d), strlen(yyjson_get_str(d)));
217230 }
218231 } else if (strcmp(type, "response.function_call_arguments.done") == 0) {
219232 yyjson_val *iv = yyjson_obj_get(root, "output_index");
220- size_t index = yyjson_is_int(iv) ? (size_t)yyjson_get_int(iv) : st->calls_len;
233+ size_t index = stream_index(iv, st->calls_len);
221234 if (index < st->calls_len) {
222235 yyjson_val *a = yyjson_obj_get(root, "arguments");
223236 if (yyjson_is_str(a)) {
@@ -231,7 +244,7 @@ static void on_data(const char *json, size_t len, void *user) {
231244 yyjson_val *it = yyjson_is_obj(item) ? yyjson_obj_get(item, "type") : NULL;
232245 if (yyjson_is_str(it) && strcmp(yyjson_get_str(it), "function_call") == 0) {
233246 yyjson_val *iv = yyjson_obj_get(root, "output_index");
234- size_t index = yyjson_is_int(iv) ? (size_t)yyjson_get_int(iv) : st->calls_len;
247+ size_t index = stream_index(iv, st->calls_len);
235248 if (index < st->calls_len) {
236249 call_acc *a = &st->calls[index];
237250 yyjson_val *id = yyjson_obj_get(item, "call_id");
src/config.c+4−2
@@ -199,9 +199,11 @@ static int parse_partial(yyjson_val *root, const char *path, partial *p, char **
199199 size_t idx, max;
200200 yyjson_val *e;
201201 yyjson_arr_foreach(v, idx, max, e) {
202- if (parse_provider(e, idx, path, &p->custom_providers[p->custom_providers_len], err) != 0)
203- return -1;
202+ // Count the slot before filling it, so a parse failure still frees
203+ // the fields that were already set.
204204 p->custom_providers_len++;
205+ if (parse_provider(e, idx, path, &p->custom_providers[p->custom_providers_len - 1], err) != 0)
206+ return -1;
205207 }
206208 }
207209 return 0;
src/main.c+16−0
@@ -69,6 +69,9 @@ int main(int argc, char **argv) {
6969 tiny_config *cfg = tiny_config_load(config_path, &err);
7070 if (!cfg) {
7171 fprintf(stderr, "%s\n", err ? err : "config error");
72+ free(err);
73+ free(stdin_prompt);
74+ curl_global_cleanup();
7275 return 1;
7376 }
7477
@@ -77,6 +80,10 @@ int main(int argc, char **argv) {
7780 if (tiny_resolve_model(cfg, &model, &err) != 0) {
7881 if (print_mode) {
7982 fprintf(stderr, "%s\n", err ? err : "model error");
83+ free(err);
84+ tiny_config_free(cfg);
85+ free(stdin_prompt);
86+ curl_global_cleanup();
8087 return 1;
8188 }
8289 // Keep the interactive TUI usable: the user can pick a provider with
@@ -91,6 +98,10 @@ int main(int argc, char **argv) {
9198 tiny_resources *resources = tiny_resources_build(cfg, cwd, &err);
9299 if (!resources) {
93100 fprintf(stderr, "%s\n", err ? err : "prompt error");
101+ free(err);
102+ tiny_config_free(cfg);
103+ free(stdin_prompt);
104+ curl_global_cleanup();
94105 return 1;
95106 }
96107
@@ -117,6 +128,11 @@ int main(int argc, char **argv) {
117128 tiny_session *session = tiny_session_open(cfg->sessions_dir, cwd, prompt, &err);
118129 if (!session) {
119130 fprintf(stderr, "%s\n", err ? err : "session error");
131+ free(err);
132+ tiny_resources_free(resources);
133+ tiny_config_free(cfg);
134+ free(stdin_prompt);
135+ curl_global_cleanup();
120136 return 1;
121137 }
122138
src/tool.c+1−1
@@ -433,7 +433,7 @@ static void tool_bash(const char *args, tiny_tool_output_fn out, void *user,
433433 break;
434434 }
435435 if (pr == 0) continue;
436- ssize_t n = read(pipefd[0], chunk, sizeof chunk);
436+ ssize_t n = read(pipefd[0], chunk, sizeof chunk - 1);
437437 if (n < 0) {
438438 if (errno == EINTR) continue;
439439 break;