Skip to content

Commit c580c36

Browse files
authored
Fix date_format crash on Windows (#279)
* Validate date_format on windows On windows, strftime causes a crash if the format is invalid, so we have to do a manual check * Allow #, E and O modifiers for date format
1 parent cfdb437 commit c580c36

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

libs/std/date.c

+35
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ static value date_new( value s ) {
119119
return alloc_int32(o);
120120
}
121121

122+
#ifdef NEKO_WINDOWS
123+
static char VALID_FORMAT_CODES[] = "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%";
124+
#endif
125+
122126
/**
123127
date_format : #int32 -> fmt:string? -> string
124128
<doc>Format a date using [strftime]. If [fmt] is [null] then default format is used</doc>
@@ -134,6 +138,37 @@ static value date_format( value o, value fmt ) {
134138
d = val_any_int(o);
135139
if( localtime_r(&d,&t) == NULL )
136140
neko_error();
141+
#ifdef NEKO_WINDOWS
142+
int len = val_strlen(fmt);
143+
const char* str = val_string(fmt);
144+
int i = 0;
145+
while (i < len) {
146+
if (str[i] != '%') {
147+
i++;
148+
continue;
149+
}
150+
i++;
151+
if (str[i] == '#') {
152+
i++;
153+
}
154+
if (str[i] == 'E' || str[i] == 'O') {
155+
i++;
156+
}
157+
bool is_valid = false;
158+
const char* format_code = VALID_FORMAT_CODES;
159+
while (*format_code) {
160+
if (*format_code == str[i]) {
161+
is_valid = true;
162+
break;
163+
}
164+
format_code++;
165+
}
166+
if (!is_valid) {
167+
neko_error();
168+
}
169+
i++;
170+
}
171+
#endif
137172
if( strftime(buf,127,val_string(fmt),&t) == 0 )
138173
neko_error();
139174
return alloc_string(buf);

0 commit comments

Comments
 (0)