mirror of https://github.com/OpenTTD/OpenTTD.git
(svn r26058) -Fix: handle the return value of a number of functions better
This commit is contained in:
parent
b3e93d6520
commit
0e9c992104
10
src/bmp.cpp
10
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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -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))) {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue