From eaaf52fc683b44c95551ad6bf932aef93f5847b0 Mon Sep 17 00:00:00 2001 From: Ger Hobbelt Date: Mon, 21 Aug 2023 13:07:06 +0200 Subject: [PATCH 1/2] numaGetCount(): improved checking for the fringe case where the NUMA histogram is empty (count = 0) (Discovered through code review.) --- src/colorquant1.c | 3 ++- src/numabasic.c | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/colorquant1.c b/src/colorquant1.c index 8ec2b247c..106c17f02 100644 --- a/src/colorquant1.c +++ b/src/colorquant1.c @@ -3110,7 +3110,8 @@ PIXCMAP *cmap; makeRGBToIndexTables(level, &rtab, >ab, &btab); /* The octarray will give a ptr from the octcube to the colorarray */ - ncubes = numaGetCount(na); + if ((ncubes = numaGetCount(na)) == 0) + return ERROR_PTR("no slots in pixel occupation histogram", __func__, NULL); octarray = (l_int32 *)LEPT_CALLOC(ncubes, sizeof(l_int32)); /* The colorarray will hold the colors of the first pixel diff --git a/src/numabasic.c b/src/numabasic.c index 11ac541e8..d09919c32 100644 --- a/src/numabasic.c +++ b/src/numabasic.c @@ -812,7 +812,8 @@ l_int32 *array; if (!na) return (l_int32 *)ERROR_PTR("na not defined", __func__, NULL); - n = numaGetCount(na); + if ((n = numaGetCount(na)) == 0) + return ERROR_PTR("array not made (na is empty)", __func__, NULL); if ((array = (l_int32 *)LEPT_CALLOC(n, sizeof(l_int32))) == NULL) return (l_int32 *)ERROR_PTR("array not made", __func__, NULL); for (i = 0; i < n; i++) { @@ -859,7 +860,8 @@ l_float32 *array; if (copyflag == L_NOCOPY) { array = na->array; } else { /* copyflag == L_COPY */ - n = numaGetCount(na); + if ((n = numaGetCount(na)) == 0) + return ERROR_PTR("array not made (na is empty)", __func__, NULL); if ((array = (l_float32 *)LEPT_CALLOC(n, sizeof(l_float32))) == NULL) return (l_float32 *)ERROR_PTR("array not made", __func__, NULL); for (i = 0; i < n; i++) From 9d7ecbe56ecba86973c0c0bb6a1b4a442d3b7118 Mon Sep 17 00:00:00 2001 From: Ger Hobbelt Date: Mon, 21 Aug 2023 13:09:04 +0200 Subject: [PATCH 2/2] numaGetCount(): make sure surrounding code/variables are properly pre-initialized to cope with the fringe case where the NUMA histogram is empty (count = 0) (Discovered through code review & accidental misuse in vitro (when I fed numaHistogramGetValFromRank() an empty histogram by accident).) --- src/graphics.c | 1 + src/numafunc2.c | 1 + 2 files changed, 2 insertions(+) diff --git a/src/graphics.c b/src/graphics.c index d8b5781b1..ccb84e210 100644 --- a/src/graphics.c +++ b/src/graphics.c @@ -1152,6 +1152,7 @@ PTA *pta1, *pta2, *ptad; /* Generate the plot points */ pta1 = ptaCreate(n); + maxw = maxh = 0; for (i = 0; i < n; i++) { numaGetFValue(na, i, &val); if (orient == L_HORIZONTAL_LINE) { diff --git a/src/numafunc2.c b/src/numafunc2.c index 0c0d6dd1d..7f897af95 100644 --- a/src/numafunc2.c +++ b/src/numafunc2.c @@ -1613,6 +1613,7 @@ l_float32 startval, binsize, rankcount, total, sum, fract, val; numaGetSum(na, &total); rankcount = rank * total; /* count that corresponds to rank */ sum = 0.0; + val = 0.0; for (i = 0; i < n; i++) { numaGetFValue(na, i, &val); if (sum + val >= rankcount)