4646
4747#include "runtime_file.h"
4848
49- #define LOG_FILE_RUNTIME_FORMAT_STR "%u:%02u:%02u"
50- #define LOG_FILE_LAST_PLAYED_FORMAT_STR "%04u-%02u-%02u %02u:%02u:%02u"
51-
5249/* JSON Stuff... */
5350
5451typedef struct
@@ -184,52 +181,108 @@ static void runtime_log_read_file(runtime_log_t *runtime_log)
184181 /* Runtime */
185182 if (context .runtime_string )
186183 {
187- if (sscanf (context .runtime_string ,
188- LOG_FILE_RUNTIME_FORMAT_STR ,
189- & runtime_hours ,
190- & runtime_minutes ,
191- & runtime_seconds ) != 3 )
184+ const char * str = context .runtime_string ;
185+ char * end = NULL ;
186+ unsigned long val ;
187+
188+ /* Hours */
189+ val = strtoul (str , & end , 10 );
190+ if (end == str || * end != ':' )
191+ {
192+ RARCH_ERR ("[Runtime] Invalid \"runtime\" entry detected: \"%s\".\n" , runtime_log -> path );
193+ goto end ;
194+ }
195+ runtime_hours = (unsigned )val ;
196+ str = end + 1 ;
197+
198+ /* Minutes */
199+ val = strtoul (str , & end , 10 );
200+ if (end == str || * end != ':' )
201+ {
202+ RARCH_ERR ("[Runtime] Invalid \"runtime\" entry detected: \"%s\".\n" , runtime_log -> path );
203+ goto end ;
204+ }
205+ runtime_minutes = (unsigned )val ;
206+ str = end + 1 ;
207+
208+ /* Seconds */
209+ val = strtoul (str , & end , 10 );
210+ if (end == str || (* end != '\0' && * end != '\n' ))
192211 {
193212 RARCH_ERR ("[Runtime] Invalid \"runtime\" entry detected: \"%s\".\n" , runtime_log -> path );
194213 goto end ;
195214 }
215+ runtime_seconds = (unsigned )val ;
196216 }
197217
198218 /* Last played */
199219 if (context .last_played_string )
200220 {
201- if (sscanf (context .last_played_string ,
202- LOG_FILE_LAST_PLAYED_FORMAT_STR ,
203- & last_played_year ,
204- & last_played_month ,
205- & last_played_day ,
206- & last_played_hour ,
207- & last_played_minute ,
208- & last_played_second ) != 6 )
209- {
210- RARCH_ERR ("[Runtime] Invalid \"last played\" entry detected: \"%s\".\n" , runtime_log -> path );
211- goto end ;
212- }
221+ const char * str = context .last_played_string ;
222+ char * end = NULL ;
223+
224+ last_played_year = (unsigned )strtoul (str , & end , 10 );
225+ if (!end || * end != '-' )
226+ goto invalid ;
227+ str = end + 1 ;
228+
229+ last_played_month = (unsigned )strtoul (str , & end , 10 );
230+ if (!end || * end != '-' )
231+ goto invalid ;
232+ str = end + 1 ;
233+
234+ last_played_day = (unsigned )strtoul (str , & end , 10 );
235+ if (!end || * end != ' ' )
236+ goto invalid ;
237+ str = end + 1 ;
238+
239+ last_played_hour = (unsigned )strtoul (str , & end , 10 );
240+ if (!end || * end != ':' )
241+ goto invalid ;
242+ str = end + 1 ;
243+
244+ last_played_minute = (unsigned )strtoul (str , & end , 10 );
245+ if (!end || * end != ':' )
246+ goto invalid ;
247+ str = end + 1 ;
248+
249+ last_played_second = (unsigned )strtoul (str , & end , 10 );
250+ if (!end || (* end != '\0' && * end != ' ' ))
251+ goto invalid ;
252+
253+ goto parsed ;
254+
255+ invalid :
256+ RARCH_ERR ("[Runtime] Invalid \"last played\" entry detected: \"%s\".\n" , runtime_log -> path );
257+ goto end ;
258+
259+ parsed :
260+ ; /* continue normal flow */
213261 }
214262
215263 /* Play count */
216264 if (context .play_count )
217265 {
218- if (sscanf (context .play_count , "%u" , & play_count ) != 1 )
266+ char * endptr = NULL ;
267+ unsigned long val = strtoul (context .play_count , & endptr , 10 );
268+ if (* endptr != '\0' || errno == ERANGE )
219269 {
220270 RARCH_ERR ("[Runtime] Invalid \"play count\" entry detected: \"%s\".\n" , runtime_log -> path );
221271 goto end ;
222272 }
273+ play_count = (unsigned )val ;
223274 }
224-
225275 /* State slot */
226276 if (context .state_slot )
227277 {
228- if (sscanf (context .state_slot , "%u" , & state_slot ) != 1 )
278+ char * endptr = NULL ;
279+ unsigned long val = strtoul (context .state_slot , & endptr , 10 );
280+ if (* endptr != '\0' || errno == ERANGE )
229281 {
230282 RARCH_ERR ("[Runtime] Invalid \"state slot\" entry detected: \"%s\".\n" , runtime_log -> path );
231283 goto end ;
232284 }
285+ state_slot = (unsigned )val ;
233286 }
234287
235288 if ( state_slot > 0
@@ -1168,7 +1221,7 @@ void runtime_log_save(runtime_log_t *runtime_log)
11681221 /* > Runtime entry */
11691222 snprintf (value_string ,
11701223 sizeof (value_string ),
1171- LOG_FILE_RUNTIME_FORMAT_STR ,
1224+ "%u:%02u:%02u" ,
11721225 runtime_log -> runtime .hours , runtime_log -> runtime .minutes ,
11731226 runtime_log -> runtime .seconds );
11741227
@@ -1183,7 +1236,7 @@ void runtime_log_save(runtime_log_t *runtime_log)
11831236 /* > Last played entry */
11841237 value_string [0 ] = '\0' ;
11851238 snprintf (value_string , sizeof (value_string ),
1186- LOG_FILE_LAST_PLAYED_FORMAT_STR ,
1239+ "%04u-%02u-%02u %02u:%02u:%02u" ,
11871240 runtime_log -> last_played .year , runtime_log -> last_played .month ,
11881241 runtime_log -> last_played .day ,
11891242 runtime_log -> last_played .hour , runtime_log -> last_played .minute ,
0 commit comments