Fix date/time handling in idParser::ExpandBuiltinDefine()

this was so broken, I think if anyone ever tried to use __DATE__ or
__TIME__ it must've crashed, either from free(curtime) (which was the
return value of ctime()) or from things like token[7] = NULL when token
was a pointer (to a single element!).

I think it could work now.

Also fixed potential memory leaks in cases that don't "return" anything
This commit is contained in:
Daniel Gibson 2023-01-05 09:05:52 +01:00
parent 3d15ce367b
commit 9cb1cef572

View file

@ -669,6 +669,31 @@ define_t *idParser::CopyFirstDefine( void ) {
return NULL; return NULL;
} }
// simple abstraction around localtime()/localtime_r() or whatever
static bool my_localtime(const time_t* t, struct tm* ts)
{
// TODO: does any non-windows platform not support localtime_r()?
// then add them here (or handle them specially with their own #elif)
#ifdef _WIN32
// localtime() is C89 so it should be available everywhere, but it *may* not be threadsafe.
// It *is* threadsafe on Windows though (there it returns a thread-local variable).
// (yes, Windows has localtime_s(), but it's unclear if MinGW supports that
// or localtime_r() and in the end, *on Windows* localtime() is safe so use it)
struct tm* tmp = localtime(t);
if(tmp != NULL) {
*ts = *tmp;
return true;
}
#else // localtime_r() assumed available, use it (all Unix-likes incl. macOS support it, AROS as well)
if(localtime_r(t, ts) != NULL)
return true;
#endif
// if we get here, the localtime() (or localtime_r() or whatever) call failed
memset(ts, 0, sizeof(*ts)); // make sure ts has deterministic content
return false;
}
/* /*
================ ================
idParser::ExpandBuiltinDefine idParser::ExpandBuiltinDefine
@ -677,7 +702,6 @@ idParser::ExpandBuiltinDefine
int idParser::ExpandBuiltinDefine( idToken *deftoken, define_t *define, idToken **firsttoken, idToken **lasttoken ) { int idParser::ExpandBuiltinDefine( idToken *deftoken, define_t *define, idToken **firsttoken, idToken **lasttoken ) {
idToken *token; idToken *token;
ID_TIME_T t; ID_TIME_T t;
char *curtime;
char buf[MAX_STRING_CHARS]; char buf[MAX_STRING_CHARS];
token = new idToken(deftoken); token = new idToken(deftoken);
@ -709,14 +733,15 @@ int idParser::ExpandBuiltinDefine( idToken *deftoken, define_t *define, idToken
} }
case BUILTIN_DATE: { case BUILTIN_DATE: {
t = time(NULL); t = time(NULL);
curtime = ctime(&t); // DG: apparently this was supposed to extract the date part of "Wed Jun 30 21:49:08 1993\n"
(*token) = "\""; // (like "Jun 30 1993") - it was both ugly and broken before I changed it, though
token->Append( curtime+4 ); // Originally it copied stuff out of ctime(), which is ugly but ok, but also
token[7] = NULL; // free'd the returned value (wrong) and tried to modify token in ways that should've crashed
token->Append( curtime+20 ); // (like token[7] = NULL; which should've been (*token)[7] = '\0';)
token[10] = NULL; struct tm ts;
token->Append( "\"" ); my_localtime(&t, &ts);
free(curtime); strftime(buf, sizeof(buf), "\"%b %d %Y\"", &ts);
(*token) = buf;
token->type = TT_STRING; token->type = TT_STRING;
token->subtype = token->Length(); token->subtype = token->Length();
token->line = deftoken->line; token->line = deftoken->line;
@ -728,12 +753,13 @@ int idParser::ExpandBuiltinDefine( idToken *deftoken, define_t *define, idToken
} }
case BUILTIN_TIME: { case BUILTIN_TIME: {
t = time(NULL); t = time(NULL);
curtime = ctime(&t); // DG: apparently this was supposed to extract the time part of "Wed Jun 30 21:49:08 1993\n"
(*token) = "\""; // (like "21:49:08") - it was both ugly and broken before I changed it, though
token->Append( curtime+11 ); // (had basicaly the same problems as BUILTIN_DATE)
token[8] = NULL; struct tm ts;
token->Append( "\"" ); my_localtime(&t, &ts);
free(curtime); strftime(buf, sizeof(buf), "\"%H:%M:%S\"", &ts);
(*token) = buf;
token->type = TT_STRING; token->type = TT_STRING;
token->subtype = token->Length(); token->subtype = token->Length();
token->line = deftoken->line; token->line = deftoken->line;
@ -745,11 +771,13 @@ int idParser::ExpandBuiltinDefine( idToken *deftoken, define_t *define, idToken
} }
case BUILTIN_STDC: { case BUILTIN_STDC: {
idParser::Warning( "__STDC__ not supported\n" ); idParser::Warning( "__STDC__ not supported\n" );
delete token; // DG: we probably shouldn't leak it, right?
*firsttoken = NULL; *firsttoken = NULL;
*lasttoken = NULL; *lasttoken = NULL;
break; break;
} }
default: { default: {
delete token; // DG: we probably shouldn't leak it, right?
*firsttoken = NULL; *firsttoken = NULL;
*lasttoken = NULL; *lasttoken = NULL;
break; break;