From 0e9c9921040a1d0e2aa4b820b20535f40a0d75a3 Mon Sep 17 00:00:00 2001 From: rubidium Date: Sat, 23 Nov 2013 13:15:07 +0000 Subject: [PATCH] (svn r26058) -Fix: handle the return value of a number of functions better --- src/bmp.cpp | 10 +++++++++- src/debug.cpp | 2 ++ src/fileio.cpp | 18 +++++++++++++++--- src/ini.cpp | 4 +++- src/newgrf_config.cpp | 6 +++++- src/pathfinder/yapf/yapf_node_rail.hpp | 2 +- src/saveload/oldloader.cpp | 5 ++--- src/saveload/saveload.cpp | 4 +++- src/script/squirrel.cpp | 7 ++++++- src/strgen/strgen.cpp | 10 +++++++--- 10 files changed, 53 insertions(+), 15 deletions(-) diff --git a/src/bmp.cpp b/src/bmp.cpp index a93785d4f4..2cb3dbf787 100644 --- a/src/bmp.cpp +++ b/src/bmp.cpp @@ -25,18 +25,24 @@ void BmpInitializeBuffer(BmpBuffer *buffer, FILE *file) static inline void AdvanceBuffer(BmpBuffer *buffer) { + if (buffer->read < 0) return; + buffer->read = (int)fread(buffer->data, 1, BMP_BUFFER_SIZE, buffer->file); buffer->pos = 0; } static inline bool EndOfBuffer(BmpBuffer *buffer) { + if (buffer->read < 0) return false; + if (buffer->pos == buffer->read || buffer->pos < 0) AdvanceBuffer(buffer); return buffer->pos == buffer->read; } static inline byte ReadByte(BmpBuffer *buffer) { + if (buffer->read < 0) return 0; + if (buffer->pos == buffer->read || buffer->pos < 0) AdvanceBuffer(buffer); buffer->real_pos++; return buffer->data[buffer->pos++]; @@ -62,7 +68,9 @@ static inline void SkipBytes(BmpBuffer *buffer, int bytes) static inline void SetStreamOffset(BmpBuffer *buffer, int offset) { - fseek(buffer->file, offset, SEEK_SET); + if (fseek(buffer->file, offset, SEEK_SET) < 0) { + buffer->read = -1; + } buffer->pos = -1; buffer->real_pos = offset; AdvanceBuffer(buffer); diff --git a/src/debug.cpp b/src/debug.cpp index d173ff14c4..dceae7ec49 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -112,6 +112,8 @@ static void debug_print(const char *dbg, const char *buf) char buf2[1024 + 32]; snprintf(buf2, lengthof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf); + /* Sending out an error when this fails would be nice, however... the error + * would have to be send over this failing socket which won't work. */ send(_debug_socket, buf2, (int)strlen(buf2), 0); return; } diff --git a/src/fileio.cpp b/src/fileio.cpp index 634033747f..e17eaf654a 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -88,7 +88,9 @@ void FioSeekTo(size_t pos, int mode) if (mode == SEEK_CUR) pos += FioGetPos(); _fio.buffer = _fio.buffer_end = _fio.buffer_start + FIO_BUFFER_SIZE; _fio.pos = pos; - fseek(_fio.cur_fh, _fio.pos, SEEK_SET); + if (fseek(_fio.cur_fh, _fio.pos, SEEK_SET) < 0) { + DEBUG(misc, 0, "Seeking in %s failed", _fio.filename); + } } #if defined(LIMITED_FDS) @@ -450,7 +452,11 @@ FILE *FioFOpenFileTar(TarFileListEntry *entry, size_t *filesize) FILE *f = fopen(entry->tar_filename, "rb"); if (f == NULL) return f; - fseek(f, entry->position, SEEK_SET); + if (fseek(f, entry->position, SEEK_SET) < 0) { + fclose(f); + return NULL; + } + if (filesize != NULL) *filesize = entry->size; return f; } @@ -533,6 +539,8 @@ FILE *FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, */ static void FioCreateDirectory(const char *name) { + /* Ignore directory creation errors; they'll surface later on, and most + * of the time they are 'directory already exists' errors anyhow. */ #if defined(WIN32) || defined(WINCE) CreateDirectory(OTTD2FS(name), NULL); #elif defined(OS2) && !defined(__INNOTEK_LIBC__) @@ -897,7 +905,11 @@ bool TarScanner::AddFile(const char *filename, size_t basepath_length, const cha /* Skip to the next block.. */ skip = Align(skip, 512); - fseek(f, skip, SEEK_CUR); + if (fseek(f, skip, SEEK_CUR) < 0) { + DEBUG(misc, 0, "The file '%s' can't be read as a valid tar-file", filename); + fclose(f); + return false; + } pos += skip; } diff --git a/src/ini.cpp b/src/ini.cpp index e82daaceb0..11f79ed16e 100644 --- a/src/ini.cpp +++ b/src/ini.cpp @@ -106,7 +106,9 @@ bool IniFile::SaveToDisk(const char *filename) shfopt.pTo = tfilename; SHFileOperation(&shfopt); #else - rename(file_new, filename); + if (rename(file_new, filename) < 0) { + DEBUG(misc, 0, "Renaming %s to %s failed; configuration not saved", file_new, filename); + } #endif return true; diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index 5ac8ba4df8..bbd670a71c 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -362,7 +362,11 @@ static bool CalcGRFMD5Sum(GRFConfig *config, Subdirectory subdir) size_t start = ftell(f); size = min(size, GRFGetSizeOfDataSection(f)); - fseek(f, start, SEEK_SET); + + if (fseek(f, start, SEEK_SET) < 0) { + FioFCloseFile(f); + return false; + } /* calculate md5sum */ while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) { diff --git a/src/pathfinder/yapf/yapf_node_rail.hpp b/src/pathfinder/yapf/yapf_node_rail.hpp index 4950b2d295..b615b82273 100644 --- a/src/pathfinder/yapf/yapf_node_rail.hpp +++ b/src/pathfinder/yapf/yapf_node_rail.hpp @@ -196,7 +196,7 @@ struct CYapfRailNodeT while (cur != GetLastTile() || cur_td != GetLastTrackdir()) { if (!((obj.*func)(cur, cur_td))) return false; - ft.Follow(cur, cur_td); + if (!ft.Follow(cur, cur_td)) break; cur = ft.m_new_tile; assert(KillFirstBit(ft.m_new_td_bits) == TRACKDIR_BIT_NONE); cur_td = FindFirstTrackdir(ft.m_new_td_bits); diff --git a/src/saveload/oldloader.cpp b/src/saveload/oldloader.cpp index f6ebb48bcc..ff911a899e 100644 --- a/src/saveload/oldloader.cpp +++ b/src/saveload/oldloader.cpp @@ -250,10 +250,9 @@ static SavegameType DetermineOldSavegameType(FILE *f, char *title, const char *l /* Can't fseek to 0 as in tar files that is not correct */ long pos = ftell(f); - if (!CheckOldSavegameType(f, temp, lastof(temp), TTO_HEADER_SIZE)) { + if (pos >= 0 && !CheckOldSavegameType(f, temp, lastof(temp), TTO_HEADER_SIZE)) { type = SGT_TTD; - fseek(f, pos, SEEK_SET); - if (!CheckOldSavegameType(f, temp, lastof(temp), TTD_HEADER_SIZE)) { + if (fseek(f, pos, SEEK_SET) < 0 || !CheckOldSavegameType(f, temp, lastof(temp), TTD_HEADER_SIZE)) { type = SGT_INVALID; } } diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 4bffa40c73..dc63abe5b7 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -1848,7 +1848,9 @@ struct FileReader : LoadFilter { /* virtual */ void Reset() { clearerr(this->file); - fseek(this->file, this->begin, SEEK_SET); + if (fseek(this->file, this->begin, SEEK_SET)) { + DEBUG(sl, 1, "Could not reset the file reading"); + } } }; diff --git a/src/script/squirrel.cpp b/src/script/squirrel.cpp index 7c684d4b82..89b0b56254 100644 --- a/src/script/squirrel.cpp +++ b/src/script/squirrel.cpp @@ -493,7 +493,12 @@ SQRESULT Squirrel::LoadFile(HSQUIRRELVM vm, const char *filename, SQBool printer } func = _io_file_lexfeed_UTF8; break; - default: func = _io_file_lexfeed_ASCII; fseek(file, -2, SEEK_CUR); break; // ASCII + default: // ASCII + func = _io_file_lexfeed_ASCII; + if (fseek(file, -2, SEEK_CUR) < 0) { + return sq_throwerror(vm, _SC("cannot read the file")); + } + break; } if (SQ_SUCCEEDED(sq_compile(vm, func, &f, OTTD2SQ(filename), printerror))) { diff --git a/src/strgen/strgen.cpp b/src/strgen/strgen.cpp index 01eaed4cb5..5d04538413 100644 --- a/src/strgen/strgen.cpp +++ b/src/strgen/strgen.cpp @@ -353,7 +353,9 @@ struct LanguageFileWriter : LanguageWriter, FileWriter { void Finalise() { - fputc(0, this->fh); + if (fputc(0, this->fh) == EOF) { + error("Could not write to %s", this->filename); + } this->FileWriter::Finalise(); } @@ -368,10 +370,12 @@ struct LanguageFileWriter : LanguageWriter, FileWriter { /** Multi-OS mkdirectory function */ static inline void ottd_mkdir(const char *directory) { + /* Ignore directory creation errors; they'll surface later on, and most + * of the time they are 'directory already exists' errors anyhow. */ #if defined(WIN32) || defined(__WATCOMC__) - mkdir(directory); + mkdir(directory); #else - mkdir(directory, 0755); + mkdir(directory, 0755); #endif }