command: check over/underflows in check_int()

The "long" result of strtol() was implicitly casted down to a 32 bit
integer.  Add some range checking instead.
This commit is contained in:
Max Kellermann 2008-10-23 09:54:10 +02:00
parent 95ae1d9e9e
commit e172874cc6

View File

@ -129,12 +129,13 @@ check_uint32(struct client *client, uint32_t *dst,
}
static bool mpd_fprintf__
check_int(struct client *client, int *dst,
check_int(struct client *client, int *value_r,
const char *s, const char *fmt, ...)
{
char *test;
long value;
*dst = strtol(s, &test, 10);
value = strtol(s, &test, 10);
if (*test != '\0') {
va_list args;
va_start(args, fmt);
@ -142,6 +143,16 @@ check_int(struct client *client, int *dst,
va_end(args);
return false;
}
#if LONG_MAX > INT_MAX
if (value < INT_MIN || value > INT_MAX) {
command_error(client, ACK_ERROR_ARG,
"Number too large: %s", s);
return false;
}
#endif
*value_r = (int)value;
return true;
}