Prevent passing NULL/empty strings to memcmp

This commit is contained in:
twinaphex 2017-05-28 18:07:00 +02:00
parent d8f1cf7b5a
commit cf2f8f6489
1 changed files with 83 additions and 72 deletions

View File

@ -277,46 +277,50 @@ int detect_psp_game(const char *track_path, char *game_id)
if (filestream_read(fd, game_id, 5) > 0) if (filestream_read(fd, game_id, 5) > 0)
{ {
game_id[5] = '\0'; game_id[5] = '\0';
if (
(string_is_equal_fast(game_id, "ULES-", 5))
|| (string_is_equal_fast(game_id, "ULUS-", 5))
|| (string_is_equal_fast(game_id, "ULJS-", 5))
|| (string_is_equal_fast(game_id, "ULEM-", 5)) if (!string_is_empty(game_id))
|| (string_is_equal_fast(game_id, "ULUM-", 5))
|| (string_is_equal_fast(game_id, "ULJM-", 5))
|| (string_is_equal_fast(game_id, "UCES-", 5))
|| (string_is_equal_fast(game_id, "UCUS-", 5))
|| (string_is_equal_fast(game_id, "UCJS-", 5))
|| (string_is_equal_fast(game_id, "UCAS-", 5))
|| (string_is_equal_fast(game_id, "NPEH-", 5))
|| (string_is_equal_fast(game_id, "NPUH-", 5))
|| (string_is_equal_fast(game_id, "NPJH-", 5))
|| (string_is_equal_fast(game_id, "NPEG-", 5))
|| (string_is_equal_fast(game_id, "NPUG-", 5))
|| (string_is_equal_fast(game_id, "NPJG-", 5))
|| (string_is_equal_fast(game_id, "NPHG-", 5))
|| (string_is_equal_fast(game_id, "NPEZ-", 5))
|| (string_is_equal_fast(game_id, "NPUZ-", 5))
|| (string_is_equal_fast(game_id, "NPJZ-", 5))
)
{ {
filestream_seek(fd, pos, SEEK_SET); if (
if (filestream_read(fd, game_id, 10) > 0) (string_is_equal_fast(game_id, "ULES-", 5))
{ || (string_is_equal_fast(game_id, "ULUS-", 5))
|| (string_is_equal_fast(game_id, "ULJS-", 5))
|| (string_is_equal_fast(game_id, "ULEM-", 5))
|| (string_is_equal_fast(game_id, "ULUM-", 5))
|| (string_is_equal_fast(game_id, "ULJM-", 5))
|| (string_is_equal_fast(game_id, "UCES-", 5))
|| (string_is_equal_fast(game_id, "UCUS-", 5))
|| (string_is_equal_fast(game_id, "UCJS-", 5))
|| (string_is_equal_fast(game_id, "UCAS-", 5))
|| (string_is_equal_fast(game_id, "NPEH-", 5))
|| (string_is_equal_fast(game_id, "NPUH-", 5))
|| (string_is_equal_fast(game_id, "NPJH-", 5))
|| (string_is_equal_fast(game_id, "NPEG-", 5))
|| (string_is_equal_fast(game_id, "NPUG-", 5))
|| (string_is_equal_fast(game_id, "NPJG-", 5))
|| (string_is_equal_fast(game_id, "NPHG-", 5))
|| (string_is_equal_fast(game_id, "NPEZ-", 5))
|| (string_is_equal_fast(game_id, "NPUZ-", 5))
|| (string_is_equal_fast(game_id, "NPJZ-", 5))
)
{
filestream_seek(fd, pos, SEEK_SET);
if (filestream_read(fd, game_id, 10) > 0)
{
#if 0 #if 0
game_id[4] = '-'; game_id[4] = '-';
game_id[8] = game_id[9]; game_id[8] = game_id[9];
game_id[9] = game_id[10]; game_id[9] = game_id[10];
#endif #endif
game_id[10] = '\0'; game_id[10] = '\0';
rv = true; rv = true;
} }
break; break;
}
} }
} }
else else
@ -346,6 +350,7 @@ int detect_system(const char *track_path, const char **system_name)
for (i = 0; MAGIC_NUMBERS[i].system_name != NULL; i++) for (i = 0; MAGIC_NUMBERS[i].system_name != NULL; i++)
{ {
filestream_seek(fd, MAGIC_NUMBERS[i].offset, SEEK_SET); filestream_seek(fd, MAGIC_NUMBERS[i].offset, SEEK_SET);
if (filestream_read(fd, magic, MAGIC_LEN) < MAGIC_LEN) if (filestream_read(fd, magic, MAGIC_LEN) < MAGIC_LEN)
{ {
RARCH_LOG("Could not read data from file '%s' at offset %d: %s\n", RARCH_LOG("Could not read data from file '%s' at offset %d: %s\n",
@ -354,7 +359,9 @@ int detect_system(const char *track_path, const char **system_name)
goto clean; goto clean;
} }
if (string_is_equal_fast(MAGIC_NUMBERS[i].magic, magic, MAGIC_LEN)) if (!string_is_empty(MAGIC_NUMBERS[i].magic) &&
!string_is_empty(magic) &&
string_is_equal_fast(MAGIC_NUMBERS[i].magic, magic, MAGIC_LEN))
{ {
*system_name = MAGIC_NUMBERS[i].system_name; *system_name = MAGIC_NUMBERS[i].system_name;
rv = 0; rv = 0;
@ -366,7 +373,8 @@ int detect_system(const char *track_path, const char **system_name)
if (filestream_read(fd, magic, 8) > 0) if (filestream_read(fd, magic, 8) > 0)
{ {
magic[8] = '\0'; magic[8] = '\0';
if (string_is_equal_fast(magic, "PSP GAME", 8)) if (!string_is_empty(magic) &&
string_is_equal_fast(magic, "PSP GAME", 8))
{ {
*system_name = "psp\0"; *system_name = "psp\0";
rv = 0; rv = 0;
@ -403,46 +411,49 @@ int find_first_data_track(const char *cue_path,
while (get_token(fd, tmp_token, MAX_TOKEN_LEN) > 0) while (get_token(fd, tmp_token, MAX_TOKEN_LEN) > 0)
{ {
if (string_is_equal_fast(tmp_token, "FILE", 4)) if (!string_is_empty(tmp_token))
{ {
char cue_dir[PATH_MAX_LENGTH]; if (string_is_equal_fast(tmp_token, "FILE", 4))
cue_dir[0] = '\0';
fill_pathname_basedir(cue_dir, cue_path, sizeof(cue_dir));
get_token(fd, tmp_token, MAX_TOKEN_LEN);
fill_pathname_join(track_path, cue_dir, tmp_token, max_len);
}
else if (string_is_equal_fast(tmp_token, "TRACK", 5))
{
int m, s, f;
get_token(fd, tmp_token, MAX_TOKEN_LEN);
get_token(fd, tmp_token, MAX_TOKEN_LEN);
if (string_is_equal_fast(tmp_token, "AUDIO", 5))
continue;
find_token(fd, "INDEX");
get_token(fd, tmp_token, MAX_TOKEN_LEN);
get_token(fd, tmp_token, MAX_TOKEN_LEN);
if (sscanf(tmp_token, "%02d:%02d:%02d", &m, &s, &f) < 3)
{ {
RARCH_LOG("Error parsing time stamp '%s'\n", tmp_token); char cue_dir[PATH_MAX_LENGTH];
filestream_close(fd);
return -errno; cue_dir[0] = '\0';
fill_pathname_basedir(cue_dir, cue_path, sizeof(cue_dir));
get_token(fd, tmp_token, MAX_TOKEN_LEN);
fill_pathname_join(track_path, cue_dir, tmp_token, max_len);
} }
else if (string_is_equal_fast(tmp_token, "TRACK", 5))
{
int m, s, f;
get_token(fd, tmp_token, MAX_TOKEN_LEN);
get_token(fd, tmp_token, MAX_TOKEN_LEN);
*offset = ((m * 60) * (s * 75) * f) * 25; if (string_is_equal_fast(tmp_token, "AUDIO", 5))
continue;
RARCH_LOG("%s '%s+%d'\n", find_token(fd, "INDEX");
msg_hash_to_str(MSG_FOUND_FIRST_DATA_TRACK_ON_FILE), get_token(fd, tmp_token, MAX_TOKEN_LEN);
track_path, *offset); get_token(fd, tmp_token, MAX_TOKEN_LEN);
rv = 0; if (sscanf(tmp_token, "%02d:%02d:%02d", &m, &s, &f) < 3)
goto clean; {
RARCH_LOG("Error parsing time stamp '%s'\n", tmp_token);
filestream_close(fd);
return -errno;
}
*offset = ((m * 60) * (s * 75) * f) * 25;
RARCH_LOG("%s '%s+%d'\n",
msg_hash_to_str(MSG_FOUND_FIRST_DATA_TRACK_ON_FILE),
track_path, *offset);
rv = 0;
goto clean;
}
} }
} }