From 3f07ac1d9525bac6e197cd99ac45f174fef55edb Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Fri, 28 Jan 2022 03:49:28 +0000 Subject: [PATCH] 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. --- src/lib/libast/port/lcgen.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/libast/port/lcgen.c b/src/lib/libast/port/lcgen.c index 61b746e07..095ceb69b 100644 --- a/src/lib/libast/port/lcgen.c +++ b/src/lib/libast/port/lcgen.c @@ -128,7 +128,7 @@ static struct State_s #define MAP 4 #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* enter(register Table_t* tab, register Link_t* v)