]> de.git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
refine edge cases handling to keynums/keystrings,
authorNaitLee <naitli@foxmail.com>
Mon, 30 Jan 2023 18:14:58 +0000 (02:14 +0800)
committerNaitLee <naitli@foxmail.com>
Mon, 30 Jan 2023 18:14:58 +0000 (02:14 +0800)
use the new constant `TINYSTR_LEN` in place of `sizeof(tinystr)` leftovers

Signed-off-by: NaitLee <naitli@foxmail.com>
keys.c
menu.c
prvm_cmds.c
vid_sdl.c

diff --git a/keys.c b/keys.c
index 1d320ec7c14ea866d9e499701e23ccfbfeb9ba86..8049bb3b993ad7b637c4143d18fa8e9a69db1ecf 100644 (file)
--- 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 990d6db3e0e1c708b97d3527da7cb11f1267ffc8..8b41f28919ce8c1e0f48d0170618b857f660d668 100644 (file)
--- 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);
                }
 
index 1cf7e852b6f98b2743fe6e6c8bf11c9494d9595f..185ef53814e967e41e6257c487161edcf0c80b63 100644 (file)
@@ -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));
 }
 
 /*
index 528882a895daeb8f971e4c33320d2684ab516a24..315739ad6cc0d03f3713b75698fdd1c1c5039fcb 100644 (file)
--- 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;