diff --git a/tasks/task_database.c b/tasks/task_database.c index f485d33d38..0d0d66e726 100644 --- a/tasks/task_database.c +++ b/tasks/task_database.c @@ -277,17 +277,24 @@ static bool intfstream_file_get_serial(const char *name, size_t offset, size_t s if (!fd) return 0; - intfstream_seek(fd, 0, SEEK_END); + if (intfstream_seek(fd, 0, SEEK_END) == -1) + goto error; + file_size = intfstream_tell(fd); - intfstream_seek(fd, 0, SEEK_SET); + + if (intfstream_seek(fd, 0, SEEK_SET) == -1) + goto error; if (file_size < 0) goto error; if (offset != 0 || size < (size_t) file_size) { + if (intfstream_seek(fd, offset, SEEK_SET) == -1) + goto error; + data = (uint8_t*)malloc(size); - intfstream_seek(fd, offset, SEEK_SET); + if (intfstream_read(fd, data, size) != (ssize_t) size) { free(data); @@ -415,38 +422,33 @@ static bool intfstream_file_get_crc(const char *name, if (!fd) return 0; - intfstream_seek(fd, 0, SEEK_END); + if (intfstream_seek(fd, 0, SEEK_END) == -1) + goto error; + file_size = intfstream_tell(fd); - intfstream_seek(fd, 0, SEEK_SET); + + if (intfstream_seek(fd, 0, SEEK_SET) == -1) + goto error; if (file_size < 0) - { - intfstream_close(fd); - free(fd); - return 0; - } + goto error; if (offset != 0 || size < (size_t) file_size) { + if (intfstream_seek(fd, offset, SEEK_SET) == -1) + goto error; + data = (uint8_t*)malloc(size); - intfstream_seek(fd, offset, SEEK_SET); if (intfstream_read(fd, data, size) != (ssize_t) size) - { - intfstream_close(fd); - free(fd); - free(data); - return 0; - } + goto error; + intfstream_close(fd); free(fd); fd = open_memory(data, size); if (!fd) - { - free(data); - return 0; - } + goto error; } rv = intfstream_get_crc(fd, crc); @@ -454,6 +456,16 @@ static bool intfstream_file_get_crc(const char *name, free(fd); free(data); return rv; + +error: + if (fd) + { + intfstream_close(fd); + free(fd); + } + if (data) + free(data); + return 0; } static int task_database_cue_get_crc(const char *name, uint32_t *crc) diff --git a/tasks/task_database_cue.c b/tasks/task_database_cue.c index 83ad52b2ab..b972face4f 100644 --- a/tasks/task_database_cue.c +++ b/tasks/task_database_cue.c @@ -134,7 +134,9 @@ static int detect_ps1_game_sub(intfstream_t *fp, buffer[0] = '\0'; is_mode1 = 0; - intfstream_seek(fp, 0, SEEK_END); + + if (intfstream_seek(fp, 0, SEEK_END) == -1) + goto error; if (!sub_channel_mixed) { @@ -142,7 +144,9 @@ static int detect_ps1_game_sub(intfstream_t *fp, { unsigned int mode_test = 0; - intfstream_seek(fp, 0, SEEK_SET); + if (intfstream_seek(fp, 0, SEEK_SET) == -1) + goto error; + intfstream_read(fp, &mode_test, 4); if (mode_test != MODETEST_VAL) is_mode1 = 1; @@ -152,11 +156,15 @@ static int detect_ps1_game_sub(intfstream_t *fp, skip = is_mode1? 0: 24; frame_size = sub_channel_mixed? 2448: is_mode1? 2048: 2352; - intfstream_seek(fp, 156 + skip + 16 * frame_size, SEEK_SET); + if (intfstream_seek(fp, 156 + skip + 16 * frame_size, SEEK_SET) == -1) + goto error; + intfstream_read(fp, buffer, 6); cd_sector = buffer[2] | (buffer[3] << 8) | (buffer[4] << 16); - intfstream_seek(fp, skip + cd_sector * frame_size, SEEK_SET); + + if (intfstream_seek(fp, skip + cd_sector * frame_size, SEEK_SET) == -1) + goto error; intfstream_read(fp, buffer, 2048 * 2); tmp = buffer; @@ -175,7 +183,9 @@ static int detect_ps1_game_sub(intfstream_t *fp, goto error; cd_sector = tmp[2] | (tmp[3] << 8) | (tmp[4] << 16); - intfstream_seek(fp, skip + cd_sector * frame_size, SEEK_SET); + if (intfstream_seek(fp, skip + cd_sector * frame_size, SEEK_SET) == -1) + goto error; + intfstream_read(fp, buffer, 256); buffer[256] = '\0';