playlist: pass unsigned integers to playlistInfo()

A song index cannot be negative.  Also require the second parameter to
be valid.
This commit is contained in:
Max Kellermann 2009-01-10 17:39:49 +01:00
parent b7c4b78846
commit 8ebe7bfb25
3 changed files with 42 additions and 25 deletions

View File

@ -162,7 +162,7 @@ check_int(struct client *client, int *value_r,
}
static bool G_GNUC_PRINTF(5, 6)
check_range(struct client *client, int *value_r1, int *value_r2,
check_range(struct client *client, unsigned *value_r1, unsigned *value_r2,
const char *s, const char *fmt, ...)
{
char *test, *test2;
@ -177,15 +177,21 @@ check_range(struct client *client, int *value_r1, int *value_r2,
return false;
}
#if LONG_MAX > INT_MAX
if (value < INT_MIN || value > INT_MAX) {
if (value < 0) {
command_error(client, ACK_ERROR_ARG,
"Number is negative: %s", s);
return false;
}
#if LONG_MAX > UINT_MAX
if (value > UINT_MAX) {
command_error(client, ACK_ERROR_ARG,
"Number too large: %s", s);
return false;
}
#endif
*value_r1 = (int)value;
*value_r1 = (unsigned)value;
if (*test == ':') {
value = strtol(++test, &test2, 10);
@ -196,14 +202,23 @@ check_range(struct client *client, int *value_r1, int *value_r2,
va_end(args);
return false;
}
#if LONG_MAX > INT_MAX
if (value < INT_MIN || value > INT_MAX) {
if (value < 0) {
command_error(client, ACK_ERROR_ARG,
"Number is negative: %s", s);
return false;
}
#if LONG_MAX > UINT_MAX
if (value > UINT_MAX) {
command_error(client, ACK_ERROR_ARG,
"Number too large: %s", s);
return false;
}
#endif
*value_r2 = (int)value;
*value_r2 = (unsigned)value;
} else {
*value_r2 = (unsigned)value + 1;
}
return true;
@ -743,13 +758,14 @@ handle_plchangesposid(struct client *client, G_GNUC_UNUSED int argc, char *argv[
static enum command_return
handle_playlistinfo(struct client *client, int argc, char *argv[])
{
int song = -1, max = -1;
unsigned start = 0, end = UINT_MAX;
enum playlist_result result;
if (argc == 2 && !check_range(client, &song, &max, argv[1], need_range))
if (argc == 2 && !check_range(client, &start, &end,
argv[1], need_range))
return COMMAND_RETURN_ERROR;
result = playlistInfo(client, song, max);
result = playlistInfo(client, start, end);
return print_playlist_result(client, result);
}

View File

@ -413,24 +413,16 @@ int playlistChangesPosId(struct client *client, uint32_t version)
return 0;
}
enum playlist_result playlistInfo(struct client *client, int song, int max)
enum playlist_result
playlistInfo(struct client *client, unsigned start, unsigned end)
{
unsigned begin = 0;
unsigned end = playlist.length;
if (end > playlist.length)
end = playlist.length;
if (song >= 0) {
begin = song;
end = song + 1;
}
if (song >= (int)playlist.length)
if (start > end)
return PLAYLIST_RESULT_BAD_RANGE;
if (max > 0) {
end = MIN((unsigned)max, playlist.length);
if (end <= begin)
return PLAYLIST_RESULT_BAD_RANGE;
}
for (unsigned i = begin; i < end; i++)
for (unsigned i = start; i < end; i++)
printPlaylistSongInfo(client, i);
return PLAYLIST_RESULT_SUCCESS;

View File

@ -95,7 +95,16 @@ enum playlist_result deleteFromPlaylist(unsigned song);
enum playlist_result deleteFromPlaylistById(unsigned song);
enum playlist_result playlistInfo(struct client *client, int song, int max);
/**
* Send detailed information about a range of songs in the playlist to
* a client.
*
* @param client the client which has requested information
* @param start the index of the first song (including)
* @param end the index of the last song (excluding)
*/
enum playlist_result
playlistInfo(struct client *client, unsigned start, unsigned end);
enum playlist_result playlistId(struct client *client, int song);