Skip to content

Commit

Permalink
formats: Fix strncpy termination safety issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mrannanj committed Dec 10, 2024
1 parent 6168f39 commit ead380a
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/formats/animation.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion src/formats/bkanim.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
14 changes: 6 additions & 8 deletions src/formats/chr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/formats/language.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
5 changes: 3 additions & 2 deletions src/formats/move.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -208,14 +209,14 @@ 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;
}

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;
}
5 changes: 3 additions & 2 deletions src/formats/tournament.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions src/game/scenes/lobby.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: {
Expand Down
3 changes: 2 additions & 1 deletion src/game/scenes/scoreboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ead380a

Please sign in to comment.