added util_vasprintf/util_asprintf .. so we can stop assuming a certian static array size for formatting strings ... I expect to see full migration to this before the next release for protection of over/under flows. As well as identifers >= 1024 bytes :)

This commit is contained in:
Dale Weiler 2012-12-28 15:02:53 +00:00
parent 2ddb49f546
commit 8f34e9fa37
3 changed files with 46 additions and 10 deletions

20
ftepp.c
View file

@ -82,8 +82,8 @@ static uint32_t ftepp_predef_randval = 0;
/* __LINE__ */
char *ftepp_predef_line(lex_file *context) {
char *value = (char*)mem_a(128);
sprintf(value, "%d", (int)context->line);
char *value;
util_asprintf(&value, "%d", (int)context->line);
return value;
}
/* __FILE__ */
@ -97,34 +97,34 @@ char *ftepp_predef_file(lex_file *context) {
}
/* __COUNTER_LAST__ */
char *ftepp_predef_counterlast(lex_file *context) {
char *value = (char*)mem_a(128);
sprintf(value, "%u", ftepp_predef_countval);
char *value;
util_asprintf(&value, "%u", ftepp_predef_countval);
(void)context;
return value;
}
/* __COUNTER__ */
char *ftepp_predef_counter(lex_file *context) {
char *value = (char*)mem_a(128);
char *value;
ftepp_predef_countval ++;
sprintf(value, "%u", ftepp_predef_countval);
util_asprintf(&value, "%u", ftepp_predef_countval);
(void)context;
return value;
}
/* __RANDOM__ */
char *ftepp_predef_random(lex_file *context) {
char *value = (char*)mem_a(128);
char *value;
ftepp_predef_randval = (util_rand() % 0xFF) + 1;
sprintf(value, "%u", ftepp_predef_randval);
util_asprintf(&value, "%u", ftepp_predef_randval);
(void)context;
return value;
}
/* __RANDOM_LAST__ */
char *ftepp_predef_randomlast(lex_file *context) {
char *value = (char*)mem_a(128);
sprintf(value, "%u", ftepp_predef_randval);
char *value;
util_asprintf(&value, "%u", ftepp_predef_randval);
(void)context;
return value;

View file

@ -261,6 +261,10 @@ uint16_t util_crc16(uint16_t crc, const char *data, size_t len);
void util_seed(uint32_t);
uint32_t util_rand();
int util_vasprintf(char **ret, const char *fmt, va_list);
int util_asprintf (char **ret, const char *fmt, ...);
#ifdef NOTRACK
# define mem_a(x) malloc (x)
# define mem_d(x) free ((void*)x)

32
util.c
View file

@ -573,6 +573,38 @@ void util_htdel(hash_table_t *ht) {
mem_d(ht);
}
/*
* Portable implementation of vasprintf/asprintf. Assumes vsnprintf
* exists, otherwise compiler error.
*/
int util_vasprintf(char **ret, const char *fmt, va_list args) {
int read;
va_list copy;
va_copy(copy, args);
*ret = 0;
if ((read = vsnprintf(NULL, 0, fmt, args)) >= 0) {
char *buffer;
if ((buffer = (char*)mem_a(read + 1))) {
if ((read = vsnprintf(buffer, read + 1, fmt, copy)) < 0)
mem_d(buffer);
else
*ret = buffer;
}
}
va_end(copy);
return read;
}
int util_asprintf(char **ret, const char *fmt, ...) {
va_list args;
int read;
va_start(args, fmt);
read = util_vasprintf(ret, fmt, args);
va_end (args);
return read;
}
/*
* Implementation of the Mersenne twister PRNG (pseudo random numer
* generator). Implementation of MT19937. Has a period of 2^19937-1