Fix bounds checking of course/act name replacement

This commit is contained in:
MysterD 2023-11-07 15:19:45 -08:00 committed by Agent X
parent cece41f1a8
commit 1f54996d47
6 changed files with 27 additions and 16 deletions

View file

@ -2804,7 +2804,7 @@ void render_pause_castle_main_strings(s16 x, s16 y) {
#endif
u8 courseNum = gDialogLineNum + 1;
const u8 *courseName = get_level_name_sm64(courseNum, courseNum < COURSE_COUNT ? gCourseNumToLevelNumTable[courseNum] : LEVEL_NONE, 1, 1);
const u8 *courseName = get_level_name_sm64(courseNum, get_level_num_from_course_num(courseNum), 1, 1);
u8 strVal[8];
s16 starNum = gDialogLineNum;
@ -2999,7 +2999,7 @@ void render_pause_castle_main_strings_extended(s16 x, s16 y) {
gSPDisplayList(gDisplayListHead++, dl_ia_text_begin);
gDPSetEnvColor(gDisplayListHead++, 255, 255, 255, gDialogTextAlpha);
const u8 *courseName = get_level_name_sm64(gDialogLineNum, gDialogLineNum < COURSE_COUNT ? gCourseNumToLevelNumTable[gDialogLineNum] : LEVEL_NONE, 1, 1);
const u8 *courseName = get_level_name_sm64(gDialogLineNum, get_level_num_from_course_num(gDialogLineNum), 1, 1);
// Main courses (0-14)
if (gDialogLineNum < COURSE_STAGES_COUNT) {

View file

@ -261,17 +261,18 @@ const char *get_level_name(s16 courseNum, s16 levelNum, s16 areaIndex) {
const char *get_star_name_ascii(s16 courseNum, s16 starNum, s16 charCase) {
static char output[256];
if (starNum >= 0 && starNum <= 6 &&
courseNum >= 0 && courseNum <= COURSE_MAX &&
gReplacedActNameTable[courseNum]->actName && gReplacedActNameTable[courseNum]->actName[starNum - 1].isModified) {
snprintf(output, 256, "%s", gReplacedActNameTable[courseNum]->actName[starNum - 1].name);
s16 starIndex = starNum - 1;
if (starIndex >= 0 && starIndex < MAX_ACTS &&
courseNum >= 0 && courseNum < COURSE_END &&
gReplacedActNameTable[courseNum]->actName && gReplacedActNameTable[courseNum]->actName[starIndex].isModified) {
snprintf(output, 256, "%s", gReplacedActNameTable[courseNum]->actName[starIndex].name);
}
// Main courses: BOB to RR
else if (COURSE_IS_MAIN_COURSE(courseNum)) {
if (starNum >= 1 && starNum <= 6) {
if (starIndex >= 0 && starIndex < MAX_ACTS) {
void **actNameTable = get_act_name_table();
const u8 *starName = segmented_to_virtual(actNameTable[(courseNum - COURSE_BOB) * 6 + (starNum - 1)]);
const u8 *starName = segmented_to_virtual(actNameTable[(courseNum - COURSE_BOB) * MAX_ACTS + starIndex]);
convert_string_sm64_to_ascii(output, starName);
charCase = MIN(charCase, 0); // Don't need to capitalize vanilla act names
} else if (starNum == 7) {

View file

@ -65,6 +65,13 @@ s8 gCourseNumToLevelNumTable[] = {
STATIC_ASSERT(ARRAY_COUNT(gLevelToCourseNumTable) == LEVEL_COUNT - 1,
"change this array if you are adding levels");
s8 get_level_num_from_course_num(s16 courseNum) {
if (courseNum < 0 || courseNum >= COURSE_COUNT) {
return LEVEL_NONE;
}
return gCourseNumToLevelNumTable[courseNum];
}
s8 get_level_course_num(s16 levelNum) {
if (levelNum >= CUSTOM_LEVEL_NUM_START) {
struct CustomLevelInfo* info = smlua_level_util_get_info(levelNum);

View file

@ -129,6 +129,7 @@ extern struct WarpCheckpoint gWarpCheckpoint;
extern s8 gMainMenuDataModified;
extern s8 gSaveFileModified;
s8 get_level_num_from_course_num(s16 courseNum);
s8 get_level_course_num(s16 levelNum);
void touch_coin_score_age(s32 fileIndex, s32 courseIndex);
void save_file_do_save(s32 fileIndex, s8 forceSave);

View file

@ -44,9 +44,9 @@ AT_STARTUP static void smlua_text_utils_init() {
// Individual acts
if (COURSE_IS_MAIN_COURSE(courseNum)) {
courseActNames->actName = calloc(6, sizeof(struct ActName));
for (s16 actNum = 0; actNum < 6; actNum++) {
const u8 *starName = segmented_to_virtual(actNameTbl[courseNum * 6 + actNum]);
courseActNames->actName = calloc(MAX_ACTS, sizeof(struct ActName));
for (s16 actNum = 0; actNum < MAX_ACTS; actNum++) {
const u8 *starName = segmented_to_virtual(actNameTbl[courseNum * MAX_ACTS + actNum]);
convert_string_sm64_to_ascii(actBuffer, starName);
snprintf(courseActNames->actName[actNum].name, 50, "%s", actBuffer);
snprintf(courseActNames->actName[actNum].orig, 50, "%s", actBuffer);
@ -136,7 +136,7 @@ void smlua_text_utils_reset_all(void) {
// Individual acts
if (COURSE_IS_MAIN_COURSE(courseNum)) {
for (s16 actNum = 0; actNum < 6; actNum++) {
for (s16 actNum = 0; actNum < MAX_ACTS; actNum++) {
snprintf(courseActNames->actName[actNum].name, 50, "%s", courseActNames->actName[actNum].orig);
courseActNames->actName[actNum].isModified = false;
}
@ -226,7 +226,7 @@ void smlua_text_utils_course_name_reset(s16 courseNum) {
}
void smlua_text_utils_act_name_replace(s16 courseNum, u8 actNum, const char* name) {
if (INVALID_COURSE_NUM(courseNum) || actNum > 7) { return; }
if (INVALID_COURSE_NUM(courseNum) || actNum >= MAX_ACTS) { return; }
struct CourseName* courseActNames = gReplacedActNameTable[courseNum];
@ -235,19 +235,19 @@ void smlua_text_utils_act_name_replace(s16 courseNum, u8 actNum, const char* nam
}
const char* smlua_text_utils_act_name_get(s16 courseNum, u8 actNum) {
if (INVALID_COURSE_NUM(courseNum) || actNum > 7) { return NULL; }
if (INVALID_COURSE_NUM(courseNum) || actNum >= MAX_ACTS) { return NULL; }
return gReplacedActNameTable[courseNum]->actName[actNum].name;
}
bool smlua_text_utils_act_name_is_modified(s16 courseNum, u8 actNum) {
if (INVALID_COURSE_NUM(courseNum) || actNum > 7) { return false; }
if (INVALID_COURSE_NUM(courseNum) || actNum >= MAX_ACTS) { return false; }
return gReplacedActNameTable[courseNum]->actName[actNum].isModified;
}
void smlua_text_utils_act_name_reset(s16 courseNum, u8 actNum) {
if (INVALID_COURSE_NUM(courseNum) || actNum > 7) { return; }
if (INVALID_COURSE_NUM(courseNum) || actNum >= MAX_ACTS) { return; }
struct CourseName* courseActNames = gReplacedActNameTable[courseNum];
snprintf(courseActNames->actName[actNum].name, 50, "%s", courseActNames->actName[actNum].orig);

View file

@ -4,6 +4,8 @@
#include "types.h"
#include "dialog_ids.h"
#define MAX_ACTS 6
struct ActName {
char name[256];
char orig[256];