From 871cfdae9e48274eacb720e2423b9d8b3818bee9 Mon Sep 17 00:00:00 2001 From: NaitLee Date: Tue, 31 Jan 2023 02:14:58 +0800 Subject: [PATCH] refine edge cases handling to keynums/keystrings, use the new constant `TINYSTR_LEN` in place of `sizeof(tinystr)` leftovers Signed-off-by: NaitLee --- keys.c | 13 +++++++------ menu.c | 4 ++-- prvm_cmds.c | 2 +- vid_sdl.c | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/keys.c b/keys.c index 1d320ec7..8049bb3b 100644 --- a/keys.c +++ b/keys.c @@ -1364,9 +1364,10 @@ Key_StringToKeynum (const char *str) return kn->keynum; } - // non-ascii keys are Unicode codepoints, so give the character + // non-ascii keys are Unicode codepoints, so give the character if it's valid; + // error message have more than one character, don't allow it ch = u8_getnchar(str, &str, 3); - return ch == 0 ? -1 : (int)ch; + return (ch == 0 || *str != 0) ? -1 : (int)ch; } /* @@ -1597,9 +1598,9 @@ Key_PrintBindList(int j) { Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\", false); if (j == 0) - Con_Printf("^2%s ^7= \"%s\"\n", Key_KeynumToString (i, tinystr, sizeof(tinystr)), bindbuf); + Con_Printf("^2%s ^7= \"%s\"\n", Key_KeynumToString (i, tinystr, TINYSTR_LEN), bindbuf); else - Con_Printf("^3bindmap %d: ^2%s ^7= \"%s\"\n", j, Key_KeynumToString (i, tinystr, sizeof(tinystr)), bindbuf); + Con_Printf("^3bindmap %d: ^2%s ^7= \"%s\"\n", j, Key_KeynumToString (i, tinystr, TINYSTR_LEN), bindbuf); } } } @@ -1694,9 +1695,9 @@ Key_WriteBindings (qfile_t *f) { Cmd_QuoteString(bindbuf, sizeof(bindbuf), p, "\"\\", false); // don't need to escape $ because cvars are not expanded inside bind if (j == 0) - FS_Printf(f, "bind %s \"%s\"\n", Key_KeynumToString (i, tinystr, sizeof(tinystr)), bindbuf); + FS_Printf(f, "bind %s \"%s\"\n", Key_KeynumToString (i, tinystr, TINYSTR_LEN), bindbuf); else - FS_Printf(f, "in_bind %d %s \"%s\"\n", j, Key_KeynumToString (i, tinystr, sizeof(tinystr)), bindbuf); + FS_Printf(f, "in_bind %d %s \"%s\"\n", j, Key_KeynumToString (i, tinystr, TINYSTR_LEN), bindbuf); } } } diff --git a/menu.c b/menu.c index 990d6db3..8b41f289 100644 --- a/menu.c +++ b/menu.c @@ -2651,7 +2651,7 @@ static void M_Keys_Draw (void) { if (j > 0) strlcat(keystring, " or ", sizeof(keystring)); - strlcat(keystring, Key_KeynumToString (keys[j], tinystr, sizeof(tinystr)), sizeof(keystring)); + strlcat(keystring, Key_KeynumToString (keys[j], tinystr, TINYSTR_LEN), sizeof(keystring)); } } } @@ -2680,7 +2680,7 @@ static void M_Keys_Key(cmd_state_t *cmd, int k, int ascii) } else //if (k != '`') { - dpsnprintf(line, sizeof(line), "bind \"%s\" \"%s\"\n", Key_KeynumToString(k, tinystr, sizeof(tinystr)), bindnames[keys_cursor][0]); + dpsnprintf(line, sizeof(line), "bind \"%s\" \"%s\"\n", Key_KeynumToString(k, tinystr, TINYSTR_LEN), bindnames[keys_cursor][0]); Cbuf_InsertText (cmd, line); } diff --git a/prvm_cmds.c b/prvm_cmds.c index 1cf7e852..185ef538 100644 --- a/prvm_cmds.c +++ b/prvm_cmds.c @@ -3271,7 +3271,7 @@ void VM_keynumtostring (prvm_prog_t *prog) char tinystr[TINYSTR_LEN]; VM_SAFEPARMCOUNT(1, VM_keynumtostring); - PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0), tinystr, sizeof(tinystr))); + PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(prog, Key_KeynumToString((int)PRVM_G_FLOAT(OFS_PARM0), tinystr, TINYSTR_LEN)); } /* diff --git a/vid_sdl.c b/vid_sdl.c index 528882a8..315739ad 100644 --- a/vid_sdl.c +++ b/vid_sdl.c @@ -101,7 +101,7 @@ static int MapKey( unsigned int sdlkey ) switch(sdlkey) { // sdlkey can be Unicode codepoint for non-ascii keys, which are valid - default: return sdlkey; + default: return sdlkey & SDLK_SCANCODE_MASK ? 0 : sdlkey; // case SDLK_UNKNOWN: return K_UNKNOWN; case SDLK_RETURN: return K_ENTER; case SDLK_ESCAPE: return K_ESCAPE; -- 2.39.2