Improve memory allocation following pattern in mdb_col_to_string.

This commit is contained in:
Andy Reagan 2021-12-06 09:44:30 -05:00
parent 57cd54f654
commit 88caaa2004

View File

@ -937,16 +937,20 @@ static char *
mdb_date_to_string(MdbHandle *mdb, const char *fmt, void *buf, int start) mdb_date_to_string(MdbHandle *mdb, const char *fmt, void *buf, int start)
{ {
struct tm t = { 0 }; struct tm t = { 0 };
char *text = g_malloc(mdb->bind_size); char *text = NULL;
double td = mdb_get_double(buf, start); double td = mdb_get_double(buf, start);
// limit to ~1100AD--2700A to protect from overflow // limit dates to protect from overflow.
if (td < -1e6 || td > 1e6) // 1e6 days is ~2700 years, centered at 1900, so this range is:
return strdup(""); // Sunday, February 4, 839 BC to Tuesday, November 28, 4637
if (td < -1e6 || td > 1e6) {
mdb_date_to_tm(td, &t); // an "invalid date" would perhaps be better than null (TODO)
text = g_strdup("");
strftime(text, mdb->bind_size, mdb->date_fmt, &t); } else {
text = g_malloc(mdb->bind_size);
mdb_date_to_tm(td, &t);
strftime(text, mdb->bind_size, mdb->date_fmt, &t);
}
return text; return text;
} }