From: Guus Sliepen Date: Tue, 27 Jul 2010 16:06:27 +0000 (+0200) Subject: Use strlcat() and strlcpy(), #define wrappers when these functions are not available. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=d581939511f482a3bfdaba7f6470de8939e6cd5e;p=quix0rs-blobwars.git Use strlcat() and strlcpy(), #define wrappers when these functions are not available. --- diff --git a/src/CAudio.cpp b/src/CAudio.cpp index 521c18b..c2a3712 100644 --- a/src/CAudio.cpp +++ b/src/CAudio.cpp @@ -149,11 +149,11 @@ bool Audio::loadMusic(const char *filename) line[l - 1] = 0; if(!strncasecmp(line, "title=", 6)) - strncpy(songtitle, line + 6, sizeof songtitle); + strlcpy(songtitle, line + 6, sizeof songtitle); else if(!strncasecmp(line, "album=", 6)) - strncpy(songalbum, line + 6, sizeof songalbum); + strlcpy(songalbum, line + 6, sizeof songalbum); else if(!strncasecmp(line, "artist=", 7)) - strncpy(songartist, line + 7, sizeof songartist); + strlcpy(songartist, line + 7, sizeof songartist); else if(!strncasecmp(line, "license=", 8)) { if(!strncasecmp(line + 8, "CC-BY ", 6)) @@ -166,7 +166,7 @@ bool Audio::loadMusic(const char *filename) if(fp) fclose(fp); - strncpy(levelMusicName, filename, sizeof levelMusicName); + strlcpy(levelMusicName, filename, sizeof levelMusicName); return true; } diff --git a/src/CCutscene.cpp b/src/CCutscene.cpp index 9da816f..bf3d537 100644 --- a/src/CCutscene.cpp +++ b/src/CCutscene.cpp @@ -29,6 +29,6 @@ Cutscene::Cutscene() void Cutscene::appendText(const char *line) { - strncat(text, " ", sizeof text); - strncat(text, line, sizeof text); + strlcat(text, " ", sizeof text); + strlcat(text, line, sizeof text); } diff --git a/src/CData.cpp b/src/CData.cpp index 12642ed..e082394 100644 --- a/src/CData.cpp +++ b/src/CData.cpp @@ -36,8 +36,8 @@ void Data::set(const char *key, const char *value, int current, int target) return; } - strncpy(this->key, key, sizeof this->key); - strncpy(this->value, value, sizeof this->value); + strlcpy(this->key, key, sizeof this->key); + strlcpy(this->value, value, sizeof this->value); this->current = current; this->target = target; } diff --git a/src/CEngine.cpp b/src/CEngine.cpp index 2520ba1..0217527 100644 --- a/src/CEngine.cpp +++ b/src/CEngine.cpp @@ -64,7 +64,7 @@ Engine::Engine() binaryBuffer = NULL; #ifdef FRAMEWORK_SDL char pakPath[PATH_MAX]; - strncpy(pakPath, PAKFULLPATH, sizeof(pakPath)); + strlcpy(pakPath, PAKFULLPATH, sizeof(pakPath)); if (CFBundleGetMainBundle() != NULL) { CFURLRef pakURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR(PAKNAME), NULL, NULL); if (pakURL != NULL) { @@ -229,7 +229,7 @@ void Engine::getInput() } keyState[event.key.keysym.sym] = 1; - strncpy(lastKeyPressed, SDL_GetKeyName(event.key.keysym.sym), sizeof lastKeyPressed); + strlcpy(lastKeyPressed, SDL_GetKeyName(event.key.keysym.sym), sizeof lastKeyPressed); addKeyEvent(); break; @@ -314,7 +314,7 @@ void Engine::clearInput() void Engine::setUserHome(const char *path) { - strncpy(userHomeDirectory, path, sizeof userHomeDirectory); + strlcpy(userHomeDirectory, path, sizeof userHomeDirectory); debug(("User Home = %s\n", path)); } @@ -489,7 +489,7 @@ void Engine::setInfoMessage(const char *message, int priority, int type) { if (priority >= messagePriority) { - strncpy(this->message, message, sizeof this->message); + strlcpy(this->message, message, sizeof this->message); messageTime = 180; messagePriority = priority; messageType = type; @@ -957,7 +957,7 @@ int Engine::getValueOfFlagTokens(const char *realLine) char line[1024]; bool found; int value; - strncpy(line, realLine, sizeof line); + strlcpy(line, realLine, sizeof line); int flags = 0; diff --git a/src/CEntity.cpp b/src/CEntity.cpp index e1575ca..9fcf6a8 100644 --- a/src/CEntity.cpp +++ b/src/CEntity.cpp @@ -52,7 +52,7 @@ Entity::Entity() void Entity::setName(const char *name) { - strncpy(this->name, name, sizeof this->name); + strlcpy(this->name, name, sizeof this->name); } void Entity::setSprites(Sprite *sprite1, Sprite *sprite2, Sprite *sprite3) diff --git a/src/CFileData.cpp b/src/CFileData.cpp index f3ebe92..52643f0 100644 --- a/src/CFileData.cpp +++ b/src/CFileData.cpp @@ -36,7 +36,7 @@ void FileData::set(const char *filename, Uint32 fSize, Uint32 cSize, Uint32 loca return; } - strncpy(this->filename, filename, sizeof this->filename); + strlcpy(this->filename, filename, sizeof this->filename); this->fSize = fSize; this->cSize = cSize; diff --git a/src/CGame.cpp b/src/CGame.cpp index a5fa060..b75820d 100644 --- a/src/CGame.cpp +++ b/src/CGame.cpp @@ -66,8 +66,8 @@ void Game::clear() bulletsHit[i] = bulletsFired[i] = 0; } - strncpy(mapName, "data/grasslands1", sizeof mapName); - strncpy(stageName, "Grasslands", sizeof stageName); + strlcpy(mapName, "data/grasslands1", sizeof mapName); + strlcpy(stageName, "Grasslands", sizeof stageName); continuesUsed = 0; levelsStarted = 0; @@ -221,12 +221,12 @@ void Game::totalUpStats() void Game::setStageName(const char *name) { - strncpy(stageName, name, sizeof stageName); + strlcpy(stageName, name, sizeof stageName); } void Game::setMapName(const char *name) { - strncpy(mapName, name, sizeof mapName); + strlcpy(mapName, name, sizeof mapName); } void Game::setMissionOver(int reason) diff --git a/src/CGameData.cpp b/src/CGameData.cpp index a7d17cf..36e1112 100644 --- a/src/CGameData.cpp +++ b/src/CGameData.cpp @@ -90,7 +90,7 @@ void GameData::setMIARescueCount(const char *key, int rescues, int total) data = (Data*)data->next; if (strcmp(newKey, data->key) == 0) { - strncpy(data->value, "MIAs", sizeof data->value); + strlcpy(data->value, "MIAs", sizeof data->value); data->current = rescues; data->target = total; return; diff --git a/src/CGraphics.cpp b/src/CGraphics.cpp index 353a837..6dde3df 100644 --- a/src/CGraphics.cpp +++ b/src/CGraphics.cpp @@ -616,7 +616,7 @@ void Graphics::loadFont(int i, const char *filename, int pixelSize) Sprite *Graphics::addSprite(const char *name) { Sprite *sprite = new Sprite; - strncpy(sprite->name, name, sizeof sprite->name); + strlcpy(sprite->name, name, sizeof sprite->name); spriteList.add(sprite); @@ -946,8 +946,8 @@ void Graphics::clearChatString() void Graphics::createChatString(const char *in) { - strncat(chatString, " ", sizeof chatString); - strncat(chatString, in, sizeof chatString); + strlcat(chatString, " ", sizeof chatString); + strlcat(chatString, in, sizeof chatString); } void Graphics::drawChatString(SDL_Surface *surface, int y) diff --git a/src/CHub.cpp b/src/CHub.cpp index 4c8192e..ad04651 100644 --- a/src/CHub.cpp +++ b/src/CHub.cpp @@ -33,8 +33,8 @@ HubLevel::HubLevel() void HubLevel::set(const char *stageName, const char *filename, int x, int y) { - strncpy(this->stageName, stageName, sizeof this->stageName); - strncpy(this->filename, filename, sizeof this->filename); + strlcpy(this->stageName, stageName, sizeof this->stageName); + strlcpy(this->filename, filename, sizeof this->filename); this->x = x; this->y = y; } diff --git a/src/CKeyboard.cpp b/src/CKeyboard.cpp index b9bc5bc..d0f877a 100644 --- a/src/CKeyboard.cpp +++ b/src/CKeyboard.cpp @@ -49,7 +49,7 @@ const char *Keyboard::translateKey(int key) return "..."; } - strncpy(keyName, _(SDL_GetKeyName((SDLKey)key)), sizeof keyName); + strlcpy(keyName, _(SDL_GetKeyName((SDLKey)key)), sizeof keyName); /* This is not really neccessary, but it just makes diff --git a/src/CLineDef.cpp b/src/CLineDef.cpp index 916bdf6..0686573 100644 --- a/src/CLineDef.cpp +++ b/src/CLineDef.cpp @@ -33,9 +33,9 @@ LineDef::LineDef() void LineDef::set(const char *name, const char *linkName, const char *activateMessage, int x, int y, int width, int height) { - strncpy(this->name, name, sizeof this->name); - strncpy(this->linkName, linkName, sizeof this->linkName); - strncpy(this->activateMessage, activateMessage, sizeof this->activateMessage); + strlcpy(this->name, name, sizeof this->name); + strlcpy(this->linkName, linkName, sizeof this->linkName); + strlcpy(this->activateMessage, activateMessage, sizeof this->activateMessage); this->x = x; this->y = y; this->width = width; diff --git a/src/CMap.cpp b/src/CMap.cpp index 72ce787..007c57b 100644 --- a/src/CMap.cpp +++ b/src/CMap.cpp @@ -220,7 +220,7 @@ void Map::destroyPersistant(const char *name) if (strcmp(p->stageName, name) == 0) { - strncpy(p->stageName, "@none@", sizeof p->stageName); + strlcpy(p->stageName, "@none@", sizeof p->stageName); p->clear(); return; } @@ -229,7 +229,7 @@ void Map::destroyPersistant(const char *name) void Map::setName(const char *name) { - strncpy(this->name, name, sizeof this->name); + strlcpy(this->name, name, sizeof this->name); if (strstr(name, "BioMech")) { diff --git a/src/CMedalServer.cpp b/src/CMedalServer.cpp index fc9e9b6..1ec4e79 100644 --- a/src/CMedalServer.cpp +++ b/src/CMedalServer.cpp @@ -50,7 +50,7 @@ bool MedalServer::connect(const char *privateKey) debug(("Connected %s to %s:%d\n", privateKey, MEDAL_SERVER_HOST, MEDAL_SERVER_PORT)); - strncpy(this->privateKey, privateKey, sizeof this->privateKey); + strlcpy(this->privateKey, privateKey, sizeof this->privateKey); connected = true; return true; @@ -70,7 +70,7 @@ int MedalServer::postMedal(const char *str) char *store; char medal[128]; - strncpy(medal, str, sizeof medal); + strlcpy(medal, str, sizeof medal); for (unsigned int i = 0 ; i < strlen(medal) ; i++) { @@ -123,7 +123,7 @@ int MedalServer::postMedal(const char *str) if (response == 4) { - strncpy(rubyMessage, message, sizeof rubyMessage); + strlcpy(rubyMessage, message, sizeof rubyMessage); gotRuby = true; } else diff --git a/src/CObjective.cpp b/src/CObjective.cpp index 9bf75f3..aa5c6fb 100644 --- a/src/CObjective.cpp +++ b/src/CObjective.cpp @@ -34,8 +34,8 @@ Objective::Objective() Objective::Objective(const char *description, const char *target, int targetValue, bool required) { - strncpy(this->description, description, sizeof this->description); - strncpy(this->target, target, sizeof this->target); + strlcpy(this->description, description, sizeof this->description); + strlcpy(this->target, target, sizeof this->target); this->targetValue = targetValue; this->required = required; diff --git a/src/CPak.cpp b/src/CPak.cpp index 6c4c16a..a6811d3 100644 --- a/src/CPak.cpp +++ b/src/CPak.cpp @@ -61,7 +61,7 @@ void Pak::showPakErrorAndExit() void Pak::setPakFile(const char *pakFilename) { #if USEPAK - strncpy(this->pakFilename, pakFilename, sizeof this->pakFilename); + strlcpy(this->pakFilename, pakFilename, sizeof this->pakFilename); debug(("Pak : Filename set to %s\n", pakFilename)); diff --git a/src/CPersistData.cpp b/src/CPersistData.cpp index cd4bf14..1a1f300 100644 --- a/src/CPersistData.cpp +++ b/src/CPersistData.cpp @@ -37,7 +37,7 @@ bool PersistData::setData(const char *data) return false; } - strncpy(this->data, data, sizeof this->data); + strlcpy(this->data, data, sizeof this->data); return true; } diff --git a/src/CPersistant.cpp b/src/CPersistant.cpp index 82f03d6..2ea8e61 100644 --- a/src/CPersistant.cpp +++ b/src/CPersistant.cpp @@ -34,7 +34,7 @@ Persistant::~Persistant() void Persistant::setName(const char *name) { - strncpy(this->stageName, name, sizeof this->stageName); + strlcpy(this->stageName, name, sizeof this->stageName); } void Persistant::clear() diff --git a/src/CSpawnPoint.cpp b/src/CSpawnPoint.cpp index c7c91e5..178b7c2 100644 --- a/src/CSpawnPoint.cpp +++ b/src/CSpawnPoint.cpp @@ -42,7 +42,7 @@ void SpawnPoint::reset() void SpawnPoint::create(const char *name, int x, int y, int spawnType, int spawnSubType, int minInterval, int maxInterval, bool active) { - strncpy(this->name, name, sizeof this->name); + strlcpy(this->name, name, sizeof this->name); this->x = x; this->y = y; this->spawnType = spawnType; diff --git a/src/CSwitch.cpp b/src/CSwitch.cpp index d5492f4..ef1c59d 100644 --- a/src/CSwitch.cpp +++ b/src/CSwitch.cpp @@ -37,10 +37,10 @@ Switch::Switch() void Switch::set(const char *name, const char *linkName, const char *requiredObjectName, const char *activateMessage, int type, int x, int y, bool activated) { - strncpy(this->name, name, sizeof this->name); - strncpy(this->linkName, linkName, sizeof this->linkName); - strncpy(this->requiredObjectName, requiredObjectName, sizeof this->requiredObjectName); - strncpy(this->activateMessage, activateMessage, sizeof this->activateMessage); + strlcpy(this->name, name, sizeof this->name); + strlcpy(this->linkName, linkName, sizeof this->linkName); + strlcpy(this->requiredObjectName, requiredObjectName, sizeof this->requiredObjectName); + strlcpy(this->activateMessage, activateMessage, sizeof this->activateMessage); this->type = type; this->x = x; this->y = y; diff --git a/src/CTeleporter.cpp b/src/CTeleporter.cpp index 9f213aa..ae299a0 100644 --- a/src/CTeleporter.cpp +++ b/src/CTeleporter.cpp @@ -29,7 +29,7 @@ Teleporter::Teleporter() void Teleporter::setName(const char *name) { - strncpy(this->name, name, sizeof this->name); + strlcpy(this->name, name, sizeof this->name); } void Teleporter::set(int x, int y, int destX, int destY) diff --git a/src/CTrain.cpp b/src/CTrain.cpp index b385278..89d5302 100644 --- a/src/CTrain.cpp +++ b/src/CTrain.cpp @@ -76,7 +76,7 @@ bool Train::waitsForPlayer() void Train::setName(const char *name) { - strncpy(this->name, name, sizeof this->name); + strlcpy(this->name, name, sizeof this->name); } void Train::set(int startX, int startY, int endX, int endY, int pause, bool fromStart) diff --git a/src/CTrap.cpp b/src/CTrap.cpp index 224f924..a43304d 100644 --- a/src/CTrap.cpp +++ b/src/CTrap.cpp @@ -30,7 +30,7 @@ Trap::Trap() void Trap::setName(const char *name) { - strncpy(this->name, name, sizeof this->name); + strlcpy(this->name, name, sizeof this->name); } void Trap::setTrapType(int type) diff --git a/src/CWeapon.cpp b/src/CWeapon.cpp index 7268b8b..53d962b 100644 --- a/src/CWeapon.cpp +++ b/src/CWeapon.cpp @@ -34,7 +34,7 @@ Weapon::Weapon() void Weapon::setName(const char *name) { - strncpy(this->name, name, sizeof this->name); + strlcpy(this->name, name, sizeof this->name); } int Weapon::getSpeed(int face) diff --git a/src/CWidget.cpp b/src/CWidget.cpp index fbf8c38..eaca008 100644 --- a/src/CWidget.cpp +++ b/src/CWidget.cpp @@ -43,10 +43,10 @@ void Widget::setProperties(const char *name, const char *groupName, const char * exit(1); } - strncpy(this->name, name, sizeof this->name); - strncpy(this->groupName, groupName, sizeof this->groupName); - strncpy(this->label, label, sizeof this->label); - strncpy(this->options, options, sizeof this->options); + strlcpy(this->name, name, sizeof this->name); + strlcpy(this->groupName, groupName, sizeof this->groupName); + strlcpy(this->label, label, sizeof this->label); + strlcpy(this->options, options, sizeof this->options); this->x = x; this->y = y; this->min = min; diff --git a/src/aquaBoss.cpp b/src/aquaBoss.cpp index 73df1ab..2e50744 100644 --- a/src/aquaBoss.cpp +++ b/src/aquaBoss.cpp @@ -287,7 +287,7 @@ void aquaBossMainInit() debug(("aquaBossMainInit\n")); map.boss[0] = new Boss(); - strncpy(map.boss[0]->name, "BioMech Aqua Blob", sizeof map.boss[0]->name); + strlcpy(map.boss[0]->name, "BioMech Aqua Blob", sizeof map.boss[0]->name); map.boss[0]->health = 45 * game.skill; map.boss[0]->maxHealth = 45 * game.skill; map.boss[0]->setSprites(graphics.getSprite("AquaBossRight", true), graphics.getSprite("AquaBossLeft", true), graphics.getSprite("AquaBossLeft", true)); diff --git a/src/cutscene.cpp b/src/cutscene.cpp index 3d49d76..1952bb7 100644 --- a/src/cutscene.cpp +++ b/src/cutscene.cpp @@ -51,7 +51,7 @@ void createSceneList() line = strtok(NULL, "\n"); if (strcmp(line, "@none@") != 0) { - strncpy(scene->sprite, line, sizeof scene->sprite); + strlcpy(scene->sprite, line, sizeof scene->sprite); debug(("Loading cutscene image %s\n", scene->sprite)); graphics.quickSprite(scene->sprite, graphics.loadImage(scene->sprite)); } diff --git a/src/droidBoss.cpp b/src/droidBoss.cpp index 69e8aa3..da6b9fb 100644 --- a/src/droidBoss.cpp +++ b/src/droidBoss.cpp @@ -365,7 +365,7 @@ void droidBossInit() debug(("droidBossInit\n")); map.boss[0] = new Boss(); - strncpy(map.boss[0]->name, "BioMech Jetpack Blob", sizeof map.boss[0]->name); + strlcpy(map.boss[0]->name, "BioMech Jetpack Blob", sizeof map.boss[0]->name); map.boss[0]->health = 30 * game.skill; map.boss[0]->maxHealth = 30 * game.skill; map.boss[0]->setSprites(graphics.getSprite("JetpackBlobRight1", true), graphics.getSprite("JetpackBlobLeft1", true), graphics.getSprite("JetpackBlobLeft1", true)); @@ -387,7 +387,7 @@ void droidBossInit() for (int i = 1 ; i < 6 ; i++) { map.boss[i] = new Boss(); - strncpy(map.boss[i]->name, "Orb Bomb", sizeof map.boss[i]->name); + strlcpy(map.boss[i]->name, "Orb Bomb", sizeof map.boss[i]->name); map.boss[i]->setSprites(graphics.getSprite("DroidOrb", true), graphics.getSprite("DroidOrb", true), graphics.getSprite("DroidOrb", true)); map.boss[i]->active = false; map.boss[i]->face = 1; diff --git a/src/finalBattle.cpp b/src/finalBattle.cpp index e69b96f..197862b 100644 --- a/src/finalBattle.cpp +++ b/src/finalBattle.cpp @@ -147,7 +147,7 @@ void galdovInitBlackDroids() map.boss[i] = new Boss(); } debug(("BlackDroid %d init\n", i)); - strncpy(map.boss[i]->name, "BlackDrod", sizeof map.boss[i]->name); + strlcpy(map.boss[i]->name, "BlackDrod", sizeof map.boss[i]->name); map.boss[i]->health = -90; map.boss[i]->maxHealth = -90; map.boss[i]->setSprites(graphics.getSprite("BlackDroidRight", true), graphics.getSprite("BlackDroidLeft", true), graphics.getSprite("BlackDroidDie", true)); @@ -493,7 +493,7 @@ void galdovFinalSplit() if (map.boss[i] == NULL) { map.boss[i] = new Boss(); - strncpy(map.boss[i]->name, "MiniGaldov", sizeof map.boss[i]->name); + strlcpy(map.boss[i]->name, "MiniGaldov", sizeof map.boss[i]->name); map.boss[i]->health = 10 * game.skill; map.boss[i]->maxHealth = 10 * game.skill; } @@ -556,7 +556,7 @@ void galdovFinalInit() debug(("galdovFinalInit\n")); map.boss[0] = new Boss(); - strncpy(map.boss[0]->name, "Galdov", sizeof map.boss[0]->name); + strlcpy(map.boss[0]->name, "Galdov", sizeof map.boss[0]->name); map.boss[0]->health = 45 * game.skill; map.boss[0]->maxHealth = 45 * game.skill; map.boss[0]->setSprites(graphics.getSprite("GaldovRight", true), graphics.getSprite("GaldovLeft", true), graphics.getSprite("GaldovDie", true)); @@ -680,7 +680,7 @@ void galdovFinalShieldInit() delete map.boss[i]; } map.boss[i] = new Boss(); - strncpy(map.boss[i]->name, "OrbBomb", sizeof map.boss[i]->name); + strlcpy(map.boss[i]->name, "OrbBomb", sizeof map.boss[i]->name); map.boss[i]->setSprites(graphics.getSprite("DroidOrb", true), graphics.getSprite("DroidOrb", true), graphics.getSprite("DroidOrb", true)); map.boss[i]->health = 999999; map.boss[i]->maxHealth = 99999; diff --git a/src/galdov.cpp b/src/galdov.cpp index 5de463f..fa491dc 100644 --- a/src/galdov.cpp +++ b/src/galdov.cpp @@ -456,7 +456,7 @@ void galdovInit() debug(("galdovInit\n")); map.boss[0] = new Boss(); - strncpy(map.boss[0]->name, "Galdov", sizeof map.boss[0]->name); + strlcpy(map.boss[0]->name, "Galdov", sizeof map.boss[0]->name); map.boss[0]->health = 45 * game.skill; map.boss[0]->maxHealth = 45 * game.skill; map.boss[0]->setSprites(graphics.getSprite("GaldovRight", true), graphics.getSprite("GaldovLeft", true), graphics.getSprite("GaldovLeft", true)); @@ -480,7 +480,7 @@ void galdovInit() for (int i = 1 ; i < 10 ; i++) { map.boss[i] = new Boss(); - strncpy(map.boss[i]->name, "Fake", sizeof map.boss[i]->name); + strlcpy(map.boss[i]->name, "Fake", sizeof map.boss[i]->name); map.boss[i]->setSprites(graphics.getSprite("GaldovRight", true), graphics.getSprite("GaldovLeft", true), graphics.getSprite("GaldovLeft", true)); map.boss[i]->x = 9999; map.boss[i]->y = 9999; diff --git a/src/game.cpp b/src/game.cpp index 1e04687..8a442af 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -318,7 +318,7 @@ int gameover() Widget *widget = engine.getWidgetByName("gameOverNo"); char postfix[100]; snprintf(postfix, sizeof postfix, " (%d)", game.canContinue); - strncat(widget->label, postfix, sizeof widget->label); + strlcat(widget->label, postfix, sizeof widget->label); } while (true) @@ -575,7 +575,7 @@ int doGame() #if DEBUG Uint32 now; char fps[10]; - strncpy(fps, "fps", sizeof fps); + strlcpy(fps, "fps", sizeof fps); #endif engine.messageTime = -1; diff --git a/src/headers.h b/src/headers.h index f850159..faf0008 100755 --- a/src/headers.h +++ b/src/headers.h @@ -45,6 +45,11 @@ extern DECLSPEC int SDLCALL SDL_GetGamma(float *red, float *green, float *blue); #include #define _(string) gettext(string) +#if !defined(OpenBSD) && !defined(FreeBSD) +#define strlcat(dest, src, n) strncat((dest), (src), (n) - 1) +#define strlcpy(dest, src, n) do {strncpy((dest), (src), (n)); (dest)[(n) - 1] = 0;} while(false) +#endif + #include "defs.h" #include "CMath.h" diff --git a/src/hub.cpp b/src/hub.cpp index bcfe2e1..4233fa9 100644 --- a/src/hub.cpp +++ b/src/hub.cpp @@ -280,7 +280,7 @@ void createObjectivesPanel(const char *levelName) } else { - strncpy(string, _(data->value), sizeof string); + strlcpy(string, _(data->value), sizeof string); if (strlen(string) >= 25) { @@ -546,8 +546,8 @@ int doHub() int rtn = -1; - strncpy(level, "@none@", sizeof level); - strncpy(game.stageName, "@none@", sizeof game.stageName); + strlcpy(level, "@none@", sizeof level); + strlcpy(game.stageName, "@none@", sizeof game.stageName); bool showData = false; bool showStats = false; @@ -576,7 +576,7 @@ int doHub() if ((numberOfHubs == 0) && (gameData.completedWorld)) { game.setMapName("data/spaceStation"); - strncpy(game.stageName, "Space Station", sizeof game.stageName); + strlcpy(game.stageName, "Space Station", sizeof game.stageName); createObjectivesPanel("Space Station"); showData = true; showStats = showMIAs = false; diff --git a/src/loadSave.cpp b/src/loadSave.cpp index 7d2792c..d77c21f 100644 --- a/src/loadSave.cpp +++ b/src/loadSave.cpp @@ -42,14 +42,14 @@ void initSaveSlots() if (!fp) { - strncpy(string, "%.2d - %s", sizeof string); + strlcpy(string, "%.2d - %s", sizeof string); snprintf(engine.saveSlot[i], sizeof engine.saveSlot[i], string, (i + 1), _("Empty")); } else { if (fread(&tempGame, sizeof(Game), 1, fp) != 1) { - strncpy(string, "%.2d - %s", sizeof string); + strlcpy(string, "%.2d - %s", sizeof string); snprintf(engine.saveSlot[i], sizeof engine.saveSlot[i], string, (i + 1), _("Corrupt Save Data")); } else @@ -163,7 +163,7 @@ bool loadGame(int slot) fgets(line, 1024, fp); sscanf(line, "%[^\n\r]", string[0]); - strncpy(stageName, string[0], sizeof stageName); + strlcpy(stageName, string[0], sizeof stageName); if (strcmp(stageName, "@EOF@") == 0) { @@ -183,7 +183,7 @@ bool loadGame(int slot) fgets(line, 1024, fp); - strncpy(persistData->data, line, sizeof persistData->data); + strlcpy(persistData->data, line, sizeof persistData->data); //debug(("Read %d: %s", i, persistData->data)); @@ -237,7 +237,7 @@ int confirmSave() for (int i = 0 ; i < 5 ; i++) { snprintf(widgetName, sizeof widgetName, "slot%d", i + 1); - strncpy(engine.getWidgetByName(widgetName)->label, engine.saveSlot[i], sizeof engine.getWidgetByName(widgetName)->label); + strlcpy(engine.getWidgetByName(widgetName)->label, engine.saveSlot[i], sizeof engine.getWidgetByName(widgetName)->label); } engine.highlightWidget("slot1"); diff --git a/src/main.cpp b/src/main.cpp index 6d4e99f..bea1e30 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -126,8 +126,8 @@ int main(int argc, char *argv[]) else if (strcmp(argv[i], "-mono") == 0) engine.useAudio = 1; else if (strcmp(argv[i], "-version") == 0) showVersion(); else if (strcmp(argv[i], "--help") == 0) showHelp(); - else if (strcmp(argv[i], "-record") == 0) {recordMode = REPLAY_MODE::RECORD; strncpy(replayData.filename, argv[++i], sizeof replayData.filename);} - else if (strcmp(argv[i], "-playback") == 0) {recordMode = REPLAY_MODE::PLAYBACK; strncpy(replayData.filename, argv[++i], sizeof replayData.filename);} + else if (strcmp(argv[i], "-record") == 0) {recordMode = REPLAY_MODE::RECORD; strlcpy(replayData.filename, argv[++i], sizeof replayData.filename);} + else if (strcmp(argv[i], "-playback") == 0) {recordMode = REPLAY_MODE::PLAYBACK; strlcpy(replayData.filename, argv[++i], sizeof replayData.filename);} else if (strcmp(argv[i], "-map") == 0) {game.setMapName(argv[++i]); requiredSection = SECTION_GAME;} else if (strcmp(argv[i], "-listmaps") == 0) listMaps(); else if (strcmp(argv[i], "-credits") == 0) requiredSection = SECTION_CREDITS; @@ -148,7 +148,7 @@ int main(int argc, char *argv[]) case REPLAY_MODE::RECORD: requiredSection = SECTION_GAME; - strncpy(replayData.header.map, game.mapName, sizeof replayData.header.map); + strlcpy(replayData.header.map, game.mapName, sizeof replayData.header.map); replayData.header.skill = game.skill = 3; replayData.setMode(REPLAY_MODE::RECORD); break; diff --git a/src/mapEditor.cpp b/src/mapEditor.cpp index 2f3bc98..240553c 100644 --- a/src/mapEditor.cpp +++ b/src/mapEditor.cpp @@ -268,7 +268,7 @@ void collectMapData() if (!strstr(string, " ENEMY \"")) { str = new String; - strncpy(str->string, string, sizeof str->string); + strlcpy(str->string, string, sizeof str->string); stringTail->next = str; stringTail = str; } diff --git a/src/mias.cpp b/src/mias.cpp index 2415a3c..18345f4 100644 --- a/src/mias.cpp +++ b/src/mias.cpp @@ -22,20 +22,20 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. void initMIAPhrases() { - strncpy(mia_scared[0], "help me...", sizeof mia_scared[0]); - strncpy(mia_scared[1], "i don't wanna die...", sizeof mia_scared[1]); - strncpy(mia_scared[2], "please... someone help...", sizeof mia_scared[2]); - strncpy(mia_scared[3], "i... i'm scared...", sizeof mia_scared[3]); - strncpy(mia_scared[4], "i wanna go home...", sizeof mia_scared[4]); - strncpy(mia_scared[5], "what was that?!", sizeof mia_scared[5]); - strncpy(mia_scared[6], "i don't like it here...", sizeof mia_scared[6]); + strlcpy(mia_scared[0], "help me...", sizeof mia_scared[0]); + strlcpy(mia_scared[1], "i don't wanna die...", sizeof mia_scared[1]); + strlcpy(mia_scared[2], "please... someone help...", sizeof mia_scared[2]); + strlcpy(mia_scared[3], "i... i'm scared...", sizeof mia_scared[3]); + strlcpy(mia_scared[4], "i wanna go home...", sizeof mia_scared[4]); + strlcpy(mia_scared[5], "what was that?!", sizeof mia_scared[5]); + strlcpy(mia_scared[6], "i don't like it here...", sizeof mia_scared[6]); } void addMIA(const char *name, int x, int y, int type) { Entity *mia = new Entity(); - strncpy(mia->name, name, sizeof mia->name); + strlcpy(mia->name, name, sizeof mia->name); mia->id = type; mia->baseThink = 60; mia->health = 180; diff --git a/src/player.cpp b/src/player.cpp index 151b07c..f923b6e 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -66,7 +66,7 @@ void presentPlayerMedal(const char *tname) // Copy the input, so that threading // doesn't trip us up! char *data = new char[128]; - strncpy(data, tname, sizeof data); + strlcpy(data, tname, sizeof data); SDL_Thread *thread = SDL_CreateThread(medalWorker, (void*)data); diff --git a/src/switches.cpp b/src/switches.cpp index 96d74d9..a02ae24 100644 --- a/src/switches.cpp +++ b/src/switches.cpp @@ -65,11 +65,11 @@ void checkSwitchContact(Entity *ent) if (swt->type == SWT_PRESSURE) { - strncpy(swt->requiredObjectName, "@none@", sizeof swt->requiredObjectName); + strlcpy(swt->requiredObjectName, "@none@", sizeof swt->requiredObjectName); } checkObjectives(swt->name, true); - strncpy(swt->name, "Switch", sizeof swt->name); + strlcpy(swt->name, "Switch", sizeof swt->name); } else { diff --git a/src/tankBoss.cpp b/src/tankBoss.cpp index 6db897f..522e67a 100644 --- a/src/tankBoss.cpp +++ b/src/tankBoss.cpp @@ -382,7 +382,7 @@ void tankBossMGInit() debug(("tankBossMGInit\n")); map.boss[0] = new Boss(); - strncpy(map.boss[0]->name, "BioMech Tank V1.1", sizeof map.boss[0]->name); + strlcpy(map.boss[0]->name, "BioMech Tank V1.1", sizeof map.boss[0]->name); map.boss[0]->health = 65 * game.skill; map.boss[0]->maxHealth = 65 * game.skill; map.boss[0]->setSprites(graphics.getSprite("BlobTankCannonRight", true), graphics.getSprite("BlobTankCannonLeft", true), graphics.getSprite("BlobTankCannonLeft", true)); @@ -604,7 +604,7 @@ void tankBossGLInit() debug(("tankBossGLInit\n")); map.boss[1] = new Boss(); - strncpy(map.boss[1]->name, "BioMech Tank V2.6", sizeof map.boss[1]->name); + strlcpy(map.boss[1]->name, "BioMech Tank V2.6", sizeof map.boss[1]->name); map.boss[1]->health = 65 * game.skill; map.boss[1]->maxHealth = 65 * game.skill; map.boss[1]->setSprites(graphics.getSprite("BlobTankGrenadeRight", true), graphics.getSprite("BlobTankGrenadeLeft", true), graphics.getSprite("BlobTankGrenadeLeft", true)); diff --git a/src/title.cpp b/src/title.cpp index b022cdb..c4e0619 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -118,7 +118,7 @@ void setupSaveWidgets() for (int i = 0 ; i < 5 ; i++) { snprintf(widgetName, sizeof widgetName, "save%d", i + 1); - strncpy(engine.getWidgetByName(widgetName)->label, engine.saveSlot[i], sizeof engine.getWidgetByName(widgetName)->label); + strlcpy(engine.getWidgetByName(widgetName)->label, engine.saveSlot[i], sizeof engine.getWidgetByName(widgetName)->label); if ((strstr(engine.saveSlot[i], _("Empty"))) || (strstr(engine.saveSlot[i], _("Corrupt")))) { @@ -126,7 +126,7 @@ void setupSaveWidgets() } snprintf(widgetName, sizeof widgetName, "slot%d", i + 1); - strncpy(engine.getWidgetByName(widgetName)->label, engine.saveSlot[i], sizeof engine.getWidgetByName(widgetName)->label); + strlcpy(engine.getWidgetByName(widgetName)->label, engine.saveSlot[i], sizeof engine.getWidgetByName(widgetName)->label); } } @@ -145,7 +145,7 @@ void loadTitleWidgets() setupSaveWidgets(); Widget *widget = engine.getWidgetByName("labelManual"); - strncpy(widget->label, GAMEPLAYMANUAL, sizeof widget->label); + strlcpy(widget->label, GAMEPLAYMANUAL, sizeof widget->label); showTitleWidgets(); }