Merge pull request #6002 from rgerhards/statan2

mmaitag: fix potential memory leak
This commit is contained in:
Rainer Gerhards 2025-08-22 12:45:55 +02:00 committed by GitHub
commit 3e64f45bb8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -75,10 +75,10 @@ static rsRetVal gemini_classify_batch(ai_provider_t *prov, const char **messages
const char *mock = getenv("GEMINI_MOCK_RESPONSE"); const char *mock = getenv("GEMINI_MOCK_RESPONSE");
(void)prov; (void)prov;
(void)messages; (void)messages;
char *tmp; char *tmp = NULL;
char *tok; char *tok = NULL;
char *saveptr = NULL; char *saveptr = NULL;
char **out; char **out = NULL;
size_t idx = 0; size_t idx = 0;
DEFiRet; DEFiRet;
@ -96,21 +96,29 @@ static rsRetVal gemini_classify_batch(ai_provider_t *prov, const char **messages
for (size_t i = 0; i < next && tok != NULL; ++i) tok = strtok_r(NULL, ",", &saveptr); for (size_t i = 0; i < next && tok != NULL; ++i) tok = strtok_r(NULL, ",", &saveptr);
CHKmalloc(out = calloc(n, sizeof(char *))); CHKmalloc(out = calloc(n, sizeof(char *)));
for (idx = 0; idx < n; ++idx) { for (idx = 0; idx < n; ++idx) {
if (tok != NULL) { if (tok != NULL) {
out[idx] = strdup(tok); CHKmalloc(out[idx] = strdup(tok));
tok = strtok_r(NULL, ",", &saveptr); tok = strtok_r(NULL, ",", &saveptr);
} else { } else {
// Fallback if we run out of mock tags // Fallback if we run out of mock tags
out[idx] = strdup("REGULAR"); CHKmalloc(out[idx] = strdup("REGULAR"));
} }
} }
next += n; // Update state for the next call next += n; // Update state for the next call
free(tmp);
*tags = out; *tags = out;
out = NULL;
finalize_it: finalize_it:
if (out != NULL) {
for (idx = 0; idx < n; ++idx) {
free(out[idx]);
}
free(out);
}
free(tmp);
RETiRet; RETiRet;
} }