mirror of
https://github.com/DarkPlacesEngine/gmqcc.git
synced 2025-01-18 22:31:36 +00:00
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:
parent
2ddb49f546
commit
8f34e9fa37
3 changed files with 46 additions and 10 deletions
20
ftepp.c
20
ftepp.c
|
@ -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;
|
||||
|
|
4
gmqcc.h
4
gmqcc.h
|
@ -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
32
util.c
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue