Skip to content

Commit a158a37

Browse files
committed
Replace sscanf
1 parent e89866a commit a158a37

2 files changed

Lines changed: 109 additions & 33 deletions

File tree

runtime_file.c

Lines changed: 78 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@
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

5451
typedef 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,

tasks/task_database_cue.c

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,31 +1163,54 @@ int cue_find_track(const char *cue_path, bool first,
11631163
task_database_cue_get_token(fd, tmp_token, sizeof(tmp_token));
11641164
task_database_cue_get_token(fd, tmp_token, sizeof(tmp_token));
11651165

1166-
if (sscanf(tmp_token, "%02d:%02d:%02d", &_m, &_s, &_f) < 3)
11671166
{
1167+
const char *ptr = tmp_token;
1168+
char *end = NULL;
1169+
1170+
_m = (int)strtol(ptr, &end, 10);
1171+
if (!end || *end != ':')
1172+
{
11681173
#ifdef DEBUG
1169-
RARCH_LOG("[Scanner] Error parsing time stamp \"%s\".\n", tmp_token);
1174+
RARCH_LOG("[Scanner] Error parsing time stamp \"%s\".\n", tmp_token);
11701175
#endif
1171-
goto error;
1176+
goto error;
1177+
}
1178+
1179+
ptr = end + 1;
1180+
_s = (int)strtol(ptr, &end, 10);
1181+
if (!end || *end != ':')
1182+
{
1183+
#ifdef DEBUG
1184+
RARCH_LOG("[Scanner] Error parsing time stamp \"%s\".\n", tmp_token);
1185+
#endif
1186+
goto error;
1187+
}
1188+
1189+
ptr = end + 1;
1190+
_f = (int)strtol(ptr, &end, 10);
1191+
if (!end || (*end != '\0' && *end != ' ' && *end != '\t'))
1192+
{
1193+
#ifdef DEBUG
1194+
RARCH_LOG("[Scanner] Error parsing time stamp \"%s\".\n", tmp_token);
1195+
#endif
1196+
goto error;
1197+
}
11721198
}
11731199

11741200
last_index = (size_t)(((_m * 60 + _s) * 75) + _f) * 2352;
1175-
11761201
/* If we've changed tracks since the candidate, update it */
11771202
if ( (cand_track != -1)
11781203
&& (track != cand_track)
11791204
&& update_cand(&cand_index, &last_index, &largest,
1180-
last_file, offset,
1181-
size, s, len))
1205+
last_file, offset,
1206+
size, s, len))
11821207
{
11831208
rv = 0;
11841209
if (first)
11851210
goto clean;
11861211
}
1187-
11881212
if (!is_data)
11891213
continue;
1190-
11911214
if (cand_index == -1)
11921215
{
11931216
cand_index = last_index;

0 commit comments

Comments
 (0)