From cddc4323b1d7b3369ccdad3d55eed26f8a95d0ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Andr=C3=A9n=20Zachrisson?= Date: Sun, 1 Nov 2015 11:41:15 +0100 Subject: [PATCH] Use local file descriptor to minimize risk of mixing them up. Check the error status of the fseek and ftell --- cue2pops.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/cue2pops.c b/cue2pops.c index 47cd74d..b0c79ba 100644 --- a/cue2pops.c +++ b/cue2pops.c @@ -218,14 +218,26 @@ void NTSCpatcher(unsigned char *inbuf, int tracker) int GetLeadOut(void) { /* MSF is calculated from the dump size so DO NOT APPLY gap++/gap-- ADJUSTMENTS IN THIS FUNCTION ! */ - - if(!(file = fopen(bin_path, "rb"))) { // Open the BINARY that is attached to the cue + FILE *bin; + int status; + + if(!(bin = fopen(bin_path, "rb"))) { // Open the BINARY that is attached to the cue printf("Error: Cannot open %s\n\n", bin_path); return -1; } - fseek(file, 0, SEEK_END); - bin_size = ftell(file); // Get it's size - fclose(file); + + status = fseek(bin, 0, SEEK_END); + if (status != 0) { + printf("Error: Failed to seek %s\n", bin_path); + return -1; + } + + bin_size = ftell(bin); // Get it's size + if (bin_size == -1L) { + printf("Error: Failed to get file %s size\n", bin_path); + return -1; + } + fclose(bin); sector_count = (bin_size / sectorsize) + (150 * (pregap_count + postgap_count)) + 150; // Convert the bin_size to sector count leadoutM = sector_count / 4500;