Improve the MPD time parser
- reject negative values, which strtoul() happily accepts - deal with an arbitrary number of decimal digits - don't return milliseconds when we fail to parse seconds
This commit is contained in:
parent
3934d9b1f9
commit
c0119027b1
27
nncmpp.c
27
nncmpp.c
|
@ -3358,18 +3358,27 @@ mpd_read_time (const char *value, int *sec, int *optional_msec)
|
||||||
if (!value)
|
if (!value)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
char *end, *period = strchr (value, '.');
|
char *end = NULL;
|
||||||
if (optional_msec && period)
|
long n = strtol (value, &end, 10);
|
||||||
|
if (n < 0 || (*end && *end != '.'))
|
||||||
|
return;
|
||||||
|
|
||||||
|
int msec = 0;
|
||||||
|
if (*end == '.')
|
||||||
{
|
{
|
||||||
unsigned long n = strtoul (period + 1, &end, 10);
|
// In practice, MPD always uses three decimal digits
|
||||||
if (*end)
|
size_t digits = strspn (++end, "0123456789");
|
||||||
|
if (end[digits])
|
||||||
return;
|
return;
|
||||||
// XXX: this relies on three decimal places
|
|
||||||
*optional_msec = MIN (INT_MAX, n);
|
if (digits--) msec += (*end++ - '0') * 100;
|
||||||
|
if (digits--) msec += (*end++ - '0') * 10;
|
||||||
|
if (digits--) msec += *end++ - '0';
|
||||||
}
|
}
|
||||||
unsigned long n = strtoul (value, &end, 10);
|
|
||||||
if (end == period || !*end)
|
*sec = MIN (INT_MAX, n);
|
||||||
*sec = MIN (INT_MAX, n);
|
if (optional_msec)
|
||||||
|
*optional_msec = msec;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|
Loading…
Reference in New Issue