src/createfp.c    |   10 ++++
 src/fingerprint.c |  110 +++++++++++++++++++++++++++++++++++++++++-------------
 src/testtextcat.c |   10 ++++
 src/textcat.c     |   56 +++++++++++++++++++++------
 src/wg_mempool.c  |   53 ++++++++++++++++++++------
 5 files changed, 188 insertions(+), 51 deletions(-)

New commits:
commit 5b8f88f1964865d1721e7de1da81d92a3cd1d4a1
Author:     Caolán McNamara <[email protected]>
AuthorDate: Sat Nov 7 23:59:22 2026 +0000
Commit:     Caolán McNamara <[email protected]>
CommitDate: Sun Nov 8 01:08:37 2026 +0100

    check the input read by testtextcat and createfp
    
    testtextcat cut its input at 1024 bytes even when the input was
    shorter and its buffer had been shrunk to fit
    
    Change-Id: I58eb9c62f505c6db091cae2c17e666206555077a
    Reviewed-on: https://gerrit.libreoffice.org/c/libexttextcat/+/213828
    Reviewed-by: Caolán McNamara <[email protected]>
    Tested-by: Caolán McNamara <[email protected]>

diff --git a/src/createfp.c b/src/createfp.c
index fabc9cd..c75fbb6 100644
--- a/src/createfp.c
+++ b/src/createfp.c
@@ -93,8 +93,18 @@ int main(int argc, char **args)
     }
 
     buf = myread(stdin);
