[Scummvm-git-logs] scummvm master -> e35ad1e428e47c5d8ac20d7f0a74e0f41d52e68c

Strangerke noreply at scummvm.org
Tue May 14 18:00:05 UTC 2024


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
e35ad1e428 BAGEL: Work on fatal errors in CBofDataFile and CBofStringTable


Commit: e35ad1e428e47c5d8ac20d7f0a74e0f41d52e68c
    https://github.com/scummvm/scummvm/commit/e35ad1e428e47c5d8ac20d7f0a74e0f41d52e68c
Author: Strangerke (arnaud.boutonne at gmail.com)
Date: 2024-05-14T18:59:26+01:00

Commit Message:
BAGEL: Work on fatal errors in CBofDataFile and CBofStringTable

Changed paths:
    engines/bagel/boflib/dat_file.cpp
    engines/bagel/boflib/res.cpp


diff --git a/engines/bagel/boflib/dat_file.cpp b/engines/bagel/boflib/dat_file.cpp
index 7ae6fa59af1..0da7d531fa5 100644
--- a/engines/bagel/boflib/dat_file.cpp
+++ b/engines/bagel/boflib/dat_file.cpp
@@ -174,20 +174,18 @@ ErrorCode CBofDataFile::open() {
 	assert(isValidObject(this));
 
 	// Only continue if there is no current error
-	if (_errCode == ERR_NONE) {
-		if (_stream == nullptr) {
-			if (!(_lFlags & CDF_READONLY)) {
-				if (_lFlags & CDF_SAVEFILE) {
-					if (_lFlags & CDF_CREATE)
-						create();
-				} else if (!fileExists(_szFileName))
+	if (_errCode == ERR_NONE && _stream == nullptr) {
+		if (!(_lFlags & CDF_READONLY)) {
+			if (_lFlags & CDF_SAVEFILE) {
+				if (_lFlags & CDF_CREATE)
 					create();
-			}
+			} else if (!fileExists(_szFileName))
+				create();
+		}
 
-			if (_stream == nullptr) {
-				// Open data file
-				CBofFile::open(_szFileName, _lFlags);
-			}
+		if (_stream == nullptr) {
+			// Open data file
+			CBofFile::open(_szFileName, _lFlags);
 		}
 	}
 
@@ -242,32 +240,30 @@ ErrorCode CBofDataFile::readHeader() {
 
 					if (_lHeaderLength != 0) {
 						// Allocate buffer to hold header
-						if ((_pHeader = new HEADER_REC[(int)_lNumRecs]) != nullptr) {
-							// Seek to start of header
-							seek(_lHeaderStart);
-
-							// Read header
-							ErrorCode errCode = ERR_NONE;
-							for (int i = 0; i < _lNumRecs && errCode == ERR_NONE; ++i) {
-								errCode = read(_pHeader[i]);
-							}
+						_pHeader = new HEADER_REC[(int)_lNumRecs];
+						if (_pHeader == nullptr)
+							fatalError(ERR_MEMORY, buildString("Could not allocate footer for file '%s'", _szFileName));
+
+						// Seek to start of header
+						seek(_lHeaderStart);
 
-							if (errCode == ERR_NONE) {
-								uint32 lCrc = calculateCRC(&_pHeader->_lOffset, 4 * _lNumRecs);
+						// Read header
+						ErrorCode errCode = ERR_NONE;
+						for (int i = 0; i < _lNumRecs && errCode == ERR_NONE; ++i) {
+							errCode = read(_pHeader[i]);
+						}
 
-								if (lCrc != stHeaderInfo._lFootCrc) {
-									logError(buildString("Error: '%s' has invalid footer", _szFileName));
-									_errCode = ERR_CRC;
-								}
+						if (errCode == ERR_NONE) {
+							uint32 lCrc = calculateCRC(&_pHeader->_lOffset, 4 * _lNumRecs);
 
-							} else {
-								logError(buildString("Error: Could not read footer in file '%s'", _szFileName));
-								_errCode = ERR_FREAD;
+							if (lCrc != stHeaderInfo._lFootCrc) {
+								logError(buildString("Error: '%s' has invalid footer", _szFileName));
+								_errCode = ERR_CRC;
 							}
 
 						} else {
-							logError(buildString("Error: Could not allocate footer for file '%s'", _szFileName));
-							_errCode = ERR_MEMORY;
+							logError(buildString("Error: Could not read footer in file '%s'", _szFileName));
+							_errCode = ERR_FREAD;
 						}
 					}
 
@@ -505,35 +501,33 @@ ErrorCode CBofDataFile::writeRecord(int32 lRecNum, void *pBuf, int32 lSize, bool
 
 			// Allocate a buffer big enough for one chunk
 			byte *pTmpBuf = (byte *)bofAlloc(lChunkSize);
-			if (pTmpBuf != nullptr) {
-				// While there is data to move
-				while (lBufLength > 0) {
-					// Seek to beginning of the source for this chunk
-					setPosition(pRecInfo->_lOffset + pRecInfo->_lLength + lBufLength - lChunkSize);
+			if (pTmpBuf == nullptr)
+				fatalError(ERR_MEMORY, "Unable to allocate %ld bytes to expand record size in writeRecord()", lBufLength);
 
-					// Read the chunk
-					read(pTmpBuf, lChunkSize);
+			// While there is data to move
+			while (lBufLength > 0) {
+				// Seek to beginning of the source for this chunk
+				setPosition(pRecInfo->_lOffset + pRecInfo->_lLength + lBufLength - lChunkSize);
 
-					// Seek to this chunks new positon (offset by 'lDiff' bytes)
-					setPosition(pRecInfo->_lOffset + pRecInfo->_lLength + lBufLength - lChunkSize + lDiff);
+				// Read the chunk
+				read(pTmpBuf, lChunkSize);
 
-					// Write chunk to new position
-					write(pTmpBuf, lChunkSize);
+				// Seek to this chunks new position (offset by 'lDiff' bytes)
+				setPosition(pRecInfo->_lOffset + pRecInfo->_lLength + lBufLength - lChunkSize + lDiff);
 
-					// That much less to do next time thru
-					lBufLength -= lChunkSize;
+				// Write chunk to new position
+				write(pTmpBuf, lChunkSize);
 
-					// Last chunk is lBufLength
-					lChunkSize = MIN(lBufLength, lChunkSize);
-				}
-
-				// Don't need that temp buffer anymore
-				bofFree(pTmpBuf);
+				// That much less to do next time thru
+				lBufLength -= lChunkSize;
 
-			} else {
-				reportError(ERR_MEMORY, "Unable to allocate %ld bytes to expand record size in writeRecord()", lBufLength);
+				// Last chunk is lBufLength
+				lChunkSize = MIN(lBufLength, lChunkSize);
 			}
 
+			// Don't need that temp buffer anymore
+			bofFree(pTmpBuf);
+
 			// Tell the rest of the records that they moved
 			for (int i = lRecNum + 1; i < getNumberOfRecs(); i++) {
 				_pHeader[i]._lOffset += lDiff;
@@ -563,22 +557,20 @@ ErrorCode CBofDataFile::writeRecord(int32 lRecNum, void *pBuf, int32 lSize, bool
 
 					// Allocate a buffer that could hold the largest record
 					byte *pTmpBuf = (byte *)bofAlloc((int)getMaxRecSize());
-					if (pTmpBuf != nullptr) {
-						for (int i = (int)lRecNum + 1; i < (int)_lNumRecs - 1; i++) {
-							_errCode = readRecord(i, pTmpBuf);
-							if (_errCode != ERR_NONE)
-								break;
-
-							_errCode = writeRecord(i + 1, pTmpBuf);
-							if (_errCode != ERR_NONE)
-								break;
-						}
+					if (pTmpBuf == nullptr)
+						fatalError(ERR_MEMORY, "unable to allocate a buffer of %ld bytes", getMaxRecSize());
 
-						bofFree(pTmpBuf);
+					for (int i = (int)lRecNum + 1; i < (int)_lNumRecs - 1; i++) {
+						_errCode = readRecord(i, pTmpBuf);
+						if (_errCode != ERR_NONE)
+							break;
 
-					} else {
-						_errCode = ERR_MEMORY;
+						_errCode = writeRecord(i + 1, pTmpBuf);
+						if (_errCode != ERR_NONE)
+							break;
 					}
+
+					bofFree(pTmpBuf);
 				}
 
 				// If we are to update the header now
@@ -609,13 +601,11 @@ ErrorCode CBofDataFile::verifyRecord(int32 lRecNum) {
 
 		// Allocate space to hold this record
 		void *pBuf = bofAlloc((int)getRecSize(lRecNum));
-		if (pBuf != nullptr) {
-			_errCode = readRecord(lRecNum, pBuf);
-			bofFree(pBuf);
+		if (pBuf == nullptr)
+			fatalError(ERR_MEMORY, "Unable to allocate a buffer of %ld bytes", getRecSize(lRecNum));
 
-		} else {
-			_errCode = ERR_MEMORY;
-		}
+		_errCode = readRecord(lRecNum, pBuf);
+		bofFree(pBuf);
 	}
 
 	return _errCode;
@@ -656,33 +646,30 @@ ErrorCode CBofDataFile::addRecord(void *pBuf, int32 lLength, bool bUpdateHeader,
 				_lNumRecs++;
 
 				HEADER_REC *pTmpHeader = new HEADER_REC[(int)_lNumRecs];
-				if (pTmpHeader != nullptr) {
-					if (_pHeader != nullptr) {
-						memcpy(pTmpHeader, _pHeader, (size_t)(HEADER_REC::size() * (_lNumRecs - 1)));
-
-						delete[] _pHeader;
-					}
+				if (pTmpHeader == nullptr)
+					fatalError(ERR_MEMORY, "Could not allocate a data file header");
 
-					_pHeader = pTmpHeader;
+				if (_pHeader != nullptr) {
+					memcpy(pTmpHeader, _pHeader, (size_t)(HEADER_REC::size() * (_lNumRecs - 1)));
+					delete[] _pHeader;
+				}
 
-					int32 lRecNum = _lNumRecs - 1;
-					HEADER_REC *pCurRec = &_pHeader[lRecNum];
-					int32 lPrevLength = HEAD_INFO::size();
-					int32 lPrevOffset = 0;
+				_pHeader = pTmpHeader;
 
-					if (lRecNum != 0) {
-						lPrevLength = _pHeader[lRecNum - 1]._lLength;
-						lPrevOffset = _pHeader[lRecNum - 1]._lOffset;
-					}
+				int32 lRecNum = _lNumRecs - 1;
+				HEADER_REC *pCurRec = &_pHeader[lRecNum];
+				int32 lPrevLength = HEAD_INFO::size();
+				int32 lPrevOffset = 0;
 
-					pCurRec->_lLength = lLength;
-					pCurRec->_lOffset = lPrevOffset + lPrevLength;
+				if (lRecNum != 0) {
+					lPrevLength = _pHeader[lRecNum - 1]._lLength;
+					lPrevOffset = _pHeader[lRecNum - 1]._lOffset;
+				}
 
-					writeRecord(lRecNum, pBuf, lLength, bUpdateHeader, lKey);
+				pCurRec->_lLength = lLength;
+				pCurRec->_lOffset = lPrevOffset + lPrevLength;
 
-				} else {
-					reportError(ERR_MEMORY, "Could not allocate a data file header");
-				}
+				writeRecord(lRecNum, pBuf, lLength, bUpdateHeader, lKey);
 			}
 		}
 	}
diff --git a/engines/bagel/boflib/res.cpp b/engines/bagel/boflib/res.cpp
index 5ddfd9cbe58..d24dde27ae0 100644
--- a/engines/bagel/boflib/res.cpp
+++ b/engines/bagel/boflib/res.cpp
@@ -54,17 +54,14 @@ ErrorCode CBofStringTable::load(const char *pszFileName) {
 
 	// Allocate a buffer to hold entire file
 	_pBuf = (byte *)bofAlloc(_lBufSize + 1);
-	if (_pBuf != nullptr) {
-		memset(_pBuf, 0, _lBufSize + 1);
-
-		// Read in entire file
-		read(_pBuf, _lBufSize);
+	if (_pBuf == nullptr)
+		fatalError(ERR_MEMORY, "Unable to allocate %u bytes for String Table", _lBufSize);
 
-		buildTable();
+	memset(_pBuf, 0, _lBufSize + 1);
 
-	} else {
-		reportError(ERR_MEMORY, "Unable to allocate %ld bytes for String Table", _lBufSize);
-	}
+	// Read in entire file
+	read(_pBuf, _lBufSize);
+	buildTable();
 
 	// Don't need this file open anymore
 	close();
@@ -116,16 +113,14 @@ ErrorCode CBofStringTable::buildTable() {
 		pBuf++;
 
 		CResString *pString = new CResString(nId, (const char *)pBuf);
-		if (pString != nullptr) {
-			// Add this string to the table
-			if (_pStringTable == nullptr) {
-				_pStringTable = pString;
-			} else {
-				_pStringTable->addToTail(pString);
-			}
+		if (pString == nullptr)
+			fatalError(ERR_MEMORY, "Unable to allocate a CResString");
+
+		// Add this string to the table
+		if (_pStringTable == nullptr) {
+			_pStringTable = pString;
 		} else {
-			reportError(ERR_MEMORY, "Unable to allocate a CResString");
-			break;
+			_pStringTable->addToTail(pString);
 		}
 
 		while (*pBuf++ != '\0') {




More information about the Scummvm-git-logs mailing list