1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

libast/port/lcgen.c: fix bad output: initialise allocated memory

On my machine, the build system has been intermittently rebuilding
a sizeable part of libast for no apparent reason. I think I've
finally tracked down the cause: occasionally, the lctab.c file,
generated by port/lcgen.c, randomly changes, triggering said
recompile.

Diff of the latest instance on my system:

--- lctab.c.save	2022-01-28 03:22:47.000000000 +0000
+++ arch/darwin.i386-64/src/lib/libast/lctab.c	2022-01-28 03:30:01.000000000 +0000
@@ -2146,7 +2146,7 @@
 #endif
 0,0,0,
 },
-{"no","norway",LC_primary,
+{"no","norway",0,
 #ifdef CTRY_NORWAY
 CTRY_NORWAY,
 #else

In the port/lc.tab input file, "norway" does *not* have the
"primary" tag, unlike e.g. "sweden" or "united-kingdom". So that
LC_primary value did not belong in the generated code.

A look at the port/lcgen.c code shows that it's using uninitialised
memory. The newof() macro uses malloc, which does not initialise
the memory blocks it allocates:

131:#define newof(p,t,n,x)	((t*)malloc(sizeof(t)*(n)+(x)))

This is then used as follows:

403:	case TERRITORY:
404:		if (!(tp = newof(0, Territory_t, 1, s - b + 1)))
[...]
444:					if (!strcmp(b, "primary"))
445:						tp->primary = 1;

The flag is not set to zero if the string does not match, so it's
left uninitialised. Perhaps there are more such problems, but
instead of spending time trying to find them, I'll fix newof().

src/lib/libast/port/lcgen.c:
- In the newof() macro, call calloc(3) instead of malloc(3),
  ensuring that all allocated memory is initialised to zero.
This commit is contained in:
Martijn Dekker 2022-01-28 03:49:28 +00:00
parent 9f199a0290
commit 3f07ac1d95

View file

@ -128,7 +128,7 @@ static struct State_s
#define MAP 4 #define MAP 4
#define elementsof(x) (sizeof(x)/sizeof(x[0])) #define elementsof(x) (sizeof(x)/sizeof(x[0]))
#define newof(p,t,n,x) ((t*)malloc(sizeof(t)*(n)+(x))) #define newof(p,t,n,x) ((t*)calloc(1,sizeof(t)*(n)+(x)))
static Link_t* static Link_t*
enter(register Table_t* tab, register Link_t* v) enter(register Table_t* tab, register Link_t* v)