Skip to content

Commit

Permalink
Use strncpy_or_assert to copy pilot name into rec
Browse files Browse the repository at this point in the history
  • Loading branch information
Nopey committed Jan 6, 2025
1 parent 99b68e8 commit 66ba523
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/game/scenes/arena.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "resources/languages.h"
#include "resources/sgmanager.h"
#include "utils/allocator.h"
#include "utils/c_string_util.h"
#include "utils/log.h"
#include "utils/random.h"
#include "video/video.h"
Expand Down Expand Up @@ -1538,9 +1539,7 @@ int arena_create(scene *scene) {
local->rec->pilots[i].info.color_1 = player->pilot->color_1;
local->rec->pilots[i].info.color_2 = player->pilot->color_2;
local->rec->pilots[i].info.color_3 = player->pilot->color_3;
// XXX ugly strncpy, implicit truncation, no nul termination guarantee, why are we even copying the pilot's
// name in here?
strncpy(local->rec->pilots[i].info.name, lang_get_offset(LangPilot, player->pilot->pilot_id), 18);
strncpy_or_assert(local->rec->pilots[i].info.name, lang_get_offset(LangPilot, player->pilot->pilot_id), 18);
}
local->rec->arena_id = scene->id - SCENE_ARENA0;
} else {
Expand Down
8 changes: 8 additions & 0 deletions src/utils/c_string_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ char *strncpy_or_truncate(char *dest, const char *src, size_t n) {
return ret;
}

char *strncpy_or_assert(char *dest, const char *src, size_t n) {
char *ret = strncpy(dest, src, n);
assert(ret[n - 1] == '\0' || !"truncating in strncpy!");
if(n > 0)
dest[n - 1] = '\0';
return ret;
}

char *omf_strdup_real(char const *s, char const *file, int line) {
assert(s != NULL);
size_t valid_range = strlen(s) + 1;
Expand Down
3 changes: 3 additions & 0 deletions src/utils/c_string_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
// strncpy() that guarantees null-termination.
char *strncpy_or_truncate(char *dest, const char *src, size_t n);

// strncpy() that guarantees null-termination, and asserts when truncating.
char *strncpy_or_assert(char *dest, const char *src, size_t n);

// strdup() that uses our allocator
char *omf_strdup_real(char const *s, char const *file, int line);
#define omf_strdup(s) omf_strdup_real((s), __FILE__, __LINE__)
Expand Down

0 comments on commit 66ba523

Please sign in to comment.