aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-04 18:46:46 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-14 08:27:03 +0200
commitd08f68523ea9a39af8176703e48fd47a987893b5 (patch)
tree8f9b9d05c6b3fcb3d59c424e9e7b888f70e71b77
parent8fc09516a8028a754e8cd4aecefa0677791379cf (diff)
downloadffwd-d08f68523ea9a39af8176703e48fd47a987893b5.tar.gz
ffwd-d08f68523ea9a39af8176703e48fd47a987893b5.zip
chore: address static analyzer warnings
-rw-r--r--main.c9
-rw-r--r--pplx_embed.c7
-rw-r--r--pplx_embed_mlx.c18
-rw-r--r--pplx_server.c34
-rw-r--r--qwen_asr_tokenizer.c51
5 files changed, 92 insertions, 27 deletions
diff --git a/main.c b/main.c
index a5728d2..18afd00 100644
--- a/main.c
+++ b/main.c
@@ -527,7 +527,6 @@ int main(int argc, char *argv[])
int mlx_quantize_bits = 0;
int mlx_quantize_group_size = 64;
int dist_activation_bits = 32;
- const char *backend = "cpu";
const char *host = "127.0.0.1";
const char *api_key = NULL;
int port = 8000;
@@ -559,10 +558,13 @@ int main(int argc, char *argv[])
}
}
else if (!strcmp(f, "--model")) {
- if (append_model_spec(&model_specs, argv[++arg_start]) != 0) return 1;
+ if (append_model_spec(&model_specs, argv[++arg_start]) != 0) {
+ free_model_specs(&model_specs);
+ return 1;
+ }
}
else if (!strcmp(f, "--backend")) {
- backend = argv[++arg_start];
+ const char *backend = argv[++arg_start];
if (!strcmp(backend, "mlx")) {
#ifdef USE_MLX
use_mlx = 1;
@@ -627,7 +629,6 @@ int main(int argc, char *argv[])
else if (!strcmp(f, "--mlx")) {
#ifdef USE_MLX
use_mlx = 1;
- backend = "mlx";
#else
fprintf(stderr, "--mlx not available (build with: make mlx)\n");
free_model_specs(&model_specs);
diff --git a/pplx_embed.c b/pplx_embed.c
index 35bd739..2c0ade6 100644
--- a/pplx_embed.c
+++ b/pplx_embed.c
@@ -388,8 +388,11 @@ static int tensor_has_supported_shape(const safetensors_file_t *sf,
}
size_t bytes;
- if (mul_size(numel, elem_size, &bytes) != 0 ||
- t->data_size != bytes) {
+ if (mul_size(numel, elem_size, &bytes) != 0) {
+ fprintf(stderr, "pplx: tensor data too large for %s\n", name);
+ return 0;
+ }
+ if (t->data_size != bytes) {
fprintf(stderr, "pplx: bad data size for %s: got %zu, expected %zu\n",
name, t->data_size, bytes);
return 0;
diff --git a/pplx_embed_mlx.c b/pplx_embed_mlx.c
index ca98c5f..6e5476a 100644
--- a/pplx_embed_mlx.c
+++ b/pplx_embed_mlx.c
@@ -118,8 +118,11 @@ static int mlx_tensor_has_supported_shape(const safetensors_file_t *sf,
}
size_t bytes;
- if (mlx_mul_size(numel, elem_size, &bytes) != 0 ||
- t->data_size != bytes) {
+ if (mlx_mul_size(numel, elem_size, &bytes) != 0) {
+ fprintf(stderr, "mlx: tensor data too large for %s\n", name);
+ return 0;
+ }
+ if (t->data_size != bytes) {
fprintf(stderr, "mlx: bad data size for %s: got %zu, expected %zu\n",
name, t->data_size, bytes);
return 0;
@@ -1484,14 +1487,19 @@ int pplx_mlx_forward_slice_batch(pplx_mlx_ctx_t *ctx,
if (inputs[b].n_tokens != max_seq) has_padding = 1;
}
- size_t rows, state_values, ids_bytes, mask_bytes, state_bytes, row_bytes;
+ size_t rows, ids_bytes, mask_bytes, state_bytes, row_bytes;
if (mlx_mul_size((size_t)batch, (size_t)max_seq, &rows) != 0 ||
- mlx_mul_size(rows, (size_t)hidden, &state_values) != 0 ||
mlx_mul_size(rows, sizeof(int), &ids_bytes) != 0 ||
mlx_mul_size(rows, sizeof(float), &mask_bytes) != 0 ||
- mlx_mul_size(state_values, sizeof(float), &state_bytes) != 0 ||
mlx_mul_size((size_t)hidden, sizeof(float), &row_bytes) != 0)
return -1;
+ if (rows == 0 || row_bytes == 0 || (has_padding && mask_bytes == 0))
+ return -1;
+ if (layer_start != 0 &&
+ mlx_mul_size(rows, (size_t)hidden, &state_bytes) != 0)
+ return -1;
+ if (layer_start == 0)
+ state_bytes = 0;
int *padded_ids = layer_start == 0
? (int *)malloc(ids_bytes) : NULL;
diff --git a/pplx_server.c b/pplx_server.c
index 63c363d..85df0e5 100644
--- a/pplx_server.c
+++ b/pplx_server.c
@@ -371,6 +371,16 @@ static void client_decref(client *c) {
if (free_now) client_free(c);
}
+static void client_decref_n(client *c, int n) {
+ int free_now = 0;
+ if (n <= 0) return;
+ pthread_mutex_lock(&c->srv->mu);
+ c->refcount -= n;
+ if (c->refcount <= 0) free_now = 1;
+ pthread_mutex_unlock(&c->srv->mu);
+ if (free_now) client_free(c);
+}
+
static void client_unlink(client *c) {
http_server *s = c->srv;
if (!c->linked) return;
@@ -382,8 +392,8 @@ static void client_unlink(client *c) {
if (s->n_clients > 0) s->n_clients--;
}
-static void close_client(client *c) {
- if (!c || c->cancelled) return;
+static int close_client_unlink(client *c) {
+ if (!c || c->cancelled) return 0;
c->cancelled = 1;
if (c->fd >= 0) {
aeDeleteFileEvent(c->srv->loop, c->fd, AE_READABLE);
@@ -392,6 +402,11 @@ static void close_client(client *c) {
c->fd = -1;
}
client_unlink(c);
+ return 1;
+}
+
+static void close_client(client *c) {
+ if (!close_client_unlink(c)) return;
client_decref(c);
}
@@ -734,15 +749,16 @@ static void completion_cb(aeEventLoop *loop, int fd, void *clientData, int mask)
job *j;
while ((j = pop_done(s)) != NULL) {
client *c = j->c;
+ int refs_to_drop = 1; /* job reference */
if (!c->cancelled) {
append_http_response_ex(c, j->status, j->content_type,
j->extra_headers,
j->response ? j->response : "",
j->response ? j->response_len : 0);
if (queue_write(c) != AE_OK)
- close_client(c);
+ refs_to_drop += close_client_unlink(c);
}
- client_decref(c);
+ client_decref_n(c, refs_to_drop);
job_free(j);
}
}
@@ -1594,13 +1610,13 @@ static void prepare_embedding_request(job *j, cJSON *root, http_server *s,
return;
}
- if (cJSON_GetArraySize(detail) == 0 && m) {
+ if (cJSON_GetArraySize(detail) == 0 && m && input) {
out->n_inputs = n_inputs;
out->tokens = xcalloc((size_t)n_inputs, sizeof(*out->tokens));
out->inputs = xmalloc((size_t)n_inputs * sizeof(*out->inputs));
int idx = 0;
- if (cJSON_IsString(input)) {
+ if (input && cJSON_IsString(input) && input->valuestring) {
if (tokenize_one(m, j, input->valuestring, &out->tokens[0]) != 0) {
ve_add(detail, "[\"body\",\"input\"]", "tokenization failed",
"value_error.tokenization");
@@ -1618,6 +1634,12 @@ static void prepare_embedding_request(job *j, cJSON *root, http_server *s,
cJSON_ArrayForEach(item, input) {
char loc[64];
snprintf(loc, sizeof(loc), "[\"body\",\"input\",%d]", idx);
+ if (!cJSON_IsString(item) || !item->valuestring) {
+ ve_add(detail, loc, "input item must be a string",
+ "type_error.string");
+ idx++;
+ continue;
+ }
if (tokenize_one(m, j, item->valuestring,
&out->tokens[idx]) != 0) {
ve_add(detail, loc, "tokenization failed",
diff --git a/qwen_asr_tokenizer.c b/qwen_asr_tokenizer.c
index 8c86f22..fa85017 100644
--- a/qwen_asr_tokenizer.c
+++ b/qwen_asr_tokenizer.c
@@ -82,8 +82,8 @@ static char *decode_gpt2_token(const char *token_str) {
const unsigned char *end = p + len;
while (p < end) {
- int cp;
- int nbytes;
+ int cp = 0;
+ int nbytes = 1;
if ((*p & 0x80) == 0) {
cp = *p;
@@ -256,6 +256,28 @@ static int map_insert(str_int_entry_t *map, int cap, char *key, int value) {
return -1;
}
+static int map_insert_owned(str_int_entry_t *map, int cap, char **keyp, int value) {
+ if (!keyp) return -1;
+ char *key = *keyp;
+ if (!map || cap <= 0 || !key) return -1;
+ int mask = cap - 1;
+ int pos = (int)(fnv1a_hash(key) & (uint64_t)mask);
+ for (int i = 0; i < cap; i++) {
+ int idx = (pos + i) & mask;
+ if (!map[idx].key) {
+ map[idx].key = key;
+ map[idx].value = value;
+ *keyp = NULL;
+ return 0;
+ }
+ if (strcmp(map[idx].key, key) == 0) {
+ map[idx].value = value;
+ return 0;
+ }
+ }
+ return -1;
+}
+
static int map_get(const str_int_entry_t *map, int cap, const char *key) {
if (!map || cap <= 0 || !key) return -1;
int mask = cap - 1;
@@ -1244,7 +1266,10 @@ static int load_merges_map(qwen_tokenizer_t *tok, const char *merges_path) {
return -1;
}
- rewind(f);
+ if (fseek(f, 0, SEEK_SET) != 0) {
+ fclose(f);
+ return -1;
+ }
int rank = 0;
while (fgets(line, sizeof(line), f)) {
char *a = NULL, *b = NULL;
@@ -1258,9 +1283,9 @@ static int load_merges_map(qwen_tokenizer_t *tok, const char *merges_path) {
memcpy(key + la + 1, b, lb);
key[la + 1 + lb] = '\0';
- if (map_insert((str_int_entry_t *)tok->merge_map, tok->merge_map_cap, key, rank) != 0) {
- free(key);
- }
+ (void)map_insert_owned((str_int_entry_t *)tok->merge_map,
+ tok->merge_map_cap, &key, rank);
+ free(key);
rank++;
}
fclose(f);
@@ -1278,9 +1303,15 @@ qwen_tokenizer_t *qwen_tokenizer_load(const char *vocab_json_path) {
return NULL;
}
- fseek(f, 0, SEEK_END);
+ if (fseek(f, 0, SEEK_END) != 0) {
+ fclose(f);
+ return NULL;
+ }
long file_size = ftell(f);
- fseek(f, 0, SEEK_SET);
+ if (file_size < 0 || fseek(f, 0, SEEK_SET) != 0) {
+ fclose(f);
+ return NULL;
+ }
char *json = (char *)malloc((size_t)file_size + 1);
if (!json || fread(json, 1, (size_t)file_size, f) != (size_t)file_size) {
@@ -1306,7 +1337,7 @@ qwen_tokenizer_t *qwen_tokenizer_load(const char *vocab_json_path) {
if (*p == ',') { p++; continue; }
if (*p == '}') break;
- char key[4096];
+ char key[4096] = {0};
if (parse_json_string(&p, key, sizeof(key)) != 0) break;
skip_ws(&p);
if (*p != ':') break;
@@ -1336,7 +1367,7 @@ qwen_tokenizer_t *qwen_tokenizer_load(const char *vocab_json_path) {
if (*p == ',') { p++; continue; }
if (*p == '}') break;
- char key[4096];
+ char key[4096] = {0};
if (parse_json_string(&p, key, sizeof(key)) != 0) break;
skip_ws(&p);
if (*p != ':') break;