+    if (!buf)
+    {
+        fprintf(stderr, "Unable to read input
");
+        exit(-1);
+    }
 
     h = fp_Init(NULL);
+    if (!h)
+    {
+        fprintf(stderr, "There was an error creating the fingerprint
");
+        exit(-1);
+    }
     if (utfaware)
     {
         fp_SetProperty(h, TCPROP_UTF8AWARE, TC_TRUE);
diff --git a/src/testtextcat.c b/src/testtextcat.c
index 2ac82fe..e2b7bc4 100644
--- a/src/testtextcat.c
+++ b/src/testtextcat.c
@@ -109,9 +109,17 @@ int main(int argc, char **argv)
     textcat_SetProperty(h, TCPROP_UTF8AWARE, utfaware ? TC_TRUE : TC_FALSE);
 
     buf = myread(stdin);
+    if (!buf)
+    {
+        fprintf(stderr, "Unable to read input, Aborting.
");
+        exit(-1);
+    }
 
     /*** We only need a little text to determine the language ***/
-    buf[1024] = '
+    if (strlen(buf) > 1024)
+    {
+        buf[1024] = '
+    }
     result = textcat_Classify(h, buf, strlen(buf) + 1);
     printf("%s
", result);
 
commit cdfa1df4447aaf90d17a93e8e39d494ce8cab60c
Author:     Caolán McNamara <[email protected]>
AuthorDate: Sat Nov 7 23:58:59 2026 +0000
Commit:     Caolán McNamara <[email protected]>
CommitDate: Sun Nov 8 01:08:33 2026 +0100

    handle allocation failure in fingerprint.c and wg_mempool.c
    
    the n-gram table builders now report a failed pool allocation, so
    fp_Create can fail and free what it has
    
    Change-Id: I5daffac33cb766af1e1ff1f6ccaa21f5bf7b7503
    Reviewed-on: https://gerrit.libreoffice.org/c/libexttextcat/+/213827
    Tested-by: Caolán McNamara <[email protected]>
    Reviewed-by: Caolán McNamara <[email protected]>

diff --git a/src/fingerprint.c b/src/fingerprint.c
index f086686..899aab6 100644
--- a/src/fingerprint.c
+++ b/src/fingerprint.c
@@ -159,6 +159,10 @@ static int increasefreq(table_t * t, char *p, int len,
 
     /*** Not found, so create ***/
     entry = (entry_t *) (wgmempool_alloc(t->pool, sizeof(entry_t)));
+    if (!entry)
+    {
+        return 0;
+    }
     strncpy(entry->str, p, MAXNGRAMSIZE);
     entry->str[MAXNGRAMSIZE] = 0;
     entry->cnt = 1;
@@ -282,39 +286,58 @@ static int table2heap(table_t * t)
     return 1;
 }
 
+static void tabledone(table_t * t)
+{
+    if (!t)
+        return;
+
+    if (t->pool)
+        wgmempool_Done(t->pool);
+    free(t->table);
+    free(t->heap);
+    free(t);
+}
+
 static table_t *inittable(uint4 maxngrams)
 {
     table_t *result = (table_t *) calloc(1, sizeof(table_t));
+    if (!result)
+    {
+        return NULL;
+    }
     result->table = (entry_t **) calloc(1, sizeof(entry_t *) * TABLESIZE);
     result->pool = wgmempool_Init(10000, 10);
-
     result->heap = (entry_t *) malloc(sizeof(entry_t) * maxngrams);
+    if (!result->table || !result->pool || !result->heap)
+    {
+        tabledone(result);
+        return NULL;
+    }
     result->heapsize = maxngrams;
     result->size = 0;
 
     return result;
 }
 
-static void tabledone(table_t * t)
-{
-    if (!t)
-        return;
-
-    wgmempool_Done(t->pool);
-    free(t->table);
-    free(t->heap);
-    free(t);
-}
 
 extern void *fp_Init(const char *name)
 {
     fp_t *h = (fp_t *) calloc(1, sizeof(fp_t));
 
+    if (!h)
+        return NULL;
     h->utfaware = TC_TRUE;
     h->mindocsize = MINDOCSIZE;
 
     if (name)
+    {
         h->name = strdup(name);
+        if (!h->name)
+        {
+            free(h);
+            return NULL;
+        }
+    }
 
     return (void *)h;
 }
@@ -356,6 +379,11 @@ static char *prepbuffer(const char *src, size_t bufsize, 
uint4 mindocsize)
     char *w = dest;
     char *wlimit = dest + bufsize + 1;
 
+    if (!dest)
+    {
+        return NULL;
+    }
+
     if (p == plimit || *p == '
     {
         goto END;
@@ -431,7 +459,7 @@ static char *prepbuffer(const char *src, size_t bufsize, 
uint4 mindocsize)
 * this function extract all n-gram from past buffer and put them into the 
table "t"
 * [modified] by Jocelyn Merand to accept utf-8 multi-character symbols to be 
used in OpenOffice
 */
-static void utfcreatengramtable(table_t * t, const char *buf)
+static int utfcreatengramtable(table_t * t, const char *buf)
 {
     char n[MAXNGRAMSIZE + 1];
     const char *p = buf;
@@ -451,11 +479,12 @@ static void utfcreatengramtable(table_t * t, const char 
*buf)
         m += decay;             /* [modified] */
         *m = '
 
-        increasefreq(t, n, 1, utf8_issame);
+        if (!increasefreq(t, n, 1, utf8_issame))
+            return 0;
 
 
         if (*q == '
-            return;
+            return 1;
 
         /*** Let the compiler unroll this ***/
         for (i = 2; i <= MAXNGRAMSYMBOL; i++)
@@ -464,18 +493,19 @@ static void utfcreatengramtable(table_t * t, const char 
*buf)
             m += decay;
             *m = '
 
-            increasefreq(t, n, i, utf8_issame);
+            if (!increasefreq(t, n, i, utf8_issame))
+                return 0;
 
             if (*q == '_')
                 break;
             q += decay;
             if (*q == '
-                return;
+                return 1;
         }
 
         p = utf8_next_char(p);  /* [modified] */
     }
-    return;
+    return 1;
 }
 
 /* checks if n-gram lex is a prefix of key and of length len */
@@ -496,7 +526,7 @@ static int issame(char *lex, char *key, int len)
     return 1;
 }
 
-static void createngramtable(table_t * t, const char *buf)
+static int createngramtable(table_t * t, const char *buf)
 {
     char n[MAXNGRAMSIZE + 1];
     const char *p = buf;
@@ -513,11 +543,12 @@ static void createngramtable(table_t * t, const char *buf)
         *m++ = *q++;
         *m = '
 
-        increasefreq(t, n, 1, issame);
+        if (!increasefreq(t, n, 1, issame))
+            return 0;
 
         if (*q == '
         {
-            return;
+            return 1;
         }
 
         /*** Let the compiler unroll this ***/
@@ -527,18 +558,19 @@ static void createngramtable(table_t * t, const char *buf)
             *m++ = *q;
             *m = '
 
-            increasefreq(t, n, i, issame);
+            if (!increasefreq(t, n, i, issame))
+                return 0;
 
             if (*q == '_')
                 break;
             q++;
             if (*q == '
             {
-                return;
+                return 1;
             }
         }
     }
-    return;
+    return 1;
 }
 
 
@@ -604,6 +636,7 @@ extern int fp_Create(void *handle, const char *buffer, 
size_t bufsize,
     sint4 i = 0;
     table_t *t = NULL;
     char *tmp = NULL;
+    int created;
 
     fp_t *h = (fp_t *) handle;
 
@@ -616,16 +649,27 @@ extern int fp_Create(void *handle, const char *buffer, 
size_t bufsize,
     if (tmp == NULL)
         return 0;
     t = inittable(maxngrams);
+    if (t == NULL)
+    {
+        free(tmp);
+        return 0;
+    }
     /* printf("Table initialized
"); */
 
     /*** Create a hash table containing n-gram counts ***/
     if (h->utfaware)
     {
-        utfcreatengramtable(t, tmp);
+        created = utfcreatengramtable(t, tmp);
     }
     else
     {
-        createngramtable(t, tmp);
+        created = createngramtable(t, tmp);
+    }
+    if (!created)
+    {
+        tabledone(t);
+        free(tmp);
+        return 0;
     }
     /* printf("Table created
"); */
     /*** Take the top N n-grams and add them to the profile ***/
@@ -633,6 +677,12 @@ extern int fp_Create(void *handle, const char *buffer, 
size_t bufsize,
     maxngrams = WGMIN(maxngrams, t->size);
 
     h->fprint = (ngram_t *) malloc(sizeof(ngram_t) * maxngrams);
+    if (h->fprint == NULL)
+    {
+        tabledone(t);
+        free(tmp);
+        return 0;
+    }
     h->size = maxngrams;
 
     /*** Pull n-grams out of heap (backwards) ***/
@@ -672,6 +722,11 @@ extern int fp_Read(void *handle, const char *fname, int 
maxngrams)
     }
 
     h->fprint = (ngram_t *) malloc(maxngrams * sizeof(ngram_t));
+    if (!h->fprint)
+    {
+        fclose(fp);
+        return 0;
+    }
 
     while (cnt < maxngrams && wg_getline(line, 1024, fp))
     {
@@ -713,6 +768,11 @@ extern void fp_Print(void *handle, FILE * fp)
     fp_t *h = (fp_t *) handle;
     ngram_t *tmp = (ngram_t *) malloc(sizeof(ngram_t) * h->size);
 
+    if (!tmp)
+    {
+        return;
+    }
+
     /*** Make a temporary and sort it on rank ***/
     memcpy(tmp, h->fprint, h->size * sizeof(ngram_t));
     qsort(tmp, h->size, sizeof(ngram_t), ngramcmp_rank);
diff --git a/src/wg_mempool.c b/src/wg_mempool.c
index 89d35a1..27d2dc9 100644
--- a/src/wg_mempool.c
+++ b/src/wg_mempool.c
@@ -58,7 +58,7 @@ typedef struct mempool_s
     size_t blocksize;
 } mempool_t;
 
-static void addblock(mempool_t * h)
+static int addblock(mempool_t * h)
 {
     memblock_t *block;
 
@@ -73,13 +73,23 @@ static void addblock(mempool_t * h)
     {
         /*** Make a new block ***/
         block = (memblock_t *) malloc(sizeof(memblock_t));
+        if (!block)
+        {
+            return 0;
+        }
         block->pool = (char *)malloc(h->blocksize);
+        if (!block->pool)
+        {
+            free(block);
+            return 0;
+        }
     }
 
     block->p = block->pool;
     block->pend = block->pool + h->blocksize - h->maxallocsize;
     block->next = h->first;
     h->first = block;
+    return 1;
 }
 
 
@@ -87,11 +97,19 @@ extern void *wgmempool_Init(size_t blocksize, size_t 
maxstrsize)
 {
     mempool_t *result = (mempool_t *) malloc(sizeof(mempool_t));
 
+    if (!result)
+    {
+        return NULL;
+    }
     result->first = NULL;
     result->spare = NULL;
     result->blocksize = blocksize;
     result->maxallocsize = maxstrsize ? (maxstrsize + 1) : 0;
-    addblock(result);
+    if (!addblock(result))
+    {
+        free(result);
+        return NULL;
+    }
 
     return (void *)result;
 }
@@ -166,9 +184,12 @@ extern void *wgmempool_alloc(void *handle, size_t size)
     memblock_t *block = h->first;
 
     /*** Too little space left in block? ***/
-    if (block->p + size > block->pend + h->maxallocsize)
+    if (!block || block->p + size > block->pend + h->maxallocsize)
     {
-        addblock(h);
+        if (!addblock(h))
+        {
+            return NULL;
+        }
         block = h->first;
     }
     result = (void *)block->p;
@@ -185,17 +206,25 @@ extern char *wgmempool_strdup(void *handle, const char 
*str)
     memblock_t *block = h->first;
 
     /*** Create extra room? ***/
-    if (h->maxallocsize)
+    int needblock;
+    if (!block)
     {
-        if (block->p >= block->pend)
-        {
-            addblock(h);
-            block = h->first;
-        }
+        needblock = 1;
     }
-    else if (block->p + strlen(str) + 1 >= block->pend)
+    else if (h->maxallocsize)
+    {
+        needblock = block->p >= block->pend;
+    }
+    else
     {
-        addblock(h);
+        needblock = block->p + strlen(str) + 1 >= block->pend;
+    }
+    if (needblock)
+    {
+        if (!addblock(h))
+        {
+            return NULL;
+        }
         block = h->first;
     }
 
commit c0e6ccdf350d727c34171210725102919b6a0c84
Author:     Caolán McNamara <[email protected]>
AuthorDate: Sat Nov 7 23:57:20 2026 +0000
Commit:     Caolán McNamara <[email protected]>
CommitDate: Sun Nov 8 01:08:30 2026 +0100

    handle allocation failure in textcat.c
    
    grow the arrays through a temporary so a failed realloc leaves the
    handle in a state that textcat_Done can still free
    
    Change-Id: Id524cdfd8b5e50a93edde35dc63317f02aa985a5
    Reviewed-on: https://gerrit.libreoffice.org/c/libexttextcat/+/213826
    Tested-by: Caolán McNamara <[email protected]>
    Reviewed-by: Caolán McNamara <[email protected]>

diff --git a/src/textcat.c b/src/textcat.c
index 98ab644..f6ca85d 100644
--- a/src/textcat.c
+++ b/src/textcat.c
@@ -163,6 +163,11 @@ extern void *special_textcat_Init(const char *conffile, 
const char *prefix)
     }
 
     h = (textcat_t *) malloc(sizeof(textcat_t));
+    if (!h)
+    {
+        fclose(fp);
+        return NULL;
+    }
     h->size = 0;
     h->maxsize = 16;
     h->mindocsize = MINDOCSIZE;
@@ -177,6 +182,10 @@ extern void *special_textcat_Init(const char *conffile, 
const char *prefix)
     finger_print_file_name_size = prefix_size + 1;
     finger_print_file_name =
         (char *)malloc(sizeof(char) * (finger_print_file_name_size + 1024));
+    if (!h->fprint || !h->fprint_disable || !finger_print_file_name)
+    {
+        goto BAILOUT;
+    }
     finger_print_file_name[0] = '
     strcat(finger_print_file_name, prefix);
 
@@ -200,12 +209,25 @@ extern void *special_textcat_Init(const char *conffile, 
const char *prefix)
         /*** Ensure enough space ***/
         if (h->size == h->maxsize)
         {
-            h->maxsize *= 2;
-            h->fprint =
-                (void **)realloc(h->fprint, sizeof(void *) * h->maxsize);
-            h->fprint_disable =
+            void **newfprint;
+            unsigned char *newfprint_disable;
+
+            newfprint =
+                (void **)realloc(h->fprint, sizeof(void *) * h->maxsize * 2);
+            if (!newfprint)
+            {
+                goto BAILOUT;
+            }
+            h->fprint = newfprint;
+            newfprint_disable =
                 (unsigned char *)realloc(h->fprint_disable,
-                                         sizeof(unsigned char) * h->maxsize);
+                                         sizeof(unsigned char) * h->maxsize * 
2);
+            if (!newfprint_disable)
+            {
+                goto BAILOUT;
+            }
+            h->fprint_disable = newfprint_disable;
+            h->maxsize *= 2;
         }
 
         /*** Load data ***/
@@ -281,6 +303,10 @@ extern char *textcat_Classify(void *handle, const char 
*buffer, size_t size)
     if (h->tmp_candidates == NULL)
     {
         h->tmp_candidates = textcat_GetClassifyFullOutput(h);
+        if (h->tmp_candidates == NULL)
+        {
+            return TEXTCAT_RESULT_UNKNOWN_STR;
+        }
     }
 
     cnt = textcat_ClassifyFull(h, buffer, size, h->tmp_candidates);
@@ -323,6 +349,10 @@ extern int textcat_ClassifyFull(void *handle, const char 
*buffer, size_t size,
     void *unknown;
 
     unknown = fp_Init(NULL);
+    if (unknown == NULL)
+    {
+        return TEXTCAT_RESULT_UNKNOWN;
+    }
     fp_SetProperty(unknown, TCPROP_UTF8AWARE, h->utfaware);
     fp_SetProperty(unknown, TCPROP_MINIMUM_DOCUMENT_SIZE, h->mindocsize);
     if (fp_Create(unknown, buffer, size, MAXNGRAMS) == 0)
commit 32a5b21bae9668078eaf1c058bd105a06aa68a78
Author:     Caolán McNamara <[email protected]>
AuthorDate: Sat Nov 7 23:45:50 2026 +0000
Commit:     Caolán McNamara <[email protected]>
CommitDate: Sun Nov 8 01:08:26 2026 +0100

    cid#915899 try and help coverity out wrt Resource leak
    
    and
    
    cid#915900 try and help coverity out wrt Resource leak
    
    count each fingerprint as soon as it is created, so textcat_Done frees
    a half-loaded one too and the error paths need no special cleanup
    
    Change-Id: I45799f18c2f5536bb7a3ba0caa4a100ff8f2698e
    Reviewed-on: https://gerrit.libreoffice.org/c/libexttextcat/+/213825
    Reviewed-by: Caolán McNamara <[email protected]>
    Tested-by: Caolán McNamara <[email protected]>

diff --git a/src/textcat.c b/src/textcat.c
index 9b41b68..98ab644 100644
--- a/src/textcat.c
+++ b/src/textcat.c
@@ -184,6 +184,7 @@ extern void *special_textcat_Init(const char *conffile, 
const char *prefix)
     {
         char *p;
         char *segment[4];
+        void *fprint;
 
         /*** Skip comments ***/
         if ((p = strchr(line, '#')))
@@ -208,10 +209,15 @@ extern void *special_textcat_Init(const char *conffile, 
const char *prefix)
         }
 
         /*** Load data ***/
-        if ((h->fprint[h->size] = fp_Init(segment[1])) == NULL)
+        if ((fprint = fp_Init(segment[1])) == NULL)
         {
             goto BAILOUT;
         }
+        h->fprint[h->size] = fprint;
+        h->fprint_disable[h->size] = 0xF0;  /* 0xF0 is the code for enabled
+                                               languages, 0x0F is for disabled
+                                             */
+        h->size++;
 
         while (prefix_size + strlen(segment[0]) > finger_print_file_name_size)
         {
@@ -222,7 +228,6 @@ extern void *special_textcat_Init(const char *conffile, 
const char *prefix)
                                 sizeof(char) * (tmp_size + 1));
             if (tmp == NULL)
             {
-                fp_Done(h->fprint[h->size]);
                 goto BAILOUT;
             }
             else
@@ -234,15 +239,10 @@ extern void *special_textcat_Init(const char *conffile, 
const char *prefix)
         finger_print_file_name[prefix_size] = '
         strcat(finger_print_file_name, segment[0]);
 
-        if (fp_Read(h->fprint[h->size], finger_print_file_name, 400) == 0)
+        if (fp_Read(fprint, finger_print_file_name, 400) == 0)
         {
-            fp_Done(h->fprint[h->size]);
             goto BAILOUT;
         }
-        h->fprint_disable[h->size] = 0xF0;  /* 0xF0 is the code for enabled
-                                               languages, 0x0F is for disabled 
-                                             */
-        h->size++;
     }
 
     free(finger_print_file_name);

Reply via email to