Merge pull request #321 from ennorehling/feature/fixme-base36-static

eliminate static variable leak and allocation from base36
This commit is contained in:
Enno Rehling 2015-10-27 13:30:12 +01:00
commit 185a2ee29c

View file

@ -54,22 +54,14 @@ int atoi36(const char *str)
const char *itoab(int i, int base) const char *itoab(int i, int base)
{ {
const int maxlen = 20; static char sstr[80];
static char **as = NULL; // FIXME: static return value
char *s, *dst; char *s, *dst;
static int index = 0; /* STATIC_XCALL: used across calls */ static int index = 0; /* STATIC_XCALL: used across calls */
int neg = 0; int neg = 0;
if (!as) { s = sstr + (index * 20);
int j;
char *x = (char *)calloc(sizeof(char), maxlen * 4); /* STATIC_LEAK: malloc in static variable */
as = (char **)calloc(sizeof(char *), 4);
for (j = 0; j != 4; ++j)
as[j] = x + j * maxlen;
}
s = as[index];
index = (index + 1) & 3; /* quick for % 4 */ index = (index + 1) & 3; /* quick for % 4 */
dst = s + maxlen - 1; dst = s + 19;
(*dst--) = 0; (*dst--) = 0;
if (i != 0) { if (i != 0) {
if (i < 0) { if (i < 0) {