From aa220d5acdbfd8178580e7eb503c205406e2be74 Mon Sep 17 00:00:00 2001 From: Nicolas Pennequin Date: Wed, 2 May 2007 17:51:01 +0000 Subject: Trim a bunch of long lines and fix an infinite loop and buffer overrun in the impossible case that a WPS line ends with a litteral string but without a newline char (wps_parser.c:774). git-svn-id: svn://svn.rockbox.org/rockbox/trunk@13306 a1c6a512-1295-4272-9138-f99709370657 --- apps/gui/gwps-common.c | 17 ++++++----- apps/gui/wps_debug.c | 61 ++++++++++++++++++++++++++-------------- apps/gui/wps_parser.c | 76 +++++++++++++++++++++++++++++--------------------- 3 files changed, 94 insertions(+), 60 deletions(-) (limited to 'apps/gui') diff --git a/apps/gui/gwps-common.c b/apps/gui/gwps-common.c index 14a0d565f4..ddfddc5ec7 100644 --- a/apps/gui/gwps-common.c +++ b/apps/gui/gwps-common.c @@ -769,8 +769,8 @@ static char *get_token_value(struct gui_wps *gwps, #if CONFIG_RTC struct tm* tm = NULL; - /* if the token is an RTC one, update the time and do the necessary checks */ - + /* if the token is an RTC one, update the time + and do the necessary checks */ if (token->type >= WPS_TOKENS_RTC_BEGIN && token->type <= WPS_TOKENS_RTC_END) { @@ -1313,7 +1313,7 @@ static int evaluate_conditional(struct gui_wps *gwps, int cond_index) /* intval is now the number of the enum option we want to read, starting from 1. If intval is -1, we check if value is empty. */ if (intval == -1) - intval = value && strlen(value) ? 1 : num_options; + intval = (value && *value) ? 1 : num_options; else if (intval > num_options || intval < 1) intval = num_options; @@ -1543,12 +1543,14 @@ static bool update_curr_subline(struct gui_wps *gwps, int line) /* if back where we started after search or only one subline is defined on the line */ - if (((search > 0) && (data->lines[line].curr_subline == search_start)) || + if (((search > 0) && + (data->lines[line].curr_subline == search_start)) || only_one_subline) { /* no other subline with a time > 0 exists */ data->lines[line].subline_expire_time = (reset_subline ? - current_tick : data->lines[line].subline_expire_time) + 100 * HZ; + current_tick : + data->lines[line].subline_expire_time) + 100 * HZ; break; } else @@ -1565,7 +1567,7 @@ static bool update_curr_subline(struct gui_wps *gwps, int line) new_subline_refresh = true; data->lines[line].subline_expire_time = (reset_subline ? current_tick : data->lines[line].subline_expire_time) + - BASE_SUBLINE_TIME * data->sublines[subline_idx].time_mult; + BASE_SUBLINE_TIME*data->sublines[subline_idx].time_mult; break; } } @@ -1820,7 +1822,8 @@ bool gui_wps_refresh(struct gui_wps *gwps, /* get current subline for the line */ new_subline_refresh = update_curr_subline(gwps, line); - subline_idx = wps_subline_index(data, line, data->lines[line].curr_subline); + subline_idx = wps_subline_index(data, line, + data->lines[line].curr_subline); flags = data->sublines[subline_idx].line_type; if (refresh_mode == WPS_REFRESH_ALL || (flags & refresh_mode) diff --git a/apps/gui/wps_debug.c b/apps/gui/wps_debug.c index a5f0e2128d..767f296cdd 100644 --- a/apps/gui/wps_debug.c +++ b/apps/gui/wps_debug.c @@ -149,7 +149,8 @@ static void dump_wps_tokens(struct wps_data *data) snprintf(buf, sizeof(buf), "rtc: day of month (01..31)"); break; case WPS_TOKEN_RTC_DAY_OF_MONTH_BLANK_PADDED: - snprintf(buf, sizeof(buf), "rtc: day of month, blank padded ( 1..31)"); + snprintf(buf, sizeof(buf), + "rtc: day of month, blank padded ( 1..31)"); break; case WPS_TOKEN_RTC_HOUR_24_ZERO_PADDED: snprintf(buf, sizeof(buf), "rtc: hour (00..23)"); @@ -173,28 +174,35 @@ static void dump_wps_tokens(struct wps_data *data) snprintf(buf, sizeof(buf), "rtc: second (00..59)"); break; case WPS_TOKEN_RTC_YEAR_2_DIGITS: - snprintf(buf, sizeof(buf), "rtc: last two digits of year (00..99)"); + snprintf(buf, sizeof(buf), + "rtc: last two digits of year (00..99)"); break; case WPS_TOKEN_RTC_YEAR_4_DIGITS: snprintf(buf, sizeof(buf), "rtc: year (1970...)"); break; case WPS_TOKEN_RTC_AM_PM_UPPER: - snprintf(buf, sizeof(buf), "rtc: upper case AM or PM indicator"); + snprintf(buf, sizeof(buf), + "rtc: upper case AM or PM indicator"); break; case WPS_TOKEN_RTC_AM_PM_LOWER: - snprintf(buf, sizeof(buf), "rtc: lower case am or pm indicator"); + snprintf(buf, sizeof(buf), + "rtc: lower case am or pm indicator"); break; case WPS_TOKEN_RTC_WEEKDAY_NAME: - snprintf(buf, sizeof(buf), "rtc: abbreviated weekday name (Sun..Sat)"); + snprintf(buf, sizeof(buf), + "rtc: abbreviated weekday name (Sun..Sat)"); break; case WPS_TOKEN_RTC_MONTH_NAME: - snprintf(buf, sizeof(buf), "rtc: abbreviated month name (Jan..Dec)"); + snprintf(buf, sizeof(buf), + "rtc: abbreviated month name (Jan..Dec)"); break; case WPS_TOKEN_RTC_DAY_OF_WEEK_START_MON: - snprintf(buf, sizeof(buf), "rtc: day of week (1..7); 1 is Monday"); + snprintf(buf, sizeof(buf), + "rtc: day of week (1..7); 1 is Monday"); break; case WPS_TOKEN_RTC_DAY_OF_WEEK_START_SUN: - snprintf(buf, sizeof(buf), "rtc: day of week (0..6); 0 is Sunday"); + snprintf(buf, sizeof(buf), + "rtc: day of week (0..6); 0 is Sunday"); break; #endif @@ -265,31 +273,38 @@ static void dump_wps_tokens(struct wps_data *data) break; case WPS_TOKEN_METADATA_ARTIST: - snprintf(buf, sizeof(buf), "%strack artist", next_str(next)); + snprintf(buf, sizeof(buf), "%strack artist", + next_str(next)); break; case WPS_TOKEN_METADATA_COMPOSER: - snprintf(buf, sizeof(buf), "%strack composer", next_str(next)); + snprintf(buf, sizeof(buf), "%strack composer", + next_str(next)); break; case WPS_TOKEN_METADATA_ALBUM: - snprintf(buf, sizeof(buf), "%strack album", next_str(next)); + snprintf(buf, sizeof(buf), "%strack album", + next_str(next)); break; case WPS_TOKEN_METADATA_GENRE: - snprintf(buf, sizeof(buf), "%strack genre", next_str(next)); + snprintf(buf, sizeof(buf), "%strack genre", + next_str(next)); break; case WPS_TOKEN_METADATA_TRACK_NUMBER: - snprintf(buf, sizeof(buf), "%strack number", next_str(next)); + snprintf(buf, sizeof(buf), "%strack number", + next_str(next)); break; case WPS_TOKEN_METADATA_TRACK_TITLE: - snprintf(buf, sizeof(buf), "%strack title", next_str(next)); + snprintf(buf, sizeof(buf), "%strack title", + next_str(next)); break; case WPS_TOKEN_METADATA_VERSION: - snprintf(buf, sizeof(buf), "%strack ID3 version", next_str(next)); + snprintf(buf, sizeof(buf), "%strack ID3 version", + next_str(next)); break; case WPS_TOKEN_METADATA_YEAR: @@ -325,7 +340,8 @@ static void dump_wps_tokens(struct wps_data *data) break; case WPS_TOKEN_FILE_FREQUENCY: - snprintf(buf, sizeof(buf), "%sfile audio frequency", next_str(next)); + snprintf(buf, sizeof(buf), "%sfile audio frequency", + next_str(next)); break; case WPS_TOKEN_FILE_NAME: @@ -333,7 +349,8 @@ static void dump_wps_tokens(struct wps_data *data) break; case WPS_TOKEN_FILE_NAME_WITH_EXTENSION: - snprintf(buf, sizeof(buf), "%sfile name with extension", next_str(next)); + snprintf(buf, sizeof(buf), "%sfile name with extension", + next_str(next)); break; case WPS_TOKEN_FILE_PATH: @@ -349,8 +366,8 @@ static void dump_wps_tokens(struct wps_data *data) break; case WPS_TOKEN_FILE_DIRECTORY: - snprintf(buf, sizeof(buf), "%sfile directory, level: %d", next_str(next), - token->value.i); + snprintf(buf, sizeof(buf), "%sfile directory, level: %d", + next_str(next), token->value.i); break; default: @@ -421,9 +438,11 @@ static void print_wps_strings(struct wps_data *data) DEBUGF("%2d: (%2d) '%s'\n", i, len, data->strings[i]); } DEBUGF("\n"); - DEBUGF("Number of strings: %d out of an allowed %d\n", data->num_strings, WPS_MAX_STRINGS); + DEBUGF("Number of strings: %d out of an allowed %d\n", + data->num_strings, WPS_MAX_STRINGS); DEBUGF("Total string length: %d\n", total_len); - DEBUGF("String buffer used: %d out of %d bytes\n", buf_used, STRING_BUFFER_SIZE); + DEBUGF("String buffer used: %d out of %d bytes\n", + buf_used, STRING_BUFFER_SIZE); DEBUGF("\n"); } diff --git a/apps/gui/wps_parser.c b/apps/gui/wps_parser.c index ee16de330a..b053cee109 100644 --- a/apps/gui/wps_parser.c +++ b/apps/gui/wps_parser.c @@ -136,23 +136,23 @@ static const struct wps_tag all_tags[] = { #endif #if CONFIG_RTC - { WPS_TOKEN_RTC_DAY_OF_MONTH, "cd", WPS_REFRESH_DYNAMIC, NULL }, - { WPS_TOKEN_RTC_DAY_OF_MONTH_BLANK_PADDED, "ce", WPS_REFRESH_DYNAMIC, NULL }, - { WPS_TOKEN_RTC_HOUR_24_ZERO_PADDED, "cH", WPS_REFRESH_DYNAMIC, NULL }, - { WPS_TOKEN_RTC_HOUR_24, "ck", WPS_REFRESH_DYNAMIC, NULL }, - { WPS_TOKEN_RTC_HOUR_12_ZERO_PADDED, "cI", WPS_REFRESH_DYNAMIC, NULL }, - { WPS_TOKEN_RTC_HOUR_12, "cl", WPS_REFRESH_DYNAMIC, NULL }, - { WPS_TOKEN_RTC_MONTH, "cm", WPS_REFRESH_DYNAMIC, NULL }, - { WPS_TOKEN_RTC_MINUTE, "cM", WPS_REFRESH_DYNAMIC, NULL }, - { WPS_TOKEN_RTC_SECOND, "cS", WPS_REFRESH_DYNAMIC, NULL }, - { WPS_TOKEN_RTC_YEAR_2_DIGITS, "cy", WPS_REFRESH_DYNAMIC, NULL }, - { WPS_TOKEN_RTC_YEAR_4_DIGITS, "cY", WPS_REFRESH_DYNAMIC, NULL }, - { WPS_TOKEN_RTC_AM_PM_UPPER, "cP", WPS_REFRESH_DYNAMIC, NULL }, - { WPS_TOKEN_RTC_AM_PM_LOWER, "cp", WPS_REFRESH_DYNAMIC, NULL }, - { WPS_TOKEN_RTC_WEEKDAY_NAME, "ca", WPS_REFRESH_DYNAMIC, NULL }, - { WPS_TOKEN_RTC_MONTH_NAME, "cb", WPS_REFRESH_DYNAMIC, NULL }, - { WPS_TOKEN_RTC_DAY_OF_WEEK_START_MON, "cu", WPS_REFRESH_DYNAMIC, NULL }, - { WPS_TOKEN_RTC_DAY_OF_WEEK_START_SUN, "cw", WPS_REFRESH_DYNAMIC, NULL }, + { WPS_TOKEN_RTC_DAY_OF_MONTH, "cd", WPS_REFRESH_DYNAMIC, NULL }, + { WPS_TOKEN_RTC_DAY_OF_MONTH_BLANK_PADDED,"ce", WPS_REFRESH_DYNAMIC, NULL }, + { WPS_TOKEN_RTC_HOUR_24_ZERO_PADDED, "cH", WPS_REFRESH_DYNAMIC, NULL }, + { WPS_TOKEN_RTC_HOUR_24, "ck", WPS_REFRESH_DYNAMIC, NULL }, + { WPS_TOKEN_RTC_HOUR_12_ZERO_PADDED, "cI", WPS_REFRESH_DYNAMIC, NULL }, + { WPS_TOKEN_RTC_HOUR_12, "cl", WPS_REFRESH_DYNAMIC, NULL }, + { WPS_TOKEN_RTC_MONTH, "cm", WPS_REFRESH_DYNAMIC, NULL }, + { WPS_TOKEN_RTC_MINUTE, "cM", WPS_REFRESH_DYNAMIC, NULL }, + { WPS_TOKEN_RTC_SECOND, "cS", WPS_REFRESH_DYNAMIC, NULL }, + { WPS_TOKEN_RTC_YEAR_2_DIGITS, "cy", WPS_REFRESH_DYNAMIC, NULL }, + { WPS_TOKEN_RTC_YEAR_4_DIGITS, "cY", WPS_REFRESH_DYNAMIC, NULL }, + { WPS_TOKEN_RTC_AM_PM_UPPER, "cP", WPS_REFRESH_DYNAMIC, NULL }, + { WPS_TOKEN_RTC_AM_PM_LOWER, "cp", WPS_REFRESH_DYNAMIC, NULL }, + { WPS_TOKEN_RTC_WEEKDAY_NAME, "ca", WPS_REFRESH_DYNAMIC, NULL }, + { WPS_TOKEN_RTC_MONTH_NAME, "cb", WPS_REFRESH_DYNAMIC, NULL }, + { WPS_TOKEN_RTC_DAY_OF_WEEK_START_MON, "cu", WPS_REFRESH_DYNAMIC, NULL }, + { WPS_TOKEN_RTC_DAY_OF_WEEK_START_SUN, "cw", WPS_REFRESH_DYNAMIC, NULL }, #endif /* current file */ @@ -164,7 +164,8 @@ static const struct wps_tag all_tags[] = { { WPS_TOKEN_FILE_PATH, "fp", WPS_REFRESH_STATIC, NULL }, { WPS_TOKEN_FILE_SIZE, "fs", WPS_REFRESH_STATIC, NULL }, { WPS_TOKEN_FILE_VBR, "fv", WPS_REFRESH_STATIC, NULL }, - { WPS_TOKEN_FILE_DIRECTORY, "d", WPS_REFRESH_STATIC, parse_dir_level }, + { WPS_TOKEN_FILE_DIRECTORY, "d", WPS_REFRESH_STATIC, + parse_dir_level }, /* next file */ { WPS_TOKEN_FILE_BITRATE, "Fb", WPS_REFRESH_DYNAMIC, NULL }, @@ -175,7 +176,8 @@ static const struct wps_tag all_tags[] = { { WPS_TOKEN_FILE_PATH, "Fp", WPS_REFRESH_DYNAMIC, NULL }, { WPS_TOKEN_FILE_SIZE, "Fs", WPS_REFRESH_DYNAMIC, NULL }, { WPS_TOKEN_FILE_VBR, "Fv", WPS_REFRESH_DYNAMIC, NULL }, - { WPS_TOKEN_FILE_DIRECTORY, "D", WPS_REFRESH_DYNAMIC, parse_dir_level }, + { WPS_TOKEN_FILE_DIRECTORY, "D", WPS_REFRESH_DYNAMIC, + parse_dir_level }, /* current metadata */ { WPS_TOKEN_METADATA_ARTIST, "ia", WPS_REFRESH_STATIC, NULL }, @@ -187,13 +189,13 @@ static const struct wps_tag all_tags[] = { { WPS_TOKEN_METADATA_TRACK_TITLE, "it", WPS_REFRESH_STATIC, NULL }, { WPS_TOKEN_METADATA_VERSION, "iv", WPS_REFRESH_STATIC, NULL }, { WPS_TOKEN_METADATA_YEAR, "iy", WPS_REFRESH_STATIC, NULL }, - { WPS_TOKEN_METADATA_COMMENT, "iC", WPS_REFRESH_STATIC, NULL }, + { WPS_TOKEN_METADATA_COMMENT, "iC", WPS_REFRESH_STATIC, NULL }, /* next metadata */ { WPS_TOKEN_METADATA_ARTIST, "Ia", WPS_REFRESH_DYNAMIC, NULL }, { WPS_TOKEN_METADATA_COMPOSER, "Ic", WPS_REFRESH_DYNAMIC, NULL }, { WPS_TOKEN_METADATA_ALBUM, "Id", WPS_REFRESH_DYNAMIC, NULL }, - { WPS_TOKEN_METADATA_ALBUM_ARTIST, "IA", WPS_REFRESH_DYNAMIC, NULL }, + { WPS_TOKEN_METADATA_ALBUM_ARTIST, "IA", WPS_REFRESH_DYNAMIC, NULL }, { WPS_TOKEN_METADATA_GENRE, "Ig", WPS_REFRESH_DYNAMIC, NULL }, { WPS_TOKEN_METADATA_TRACK_NUMBER, "In", WPS_REFRESH_DYNAMIC, NULL }, { WPS_TOKEN_METADATA_TRACK_TITLE, "It", WPS_REFRESH_DYNAMIC, NULL }, @@ -220,11 +222,13 @@ static const struct wps_tag all_tags[] = { { WPS_TOKEN_PLAYBACK_STATUS, "mp", WPS_REFRESH_DYNAMIC, NULL }, #ifdef HAVE_LCD_BITMAP - { WPS_TOKEN_PEAKMETER, "pm", WPS_REFRESH_PEAK_METER, NULL }, + { WPS_TOKEN_PEAKMETER, "pm", WPS_REFRESH_PEAK_METER, NULL }, #else - { WPS_TOKEN_PLAYER_PROGRESSBAR, "pf", WPS_REFRESH_DYNAMIC | WPS_REFRESH_PLAYER_PROGRESS, parse_progressbar }, + { WPS_TOKEN_PLAYER_PROGRESSBAR, "pf", + WPS_REFRESH_DYNAMIC | WPS_REFRESH_PLAYER_PROGRESS, parse_progressbar }, #endif - { WPS_TOKEN_PROGRESSBAR, "pb", WPS_REFRESH_PLAYER_PROGRESS, parse_progressbar }, + { WPS_TOKEN_PROGRESSBAR, "pb", WPS_REFRESH_PLAYER_PROGRESS, + parse_progressbar }, { WPS_TOKEN_VOLUME, "pv", WPS_REFRESH_DYNAMIC, NULL }, @@ -254,7 +258,8 @@ static const struct wps_tag all_tags[] = { { WPS_NO_TOKEN, "xl", 0, parse_image_load }, - { WPS_TOKEN_IMAGE_PRELOAD_DISPLAY, "xd", WPS_REFRESH_STATIC, parse_image_display }, + { WPS_TOKEN_IMAGE_PRELOAD_DISPLAY, "xd", WPS_REFRESH_STATIC, + parse_image_display }, { WPS_TOKEN_IMAGE_DISPLAY, "x", 0, parse_image_load }, { WPS_TOKEN_IMAGE_PROGRESS_BAR, "P", 0, parse_image_special }, @@ -622,7 +627,8 @@ static int parse_token(const char *wps_bufptr, struct wps_data *wps_data) taglen = (tag->type != WPS_TOKEN_UNKNOWN) ? strlen(tag->name) : 2; token->type = tag->type; - wps_data->sublines[wps_data->num_sublines].line_type |= tag->refresh_type; + wps_data->sublines[wps_data->num_sublines].line_type |= + tag->refresh_type; /* if the tag has a special parsing function, we call it */ if (tag->parse_func) @@ -633,7 +639,8 @@ static int parse_token(const char *wps_bufptr, struct wps_data *wps_data) break; /* tags that start with 'F', 'I' or 'D' are for the next file */ - if ( *(tag->name) == 'I' || *(tag->name) == 'F' || *(tag->name) == 'D') + if ( *(tag->name) == 'I' || *(tag->name) == 'F' || + *(tag->name) == 'D') token->next = true; wps_data->num_tokens++; @@ -758,8 +765,11 @@ static bool wps_parse(struct wps_data *data, const char *wps_bufptr) if ((data->num_lines < WPS_MAX_LINES) && (data->num_sublines < WPS_MAX_SUBLINES)) { - data->lines[data->num_lines].first_subline_idx = data->num_sublines; - data->sublines[data->num_sublines].first_token_idx = data->num_tokens; + data->lines[data->num_lines].first_subline_idx = + data->num_sublines; + + data->sublines[data->num_sublines].first_token_idx = + data->num_tokens; } break; @@ -771,7 +781,7 @@ static bool wps_parse(struct wps_data *data, const char *wps_bufptr) const char *string_start = wps_bufptr - 1; /* find the length of the string */ - while (wps_bufptr && *wps_bufptr != '#' && + while (*wps_bufptr && *wps_bufptr != '#' && *wps_bufptr != '%' && *wps_bufptr != ';' && *wps_bufptr != '<' && *wps_bufptr != '>' && *wps_bufptr != '|' && *wps_bufptr != '\n') @@ -812,7 +822,8 @@ static bool wps_parse(struct wps_data *data, const char *wps_bufptr) data->strings[data->num_strings] = stringbuf; stringbuf += len + 1; stringbuf_used += len + 1; - data->tokens[data->num_tokens].value.i = data->num_strings; + data->tokens[data->num_tokens].value.i = + data->num_strings; data->num_strings++; } else @@ -1021,7 +1032,8 @@ bool wps_data_load(struct wps_data *wps_data, if (!wps_buffer) return false; - /* copy the file's content to the buffer for parsing */ + /* copy the file's content to the buffer for parsing, + ensuring that every line ends with a newline char. */ unsigned int start = 0; while(read_line(fd, wps_buffer + start, buffersize - start) > 0) { -- cgit v1.2.3