From e00b8a93bf7a53ef09bc349aefd3a2a37d58f09e Mon Sep 17 00:00:00 2001 From: Sergeanur Date: Fri, 25 Dec 2020 15:18:48 +0200 Subject: [PATCH] Fix use of strncmp --- src/control/Script4.cpp | 8 +-- src/control/Script8.cpp | 3 +- src/core/FileLoader.cpp | 95 +++++++++++++++--------------- src/core/Frontend.cpp | 4 +- src/core/General.h | 9 +++ src/modelinfo/TimeModelInfo.cpp | 3 +- src/modelinfo/VehicleModelInfo.cpp | 14 ++--- src/peds/PedFight.cpp | 4 +- src/peds/PedType.cpp | 4 +- src/vehicles/HandlingMgr.cpp | 2 +- 10 files changed, 80 insertions(+), 66 deletions(-) diff --git a/src/control/Script4.cpp b/src/control/Script4.cpp index e8475473..ba481b52 100644 --- a/src/control/Script4.cpp +++ b/src/control/Script4.cpp @@ -2169,10 +2169,10 @@ int8 CRunningScript::ProcessCommands900To999(int32 command) case COMMAND_PRINT_HELP: { if (CCamera::m_bUseMouse3rdPerson && ( - strncmp((char*)&CTheScripts::ScriptSpace[m_nIp], "HELP15", 7) == 0 || - strncmp((char*)&CTheScripts::ScriptSpace[m_nIp], "GUN_2A", 7) == 0 || - strncmp((char*)&CTheScripts::ScriptSpace[m_nIp], "GUN_2C", 7) == 0 || - strncmp((char*)&CTheScripts::ScriptSpace[m_nIp], "GUN_2D", 7) == 0)) { + strcmp((char*)&CTheScripts::ScriptSpace[m_nIp], "HELP15") == 0 || + strcmp((char*)&CTheScripts::ScriptSpace[m_nIp], "GUN_2A") == 0 || + strcmp((char*)&CTheScripts::ScriptSpace[m_nIp], "GUN_2C") == 0 || + strcmp((char*)&CTheScripts::ScriptSpace[m_nIp], "GUN_2D") == 0)) { m_nIp += KEY_LENGTH_IN_SCRIPT; return 0; } diff --git a/src/control/Script8.cpp b/src/control/Script8.cpp index 08965e72..a75692a0 100644 --- a/src/control/Script8.cpp +++ b/src/control/Script8.cpp @@ -132,11 +132,12 @@ int8 CRunningScript::ProcessCommands1400To1499(int32 command) CollectParameters(&m_nIp, 1); CPlayerInfo* pPlayerInfo = &CWorld::Players[ScriptParams[0]]; char key[KEY_LENGTH_IN_SCRIPT]; + memset(key, 0, KEY_LENGTH_IN_SCRIPT); CTheScripts::ReadTextLabelFromScript(&m_nIp, key); m_nIp += KEY_LENGTH_IN_SCRIPT; CVector pos = pPlayerInfo->GetPos(); CZone *infoZone = CTheZones::FindInformationZoneForPosition(&pos); - UpdateCompareFlag(strncmp(key, infoZone->name, 8) == 0); + UpdateCompareFlag(strncmp(key, infoZone->name, 8) == 0); // original code doesn't seem to be using strncmp in here and compare 2 ints instead return 0; } case COMMAND_CLEAR_CHAR_ICE_CREAM_PURCHASE: diff --git a/src/core/FileLoader.cpp b/src/core/FileLoader.cpp index dd313f79..f86911ca 100644 --- a/src/core/FileLoader.cpp +++ b/src/core/FileLoader.cpp @@ -70,7 +70,7 @@ CFileLoader::LoadLevel(const char *filename) if(*line == '#') continue; - if(strncmp(line, "EXIT", 9) == 0) // BUG: 9? + if(strncmp(line, "EXIT", 4) == 0) break; if(strncmp(line, "IMAGEPATH", 9) == 0){ @@ -172,7 +172,7 @@ CFileLoader::LoadTexDictionary(const char *filename) struct ColHeader { - char ident[4]; + uint32 ident; uint32 size; }; @@ -191,7 +191,7 @@ CFileLoader::LoadCollisionFile(const char *filename, uint8 colSlot) assert(fd > 0); while(CFileMgr::Read(fd, (char*)&header, sizeof(header))){ - assert(strncmp(header.ident, "COLL", 4) == 0); + assert(header.ident == 'LLOC'); CFileMgr::Read(fd, (char*)work_buff, header.size); memcpy(modelname, work_buff, 24); @@ -228,7 +228,7 @@ CFileLoader::LoadCollisionFileFirstTime(uint8 *buffer, uint32 size, uint8 colSlo while(size > 8){ header = (ColHeader*)buffer; modelsize = header->size; - if(strncmp(header->ident, "COLL", 4) != 0) + if(header->ident == 'LLOC') return size-8 < CDSTREAM_SECTOR_SIZE; memcpy(modelname, buffer+8, 24); memcpy(work_buff, buffer+32, modelsize-24); @@ -262,7 +262,7 @@ CFileLoader::LoadCollisionFile(uint8 *buffer, uint32 size, uint8 colSlot) while(size > 8){ header = (ColHeader*)buffer; modelsize = header->size; - if(strncmp(header->ident, "COLL", 4) != 0) + if(header->ident == 'LLOC') return size-8 < CDSTREAM_SECTOR_SIZE; memcpy(modelname, buffer+8, 24); memcpy(work_buff, buffer+32, modelsize-24); @@ -586,6 +586,9 @@ CFileLoader::AddTexDictionaries(RwTexDictionary *dst, RwTexDictionary *src) RwTexDictionaryForAllTextures(src, MoveTexturesCB, dst); } +#define isLine3(l, a, b, c) ((l[0] == a) && (l[1] == b) && (l[2] == c)) +#define isLine4(l, a, b, c, d) ((l[0] == a) && (l[1] == b) && (l[2] == c) && (l[3] == d)) + void CFileLoader::LoadObjectTypes(const char *filename) { @@ -621,15 +624,15 @@ CFileLoader::LoadObjectTypes(const char *filename) continue; if(section == NONE){ - if(strncmp(line, "objs", 4) == 0) section = OBJS; - else if(strncmp(line, "tobj", 4) == 0) section = TOBJ; - else if(strncmp(line, "weap", 4) == 0) section = WEAP; - else if(strncmp(line, "hier", 4) == 0) section = HIER; - else if(strncmp(line, "cars", 4) == 0) section = CARS; - else if(strncmp(line, "peds", 4) == 0) section = PEDS; - else if(strncmp(line, "path", 4) == 0) section = PATH; - else if(strncmp(line, "2dfx", 4) == 0) section = TWODFX; - }else if(strncmp(line, "end", 3) == 0){ + if(isLine4(line, 'o','b','j','s')) section = OBJS; + else if(isLine4(line, 't','o','b','j')) section = TOBJ; + else if(isLine4(line, 'w','e','a','p')) section = WEAP; + else if(isLine4(line, 'h','i','e','r')) section = HIER; + else if(isLine4(line, 'c','a','r','s')) section = CARS; + else if(isLine4(line, 'p','e','d','s')) section = PEDS; + else if(isLine4(line, 'p','a','t','h')) section = PATH; + else if(isLine4(line, '2','d','f','x')) section = TWODFX; + }else if(isLine3(line, 'e','n','d')){ section = NONE; }else switch(section){ case OBJS: @@ -862,21 +865,21 @@ CFileLoader::LoadVehicleObject(const char *line) mi->m_level = level; mi->m_compRules = comprules; - if(strncmp(type, "car", 4) == 0){ + if(strcmp(type, "car") == 0){ mi->m_wheelId = misc; mi->m_wheelScale = wheelScale; mi->m_vehicleType = VEHICLE_TYPE_CAR; - }else if(strncmp(type, "boat", 5) == 0){ + }else if(strcmp(type, "boat") == 0){ mi->m_vehicleType = VEHICLE_TYPE_BOAT; - }else if(strncmp(type, "train", 6) == 0){ + }else if(strcmp(type, "train") == 0){ mi->m_vehicleType = VEHICLE_TYPE_TRAIN; - }else if(strncmp(type, "heli", 5) == 0){ + }else if(strcmp(type, "heli") == 0){ mi->m_vehicleType = VEHICLE_TYPE_HELI; - }else if(strncmp(type, "plane", 6) == 0){ + }else if(strcmp(type, "plane") == 0){ mi->m_planeLodId = misc; mi->m_wheelScale = 1.0f; mi->m_vehicleType = VEHICLE_TYPE_PLANE; - }else if(strncmp(type, "bike", 5) == 0){ + }else if(strcmp(type, "bike") == 0){ mi->m_bikeSteerAngle = misc; mi->m_wheelScale = wheelScale; mi->m_vehicleType = VEHICLE_TYPE_BIKE; @@ -885,29 +888,29 @@ CFileLoader::LoadVehicleObject(const char *line) mi->m_handlingId = mod_HandlingManager.GetHandlingId(handlingId); - if(strncmp(vehclass, "normal", 7) == 0) + if(strcmp(vehclass, "normal") == 0) mi->m_vehicleClass = CCarCtrl::NORMAL; - else if(strncmp(vehclass, "poorfamily", 11) == 0) + else if(strcmp(vehclass, "poorfamily") == 0) mi->m_vehicleClass = CCarCtrl::POOR; - else if(strncmp(vehclass, "richfamily", 11) == 0) + else if(strcmp(vehclass, "richfamily") == 0) mi->m_vehicleClass = CCarCtrl::RICH; - else if(strncmp(vehclass, "executive", 10) == 0) + else if(strcmp(vehclass, "executive") == 0) mi->m_vehicleClass = CCarCtrl::EXEC; - else if(strncmp(vehclass, "worker", 7) == 0) + else if(strcmp(vehclass, "worker") == 0) mi->m_vehicleClass = CCarCtrl::WORKER; - else if(strncmp(vehclass, "big", 4) == 0) + else if(strcmp(vehclass, "big") == 0) mi->m_vehicleClass = CCarCtrl::BIG; - else if(strncmp(vehclass, "taxi", 5) == 0) + else if(strcmp(vehclass, "taxi") == 0) mi->m_vehicleClass = CCarCtrl::TAXI; - else if(strncmp(vehclass, "moped", 6) == 0) + else if(strcmp(vehclass, "moped") == 0) mi->m_vehicleClass = CCarCtrl::MOPED; - else if(strncmp(vehclass, "motorbike", 10) == 0) + else if(strcmp(vehclass, "motorbike") == 0) mi->m_vehicleClass = CCarCtrl::MOTORBIKE; - else if(strncmp(vehclass, "leisureboat", 12) == 0) + else if(strcmp(vehclass, "leisureboat") == 0) mi->m_vehicleClass = CCarCtrl::LEISUREBOAT; - else if(strncmp(vehclass, "workerboat", 11) == 0) + else if(strcmp(vehclass, "workerboat") == 0) mi->m_vehicleClass = CCarCtrl::WORKERBOAT; - else if(strncmp(vehclass, "ignore", 11) == 0){ + else if(strcmp(vehclass, "ignore") == 0) { mi->m_vehicleClass = -1; return; } @@ -1122,13 +1125,13 @@ CFileLoader::LoadScene(const char *filename) continue; if(section == NONE){ - if(strncmp(line, "inst", 4) == 0) section = INST; - else if(strncmp(line, "zone", 4) == 0) section = ZONE; - else if(strncmp(line, "cull", 4) == 0) section = CULL; - else if(strncmp(line, "pick", 4) == 0) section = PICK; - else if(strncmp(line, "path", 4) == 0) section = PATH; - else if(strncmp(line, "occl", 4) == 0) section = OCCL; - }else if(strncmp(line, "end", 3) == 0){ + if(isLine4(line, 'i','n','s','t')) section = INST; + else if(isLine4(line, 'z','o','n','e')) section = ZONE; + else if(isLine4(line, 'c','u','l','l')) section = CULL; + else if(isLine4(line, 'p','i','c','k')) section = PICK; + else if(isLine4(line, 'p','a','t','h')) section = PATH; + else if(isLine4(line, 'o','c','c','l')) section = OCCL; + }else if(isLine3(line, 'e','n','d')){ section = NONE; }else switch(section){ case INST: @@ -1326,11 +1329,11 @@ CFileLoader::ReloadPaths(const char *filename) continue; if (section == NONE) { - if (strncmp(line, "path", 4) == 0) { + if (isLine4(line, 'p','a','t','h')) { section = PATH; ThePaths.AllocatePathFindInfoMem(4500); } - } else if (strncmp(line, "end", 3) == 0) { + } else if (isLine3(line, 'e','n','d')) { section = NONE; } else { switch (section) { @@ -1381,10 +1384,10 @@ CFileLoader::ReloadObjectTypes(const char *filename) continue; if (section == NONE) { - if (strncmp(line, "objs", 4) == 0) section = OBJS; - else if (strncmp(line, "tobj", 4) == 0) section = TOBJ; - else if (strncmp(line, "2dfx", 4) == 0) section = TWODFX; - } else if (strncmp(line, "end", 3) == 0) { + if (isLine4(line, 'o','b','j','s')) section = OBJS; + else if (isLine4(line, 't','o','b','j')) section = TOBJ; + else if (isLine4(line, '2','d','f','x')) section = TWODFX; + } else if (isLine3(line, 'e','n','d')) { section = NONE; } else { switch (section) { @@ -1457,7 +1460,7 @@ CFileLoader::ReLoadScene(const char *filename) if (*line == '#') continue; - if (strncmp(line, "EXIT", 9) == 0) // BUG: 9? + if (strncmp(line, "EXIT", 4) == 0) break; if (strncmp(line, "IDE", 3) == 0) { diff --git a/src/core/Frontend.cpp b/src/core/Frontend.cpp index 95f3de86..ba13712e 100644 --- a/src/core/Frontend.cpp +++ b/src/core/Frontend.cpp @@ -1048,7 +1048,7 @@ CMenuManager::DrawStandardMenus(bool activeScreen) } if (m_nPrefsAudio3DProviderIndex == NO_AUDIO_PROVIDER) { - if (strncmp(aScreens[m_nCurrScreen].m_aEntries[i].m_EntryName, "FEO_AUD", 8) == 0) { + if (strcmp(aScreens[m_nCurrScreen].m_aEntries[i].m_EntryName, "FEO_AUD") == 0) { CFont::SetColor(CRGBA(DARKMENUOPTION_COLOR.r, DARKMENUOPTION_COLOR.g, DARKMENUOPTION_COLOR.b, FadeIn(255))); } } @@ -2431,7 +2431,7 @@ CMenuManager::DrawPlayerSetupScreen(bool activeScreen) SYSTEMTIME SystemTime; HANDLE handle = FindFirstFile("skins\\*.bmp", &FindFileData); for (int i = 1; handle != INVALID_HANDLE_VALUE && i; i = FindNextFile(handle, &FindFileData)) { - if (strncmp(FindFileData.cFileName, DEFAULT_SKIN_NAME, 5) != 0) { + if (strcmp(FindFileData.cFileName, DEFAULT_SKIN_NAME) != 0) { m_pSelectedSkin->nextSkin = new tSkinInfo; m_pSelectedSkin = m_pSelectedSkin->nextSkin; m_pSelectedSkin->skinId = nextSkinId; diff --git a/src/core/General.h b/src/core/General.h index 7e06b96e..69492fb8 100644 --- a/src/core/General.h +++ b/src/core/General.h @@ -123,6 +123,15 @@ public: return *str2 != '\0'; } + static bool faststrncmp(const char *str1, const char *str2, uint32 count) + { + for(uint32 i = 0; *str1 && i < count; str1++, str2++, i++) { + if (*str1 != *str2) + return true; + } + return false; + } + static bool faststricmp(const char *str1, const char *str2) { for (; *str1; str1++, str2++) { diff --git a/src/modelinfo/TimeModelInfo.cpp b/src/modelinfo/TimeModelInfo.cpp index d4f92293..c1c18dac 100644 --- a/src/modelinfo/TimeModelInfo.cpp +++ b/src/modelinfo/TimeModelInfo.cpp @@ -2,6 +2,7 @@ #include "Camera.h" #include "ModelInfo.h" +#include "General.h" CTimeModelInfo* CTimeModelInfo::FindOtherTimeModel(void) @@ -23,7 +24,7 @@ CTimeModelInfo::FindOtherTimeModel(void) for(i = 0; i < MODELINFOSIZE; i++){ CBaseModelInfo *mi = CModelInfo::GetModelInfo(i); if (mi && mi->GetModelType() == MITYPE_TIME && - strncmp(name, mi->GetName(), 24) == 0){ + !CGeneral::faststrncmp(name, mi->GetName(), MAX_MODEL_NAME)){ m_otherTimeModelID = i; return (CTimeModelInfo*)mi; } diff --git a/src/modelinfo/VehicleModelInfo.cpp b/src/modelinfo/VehicleModelInfo.cpp index b9d6f300..6dc9e5b0 100644 --- a/src/modelinfo/VehicleModelInfo.cpp +++ b/src/modelinfo/VehicleModelInfo.cpp @@ -333,7 +333,7 @@ CVehicleModelInfo::SetAtomicRendererCB(RpAtomic *atomic, void *data) name = GetFrameNodeName(RpAtomicGetFrame(atomic)); alpha = false; RpGeometryForAllMaterials(RpAtomicGetGeometry(atomic), HasAlphaMaterialCB, &alpha); - if(strstr(name, "_hi") || strncmp(name, "extra", 5) == 0){ + if(strstr(name, "_hi") || !CGeneral::faststrncmp(name, "extra", 5)) { if(alpha || strncmp(name, "windscreen", 10) == 0) CVisibilityPlugins::SetAtomicRenderCallback(atomic, CVisibilityPlugins::RenderVehicleHiDetailAlphaCB); else @@ -359,7 +359,7 @@ CVehicleModelInfo::SetAtomicRendererCB_BigVehicle(RpAtomic *atomic, void *data) name = GetFrameNodeName(RpAtomicGetFrame(atomic)); alpha = false; RpGeometryForAllMaterials(RpAtomicGetGeometry(atomic), HasAlphaMaterialCB, &alpha); - if(strstr(name, "_hi") || strncmp(name, "extra", 5) == 0){ + if(strstr(name, "_hi") || !CGeneral::faststrncmp(name, "extra", 5)) { if(alpha) CVisibilityPlugins::SetAtomicRenderCallback(atomic, CVisibilityPlugins::RenderVehicleHiDetailAlphaCB_BigVehicle); else @@ -407,7 +407,7 @@ CVehicleModelInfo::SetAtomicRendererCB_Boat(RpAtomic *atomic, void *data) clump = (RpClump*)data; name = GetFrameNodeName(RpAtomicGetFrame(atomic)); - if(strcmp(name, "boat_hi") == 0 || strncmp(name, "extra", 5) == 0) + if(strcmp(name, "boat_hi") == 0 || !CGeneral::faststrncmp(name, "extra", 5)) CVisibilityPlugins::SetAtomicRenderCallback(atomic, CVisibilityPlugins::RenderVehicleHiDetailCB_Boat); else if(strstr(name, "_hi")) CVisibilityPlugins::SetAtomicRenderCallback(atomic, CVisibilityPlugins::RenderVehicleHiDetailCB); @@ -453,7 +453,7 @@ CVehicleModelInfo::SetAtomicRendererCB_RealHeli(RpAtomic *atomic, void *data) CVisibilityPlugins::SetAtomicRenderCallback(atomic, CVisibilityPlugins::RenderVehicleRotorAlphaCB); else if(strncmp(name, "rearrotor", 9) == 0) CVisibilityPlugins::SetAtomicRenderCallback(atomic, CVisibilityPlugins::RenderVehicleTailRotorAlphaCB); - else if(strstr(name, "_hi") || strncmp(name, "extra", 5) == 0){ + else if(strstr(name, "_hi") || !CGeneral::faststrncmp(name, "extra", 5)) { if(alpha || strncmp(name, "windscreen", 10) == 0) CVisibilityPlugins::SetAtomicRenderCallback(atomic, CVisibilityPlugins::RenderVehicleHiDetailAlphaCB); else @@ -1011,11 +1011,11 @@ CVehicleModelInfo::LoadVehicleColours(void) continue; if(section == NONE){ - if(strncmp(&line[start], "col", 3) == 0) + if(line[start] == 'c' && line[start + 1] == 'o' && line[start + 2] == 'l') section = COLOURS; - else if(strncmp(&line[start], "car", 3) == 0) + if(line[start] == 'c' && line[start + 1] == 'a' && line[start + 2] == 'r') section = CARS; - }else if(strncmp(&line[start], "end", 3) == 0){ + }else if(line[start] == 'e' && line[start + 1] == 'n' && line[start + 2] == 'd'){ section = NONE; }else if(section == COLOURS){ sscanf(&line[start], // BUG: games doesn't add start diff --git a/src/peds/PedFight.cpp b/src/peds/PedFight.cpp index 3e0dfb6d..151d3806 100644 --- a/src/peds/PedFight.cpp +++ b/src/peds/PedFight.cpp @@ -2393,8 +2393,8 @@ CPed::LoadFightData(void) break; } - if (strncmp(animName, "default", 8) != 0) { - if (strncmp(animName, "null", 5) != 0) { + if (strcmp(animName, "default") != 0) { + if (strcmp(animName, "null") != 0) { animAssoc = CAnimManager::GetAnimAssociation(ASSOCGRP_STD, animName); tFightMoves[moveId].animId = (AnimationId)animAssoc->animId; } else { diff --git a/src/peds/PedType.cpp b/src/peds/PedType.cpp index 746eb3a5..5565b90f 100644 --- a/src/peds/PedType.cpp +++ b/src/peds/PedType.cpp @@ -81,7 +81,7 @@ CPedType::LoadPedData(void) // Game uses just "line" here since sscanf already trims whitespace, but this is safer sscanf(&line[lp], "%s", word); - if(strncmp(word, "Threat", 7) == 0){ + if(strcmp(word, "Threat") == 0){ flags = 0; lp += 7; while(sscanf(&line[lp], "%s", word) == 1 && lp <= linelen){ @@ -94,7 +94,7 @@ CPedType::LoadPedData(void) lp++; } ms_apPedType[type]->m_threats = flags; - }else if(strncmp(word, "Avoid", 6) == 0){ + }else if(strcmp(word, "Avoid") == 0){ flags = 0; lp += 6; while(sscanf(&line[lp], "%s", word) == 1 && lp <= linelen){ diff --git a/src/vehicles/HandlingMgr.cpp b/src/vehicles/HandlingMgr.cpp index f96835d3..16c1b6de 100644 --- a/src/vehicles/HandlingMgr.cpp +++ b/src/vehicles/HandlingMgr.cpp @@ -172,7 +172,7 @@ cHandlingDataMgr::LoadHandlingData(void) end = start+1; // yeah, this is kinda crappy - if(strncmp(line, ";the end", 9) == 0) + if(strcmp(line, ";the end") == 0) keepGoing = 0; else if(line[0] != ';'){ if(line[0] == '!'){