commit 708db91e9975228bedb5b29d86312f90eddb922d
parent 1af75030881741bb67be1413159ae52883481feb
Author: Mario Rosell R. Martinez <mario@mariorosell.es>
Date: Sat, 28 Mar 2026 21:22:52 +0100
anntp_article: Fix very rare memory leak on anntp_article failure
Diffstat:
| M | anntp.h | | | 23 | +++++++++++++---------- |
1 file changed, 13 insertions(+), 10 deletions(-)
diff --git a/anntp.h b/anntp.h
@@ -320,6 +320,8 @@ anntp_article(AnntpConnection* cv, size_t n, AnntpArticle* art)
char cmd[ANNTP_BUFSIZE];
char line[ANNTP_BUFSIZE];
+ memset(art, 0, sizeof(*art));
+
snprintf(cmd, sizeof(cmd), "ARTICLE %zu", n);
if (anntp_writeline(cv, cmd) <= 0)
@@ -338,13 +340,8 @@ anntp_article(AnntpConnection* cv, size_t n, AnntpArticle* art)
return ANNTPE(ANE_IO);
ssize_t len = anntp_readdot(cv, buf, cap);
- if (len < 0) {
- ANNTP_FREE(buf);
- return len;
- }
-
- memset(art, 0, sizeof(*art));
- art->n = (AnntpArticleNumber)n;
+ if (len < 0)
+ goto error;
char* p = buf;
char* body_start = NULL;
@@ -382,13 +379,19 @@ anntp_article(AnntpConnection* cv, size_t n, AnntpArticle* art)
if (body_start) {
size_t blen = strlen(body_start);
art->body = (char*)ANNTP_MALLOC(blen + 1);
- if (art->body) {
- memcpy(art->body, body_start, blen + 1);
- }
+ if (!art->body)
+ goto error;
+
+ memcpy(art->body, body_start, blen + 1);
}
ANNTP_FREE(buf);
return (ssize_t)len;
+
+error:
+ anntp_article_free(art);
+ ANNTP_FREE(buf);
+ return ANNTPE(ANE_IO);
}
ANNTP_API void