Commit 4a88bd615b for frr
commit 4a88bd615bbaa2720f94ce827e0cdf9f12f6d0b7
Author: Dave Seddon <dave.seddon.ca@gmail.com>
Date: Sun Nov 22 14:46:36 2026 -0800
lib: avoid zero-length VLA in ttable_dump()
A table with no rows has ncols == 0, either because no row was ever
added or because ttable_del_row() removed the last one. ttable_dump()
then declared int cw[0], which is undefined behaviour. test_ttable
already dumps a table after deleting its last row, and fails under
UBSan:
lib/termtable.c:299:9: runtime error: variable length array bound
evaluates to non-positive value 0
Give the array a bound of at least 1. cw[] is not read when ncols is 0,
so the output is unchanged. Also dump a table that never had a row.
Signed-off-by: Dave Seddon <dave.seddon.ca@gmail.com>
diff --git a/lib/termtable.c b/lib/termtable.c
index ce19701389..4464876b2a 100644
--- a/lib/termtable.c
+++ b/lib/termtable.c
@@ -296,7 +296,7 @@ char *ttable_dump(struct ttable *tt, const char *newline)
char *buf; // print buffer
size_t pos; // position in buffer
size_t nl_len; // strlen(newline)
- int cw[tt->ncols]; // calculated column widths
+ int cw[MAX(tt->ncols, 1)]; // calculated column widths
int nlines; // total number of newlines / table lines
size_t width; // length of one line, with newline
int abspad; // calculated whitespace for sprintf
diff --git a/tests/lib/test_ttable.c b/tests/lib/test_ttable.c
index 7ac0e3516b..0b8f9e0971 100644
--- a/tests/lib/test_ttable.c
+++ b/tests/lib/test_ttable.c
@@ -166,4 +166,12 @@ int main(int argc, char **argv)
/* delete table */
ttable_del(tt);
+
+ /* dump a table that never had a row */
+ tt = ttable_new(&ttable_styles[TTSTYLE_ASCII]);
+ assert(tt->ncols == 0);
+ table = ttable_dump(tt, "\n");
+ fprintf(stdout, "%s\n", table);
+ XFREE(MTYPE_TMP_TTABLE, table);
+ ttable_del(tt);
}
diff --git a/tests/lib/test_ttable.refout b/tests/lib/test_ttable.refout
index fb59c0f3e1..45229277dd 100644
--- a/tests/lib/test_ttable.refout
+++ b/tests/lib/test_ttable.refout
@@ -141,3 +141,6 @@
| nebula dusk session streets twilight pioneer beats yeah | prarie dog cornmeal :O -*_-* |
|------------------------------------------------------------------------------------------------|
+ ||
+ ||
+