Skip to content

Commit abfb2d9

Browse files
committed
Use strncpy_or_assert to copy pilot name into rec
1 parent 99b68e8 commit abfb2d9

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

src/game/scenes/arena.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "utils/allocator.h"
3535
#include "utils/log.h"
3636
#include "utils/random.h"
37+
#include "utils/c_string_util.h"
3738
#include "video/video.h"
3839

3940
#define TEXT_COLOR 0xC7
@@ -1538,9 +1539,7 @@ int arena_create(scene *scene) {
15381539
local->rec->pilots[i].info.color_1 = player->pilot->color_1;
15391540
local->rec->pilots[i].info.color_2 = player->pilot->color_2;
15401541
local->rec->pilots[i].info.color_3 = player->pilot->color_3;
1541-
// XXX ugly strncpy, implicit truncation, no nul termination guarantee, why are we even copying the pilot's
1542-
// name in here?
1543-
strncpy(local->rec->pilots[i].info.name, lang_get_offset(LangPilot, player->pilot->pilot_id), 18);
1542+
strncpy_or_assert(local->rec->pilots[i].info.name, lang_get_offset(LangPilot, player->pilot->pilot_id), 18);
15441543
}
15451544
local->rec->arena_id = scene->id - SCENE_ARENA0;
15461545
} else {

src/utils/c_string_util.c

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ char *strncpy_or_truncate(char *dest, const char *src, size_t n) {
1010
return ret;
1111
}
1212

13+
char *strncpy_or_assert(char *dest, const char *src, size_t n) {
14+
char *ret = strncpy(dest, src, n);
15+
assert(ret[n - 1] == '\0' || !"truncating in strncpy!");
16+
if(n > 0)
17+
dest[n - 1] = '\0';
18+
return ret;
19+
}
20+
1321
char *omf_strdup_real(char const *s, char const *file, int line) {
1422
assert(s != NULL);
1523
size_t valid_range = strlen(s) + 1;

src/utils/c_string_util.h

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
// strncpy() that guarantees null-termination.
77
char *strncpy_or_truncate(char *dest, const char *src, size_t n);
88

9+
// strncpy() that guarantees null-termination, and asserts when truncating.
10+
char *strncpy_or_assert(char *dest, const char *src, size_t n);
11+
912
// strdup() that uses our allocator
1013
char *omf_strdup_real(char const *s, char const *file, int line);
1114
#define omf_strdup(s) omf_strdup_real((s), __FILE__, __LINE__)

0 commit comments

Comments
 (0)