diff --git a/src/formats/animation.c b/src/formats/animation.c index f10b35624..799a7c254 100644 --- a/src/formats/animation.c +++ b/src/formats/animation.c @@ -6,6 +6,7 @@ #include "formats/error.h" #include "formats/sprite.h" #include "utils/allocator.h" +#include "utils/c_string_util.h" #include "utils/log.h" int sd_animation_create(sd_animation *ani) { @@ -64,7 +65,7 @@ int sd_animation_set_anim_string(sd_animation *ani, const char *str) { if(strlen(str) >= SD_ANIMATION_STRING_MAX) { return SD_INVALID_INPUT; } - strncpy(ani->anim_string, str, sizeof(ani->anim_string)); + strncpy_or_truncate(ani->anim_string, str, sizeof(ani->anim_string)); return SD_SUCCESS; } diff --git a/src/formats/bkanim.c b/src/formats/bkanim.c index 224c55110..4748bd14f 100644 --- a/src/formats/bkanim.c +++ b/src/formats/bkanim.c @@ -5,6 +5,7 @@ #include "formats/bkanim.h" #include "formats/error.h" #include "utils/allocator.h" +#include "utils/c_string_util.h" #include "utils/log.h" int sd_bk_anim_create(sd_bk_anim *bka) { @@ -153,6 +154,6 @@ int sd_bk_set_anim_string(sd_bk_anim *bka, const char *data) { if(strlen(data) >= SD_BK_FOOTER_STRING_MAX - 1) { return SD_INVALID_INPUT; } - strncpy(bka->footer_string, data, sizeof(bka->footer_string)); + strncpy_or_truncate(bka->footer_string, data, sizeof(bka->footer_string)); return SD_SUCCESS; } diff --git a/src/formats/chr.c b/src/formats/chr.c index 2582e9c0a..59f44ee0a 100644 --- a/src/formats/chr.c +++ b/src/formats/chr.c @@ -16,6 +16,7 @@ #include "resources/pathmanager.h" #include "resources/trnmanager.h" #include "utils/allocator.h" +#include "utils/c_string_util.h" #include "utils/log.h" int sd_chr_create(sd_chr_file *chr) { @@ -41,9 +42,9 @@ int sd_chr_from_trn(sd_chr_file *chr, sd_tournament_file *trn, sd_pilot *pilot) chr->pilot.enemies_inc_unranked = trn->enemy_count; chr->pilot.enemies_ex_unranked = ranked; chr->pilot.rank = ranked + 1; - strncpy(chr->pilot.trn_name, trn->filename, sizeof(chr->pilot.trn_name)); - strncpy(chr->pilot.trn_desc, trn->locales[0]->title, sizeof(chr->pilot.trn_desc)); - strncpy(chr->pilot.trn_image, trn->pic_file, sizeof(chr->pilot.trn_image)); + strncpy_or_truncate(chr->pilot.trn_name, trn->filename, sizeof(chr->pilot.trn_name)); + strncpy_or_truncate(chr->pilot.trn_desc, trn->locales[0]->title, sizeof(chr->pilot.trn_desc)); + strncpy_or_truncate(chr->pilot.trn_image, trn->pic_file, sizeof(chr->pilot.trn_image)); chr->photo = omf_calloc(1, sizeof(sd_sprite)); sd_sprite_copy(chr->photo, pilot->photo); return SD_SUCCESS; @@ -95,9 +96,7 @@ int sd_chr_load(sd_chr_file *chr, const char *filename) { if(trn_loaded) { for(int i = 0; i < 10; i++) { if(trn.locales[0]->end_texts[0][i]) { - chr->cutscene_text[i] = omf_calloc(1, strlen(trn.locales[0]->end_texts[0][i]) + 1); - strncpy(chr->cutscene_text[i], trn.locales[0]->end_texts[0][i], - strlen(trn.locales[0]->end_texts[0][i])); + chr->cutscene_text[i] = omf_strdup(trn.locales[0]->end_texts[0][i]); } } // TODO do something better here @@ -180,8 +179,7 @@ int sd_chr_load(sd_chr_file *chr, const char *filename) { memread_buf(mr, chr->enemies[i]->unknown, 25); for(int m = 0; m < 10; m++) { if(trn_loaded && trn.enemies[i]->quotes[m]) { - chr->enemies[i]->pilot.quotes[m] = omf_calloc(1, strlen(trn.enemies[i]->quotes[m]) + 1); - strncpy(chr->enemies[i]->pilot.quotes[m], trn.enemies[i]->quotes[m], strlen(trn.enemies[i]->quotes[m])); + chr->enemies[i]->pilot.quotes[m] = omf_strdup(trn.enemies[i]->quotes[m]); } } } diff --git a/src/formats/language.c b/src/formats/language.c index 133bbe221..563d66106 100644 --- a/src/formats/language.c +++ b/src/formats/language.c @@ -115,7 +115,7 @@ void sd_language_append(sd_language *language, const char *description, const ch language->count++; language->strings = omf_realloc(language->strings, language->count * sizeof(sd_lang_string)); - strncpy(language->strings[language->count - 1].description, description, 32); + strncpy_or_truncate(language->strings[language->count - 1].description, description, 32); language->strings[language->count - 1].data = omf_strdup(data); } diff --git a/src/formats/move.c b/src/formats/move.c index 24683c8f7..9e2fbfc3d 100644 --- a/src/formats/move.c +++ b/src/formats/move.c @@ -5,6 +5,7 @@ #include "formats/error.h" #include "formats/move.h" #include "utils/allocator.h" +#include "utils/c_string_util.h" #include "utils/log.h" int sd_move_create(sd_move *move) { @@ -208,7 +209,7 @@ int sd_move_set_footer_string(sd_move *move, const char *str) { if(strlen(str) >= SD_MOVE_FOOTER_STRING_MAX - 1) { return SD_INVALID_INPUT; } - strncpy(move->footer_string, str, sizeof(move->footer_string)); + strncpy_or_truncate(move->footer_string, str, sizeof(move->footer_string)); return SD_SUCCESS; } @@ -216,6 +217,6 @@ int sd_move_set_move_string(sd_move *move, const char *str) { if(strlen(str) >= SD_MOVE_STRING_MAX - 1) { return SD_INVALID_INPUT; } - strncpy(move->move_string, str, sizeof(move->move_string)); + strncpy_or_truncate(move->move_string, str, sizeof(move->move_string)); return SD_SUCCESS; } diff --git a/src/formats/tournament.c b/src/formats/tournament.c index f62a5c01b..63568eede 100644 --- a/src/formats/tournament.c +++ b/src/formats/tournament.c @@ -8,6 +8,7 @@ #include "formats/tournament.h" #include "resources/pathmanager.h" #include "utils/allocator.h" +#include "utils/c_string_util.h" int sd_tournament_create(sd_tournament_file *trn) { if(trn == NULL) { @@ -94,9 +95,9 @@ int sd_tournament_load(sd_tournament_file *trn, const char *filename) { char *justfile = strrchr(filename, pm_path_sep); if(justfile == NULL) { - strncpy(trn->filename, filename, sizeof(trn->filename)); + strncpy_or_truncate(trn->filename, filename, sizeof(trn->filename)); } else { - strncpy(trn->filename, justfile + 1, sizeof(trn->filename)); + strncpy_or_truncate(trn->filename, justfile + 1, sizeof(trn->filename)); } trn->enemy_count = enemy_count; diff --git a/src/game/scenes/lobby.c b/src/game/scenes/lobby.c index 1243f34a6..489b5b8fd 100644 --- a/src/game/scenes/lobby.c +++ b/src/game/scenes/lobby.c @@ -565,7 +565,7 @@ void lobby_entered_name(component *c, void *userdata) { DEBUG("remote peer connect id %d", event.peer->connectID); event.peer->data = nat; - strncpy(local->name, textinput_value(c), sizeof(local->name)); + strncpy_or_truncate(local->name, textinput_value(c), sizeof(local->name)); char version[15]; // TODO support git version when not on a tag @@ -901,13 +901,13 @@ void lobby_tick(scene *scene, int paused) { case PACKET_YELL: { log_event log; log.color = YELL_COLOR; - strncpy(log.msg, (char *)event.packet->data + 1, sizeof(log.msg)); + strncpy_or_truncate(log.msg, (char *)event.packet->data + 1, sizeof(log.msg)); list_append(&local->log, &log, sizeof(log)); } break; case PACKET_WHISPER: { log_event log; log.color = WHISPER_COLOR; - strncpy(log.msg, (char *)event.packet->data + 1, sizeof(log.msg)); + strncpy_or_truncate(log.msg, (char *)event.packet->data + 1, sizeof(log.msg)); list_append(&local->log, &log, sizeof(log)); } break; case PACKET_DISCONNECT: { diff --git a/src/game/scenes/scoreboard.c b/src/game/scenes/scoreboard.c index 2cda064b1..59be4f55f 100644 --- a/src/game/scenes/scoreboard.c +++ b/src/game/scenes/scoreboard.c @@ -10,6 +10,7 @@ #include "game/utils/settings.h" #include "resources/scores.h" #include "utils/allocator.h" +#include "utils/c_string_util.h" #include "utils/log.h" #include "video/surface.h" #include "video/vga_state.h" @@ -41,7 +42,7 @@ void handle_scoreboard_save(scoreboard_local *local) { if(!strlen(name)) { return; } - strncpy(local->pending_data.name, name, sizeof(local->pending_data.name)); + strncpy_or_truncate(local->pending_data.name, name, sizeof(local->pending_data.name)); for(int i = 0; i < 20; i++) { unsigned int ex_score = local->data.entries[local->page][i].score; unsigned int my_score = local->pending_data.score;