]> git.xonotic.org Git - xonotic/darkplaces.git/commitdiff
fix whitespace handling:
authordivverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Sun, 28 Dec 2008 18:47:42 +0000 (18:47 +0000)
committerdivverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Sun, 28 Dec 2008 18:47:42 +0000 (18:47 +0000)
treat ONLY the following characters as whitespace:
NUL, TAB, LF, CR, SPC
Alternatively, there is commented out macro code in quakedef.h to accept any chars in 0..32 as whitespace.
Previously: 0..32 and 128..255 (due to signed char)

git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@8606 d7cf8633-e32d-0410-b094-e92efae38249

12 files changed:
cl_main.c
cl_parse.c
cmd.c
common.c
console.c
host_cmd.c
netconn.c
prvm_cmds.c
prvm_edict.c
quakedef.h
sys_linux.c
sys_win.c

index 75e3bd8533504445153cb9353c003d6d7da929b3..7beb895af01f7198034257c8580c00e19aa5711e 100644 (file)
--- a/cl_main.c
+++ b/cl_main.c
@@ -209,7 +209,7 @@ void CL_SetInfo(const char *key, const char *value, qboolean send, qboolean allo
        if (!allowmodel && (!strcasecmp(key, "pmodel") || !strcasecmp(key, "emodel")))
                fail = true;
        for (i = 0;key[i];i++)
-               if (key[i] <= ' ' || key[i] == '\"')
+               if (ISWHITESPACE(key[i]) || key[i] == '\"')
                        fail = true;
        for (i = 0;value[i];i++)
                if (value[i] == '\r' || value[i] == '\n' || value[i] == '\"')
@@ -2097,10 +2097,10 @@ void CL_Locs_Reload_f(void)
                if (text < textend)
                        text++;
                // trim trailing whitespace
-               while (lineend > linestart && lineend[-1] <= ' ')
+               while (lineend > linestart && ISWHITESPACE(lineend[-1]))
                        lineend--;
                // trim leading whitespace
-               while (linestart < lineend && *linestart <= ' ')
+               while (linestart < lineend && ISWHITESPACE(*linestart))
                        linestart++;
                // check if this is a comment
                if (linestart + 2 <= lineend && !strncmp(linestart, "//", 2))
@@ -2117,7 +2117,7 @@ void CL_Locs_Reload_f(void)
                        else
                                maxs[i - 3] = atof(linetext);
                        // now advance past the number
-                       while (linetext < lineend && *linetext > ' ' && *linetext != ',')
+                       while (linetext < lineend && !ISWHITESPACE(*linetext) && *linetext != ',')
                                linetext++;
                        // advance through whitespace
                        if (linetext < lineend)
@@ -2128,10 +2128,10 @@ void CL_Locs_Reload_f(void)
                                        limit = 6;
                                        // note: comma can be followed by whitespace
                                }
-                               if (*linetext <= ' ')
+                               if (ISWHITESPACE(*linetext))
                                {
                                        // skip whitespace
-                                       while (linetext < lineend && *linetext <= ' ')
+                                       while (linetext < lineend && ISWHITESPACE(*linetext))
                                                linetext++;
                                }
                        }
index 77ffee399c4fedeb0407fa13e89ba42c6244104b..d6b70ac250c72a93e6210bc714890b903c05bcc0 100644 (file)
@@ -2716,7 +2716,7 @@ static void CL_IPLog_Load(void)
                        text++;
                if (line[0] == '/' && line[1] == '/')
                        continue; // skip comments if anyone happens to add them
-               for (i = 0;i < len && line[i] > ' ';i++)
+               for (i = 0;i < len && !ISWHITESPACE(line[i]);i++)
                        address[i] = line[i];
                address[i] = 0;
                // skip exactly one space character
diff --git a/cmd.c b/cmd.c
index c9e5cb7b81df325bb915fecb2339c044069225bd..73f55456e1cb393df9737e3c7084b0d2496dcad8 100644 (file)
--- a/cmd.c
+++ b/cmd.c
@@ -712,7 +712,7 @@ static const char *Cmd_GetDirectCvarValue(const char *varname, cmdalias_t *alias
                                                *is_multiple = true;
 
                                        // kill pre-argument whitespace
-                                       for (;*p && *p <= ' ';p++)
+                                       for (;*p && ISWHITESPACE(*p);p++)
                                                ;
 
                                        return p;
@@ -1143,7 +1143,7 @@ static void Cmd_TokenizeString (const char *text)
        while (1)
        {
                // skip whitespace up to a /n
-               while (*text && *text <= ' ' && *text != '\r' && *text != '\n')
+               while (*text && ISWHITESPACE(*text) && *text != '\r' && *text != '\n')
                        text++;
 
                // line endings:
index e0cffe48120f4790c34348817b8f509a3fd87321..aac01fba3709b022bdb7ce0b2c8dc1a6ec4a9f3b 100644 (file)
--- a/common.c
+++ b/common.c
@@ -643,7 +643,7 @@ void Com_HexDumpToConsole(const unsigned char *data, int size)
                                        *cur++ = STRING_COLOR_TAG;
                                        *cur++ = STRING_COLOR_TAG;
                                }
-                               else if (d[j] >= ' ')
+                               else if (d[j] >= (unsigned char) ' ')
                                        *cur++ = d[j];
                                else
                                        *cur++ = '.';
@@ -996,7 +996,7 @@ skipwhite:
        // UNIX: \n
        // Mac: \r
        // Windows: \r\n
-       for (;*data <= ' ' && ((*data != '\n' && *data != '\r') || !returnnewline);data++)
+       for (;ISWHITESPACE(*data) && ((*data != '\n' && *data != '\r') || !returnnewline);data++)
        {
                if (*data == 0)
                {
@@ -1072,7 +1072,7 @@ skipwhite:
        else
        {
                // regular word
-               for (;*data > ' ';data++)
+               for (;!ISWHITESPACE(*data);data++)
                        if (len < (int)sizeof(com_token) - 1)
                                com_token[len++] = *data;
                com_token[len] = 0;
@@ -1109,7 +1109,7 @@ skipwhite:
        // UNIX: \n
        // Mac: \r
        // Windows: \r\n
-       for (;*data <= ' ' && ((*data != '\n' && *data != '\r') || !returnnewline);data++)
+       for (;ISWHITESPACE(*data) && ((*data != '\n' && *data != '\r') || !returnnewline);data++)
        {
                if (*data == 0)
                {
@@ -1186,7 +1186,7 @@ skipwhite:
        else
        {
                // regular word
-               for (;*data > ' ' && *data != '{' && *data != '}' && *data != ')' && *data != '(' && *data != ']' && *data != '[' && *data != ':' && *data != ',' && *data != ';';data++)
+               for (;!ISWHITESPACE(*data) && *data != '{' && *data != '}' && *data != ')' && *data != '(' && *data != ']' && *data != '[' && *data != ':' && *data != ',' && *data != ';';data++)
                        if (len < (int)sizeof(com_token) - 1)
                                com_token[len++] = *data;
                com_token[len] = 0;
@@ -1223,7 +1223,7 @@ skipwhite:
        // UNIX: \n
        // Mac: \r
        // Windows: \r\n
-       for (;*data <= ' ' && ((*data != '\n' && *data != '\r') || !returnnewline);data++)
+       for (;ISWHITESPACE(*data) && ((*data != '\n' && *data != '\r') || !returnnewline);data++)
        {
                if (*data == 0)
                {
@@ -1300,7 +1300,7 @@ skipwhite:
        else
        {
                // regular word
-               for (;*data > ' ' && *data != ',' && *data != ';' && *data != '{' && *data != '}' && *data != ')' && *data != '(' && *data != ']' && *data != '[' && *data != ':' && *data != ',' && *data != ';';data++)
+               for (;!ISWHITESPACE(*data) && *data != ',' && *data != ';' && *data != '{' && *data != '}' && *data != ')' && *data != '(' && *data != ']' && *data != '[' && *data != ':' && *data != ',' && *data != ';';data++)
                        if (len < (int)sizeof(com_token) - 1)
                                com_token[len++] = *data;
                com_token[len] = 0;
@@ -1332,7 +1332,7 @@ int COM_ParseToken_Console(const char **datapointer)
 
 // skip whitespace
 skipwhite:
-       for (;*data <= ' ';data++)
+       for (;ISWHITESPACE(*data);data++)
        {
                if (*data == 0)
                {
@@ -1368,7 +1368,7 @@ skipwhite:
        else
        {
                // regular word
-               for (;*data > ' ';data++)
+               for (;!ISWHITESPACE(*data);data++)
                        if (len < (int)sizeof(com_token) - 1)
                                com_token[len++] = *data;
                com_token[len] = 0;
@@ -1693,7 +1693,7 @@ int COM_ReadAndTokenizeLine(const char **text, char **argv, int maxargc, char *t
                commentprefixlength = (int)strlen(commentprefix);
        while (*l && *l != '\n' && *l != '\r')
        {
-               if (*l > ' ')
+               if (!ISWHITESPACE(*l))
                {
                        if (commentprefixlength && !strncmp(l, commentprefix, commentprefixlength))
                        {
@@ -1718,7 +1718,7 @@ int COM_ReadAndTokenizeLine(const char **text, char **argv, int maxargc, char *t
                        }
                        else
                        {
-                               while (*l > ' ')
+                               while (!ISWHITESPACE(*l))
                                {
                                        if (tokenbuf >= tokenbufend)
                                                return -1;
index da0f18a184828ddb8f3df2c3698c76ccbc12b29a..421e0379afc343a79943e8fe68359fcb8949555a 100644 (file)
--- a/console.c
+++ b/console.c
@@ -1812,8 +1812,8 @@ qboolean GetMapList (const char *s, char *completedname, int completednamebuffer
                                        if (com_token[0] == '}')
                                                break;
                                        // skip leading whitespace
-                                       for (k = 0;com_token[k] && com_token[k] <= ' ';k++);
-                                       for (l = 0;l < (int)sizeof(keyname) - 1 && com_token[k+l] && com_token[k+l] > ' ';l++)
+                                       for (k = 0;com_token[k] && ISWHITESPACE(com_token[k]);k++);
+                                       for (l = 0;l < (int)sizeof(keyname) - 1 && com_token[k+l] && !ISWHITESPACE(com_token[k+l]);l++)
                                                keyname[l] = com_token[k+l];
                                        keyname[l] = 0;
                                        if (!COM_ParseToken_Simple(&data, false, false))
index 4436ae5b4f0a52e2566741164fa98a438b52d2f4..b80cf5638085f0632361ad9d8784379c0179a934 100644 (file)
@@ -530,7 +530,7 @@ void Host_Savegame_to (const char *name)
        // convert space to _ to make stdio happy
        // LordHavoc: convert control characters to _ as well
        for (i=0 ; i<SAVEGAME_COMMENT_LENGTH ; i++)
-               if (comment[i] <= ' ')
+               if (ISWHITESPACEORCONTROL(comment[i]))
                        comment[i] = '_';
        comment[SAVEGAME_COMMENT_LENGTH] = '\0';
 
@@ -2317,7 +2317,7 @@ void Host_Rcon_f (void) // credit: taken from QuakeWorld
 
        for (i = 0;rcon_password.string[i];i++)
        {
-               if (rcon_password.string[i] <= ' ')
+               if (ISWHITESPACE(rcon_password.string[i]))
                {
                        Con_Printf("rcon_password is not allowed to have any whitespace.\n");
                        return;
index e8b38cf5f022c19effd7289de1d82c684bd690c1..ce127fec5e3527bdd1ea97717e29efa8ee370462 100755 (executable)
--- a/netconn.c
+++ b/netconn.c
@@ -2171,7 +2171,7 @@ const char *RCon_Authenticate(const char *password, const char *s, const char *e
                return NULL;
 
        for(text = s; text != endpos; ++text)
-               if(*text > 0 && (*text < ' ' || *text == ';'))
+               if((signed char) *text > 0 && ((signed char) *text < (signed char) ' ' || *text == ';'))
                        return NULL; // block possible exploits against the parser/alias expansion
 
        while(s != endpos)
@@ -2399,13 +2399,13 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                        char *s = string + 5;
                        char *endpos = string + length + 1; // one behind the NUL, so adding strlen+1 will eventually reach it
                        char password[64];
-                       for (i = 0;*s > ' ';s++)
+                       for (i = 0;!ISWHITESPACE(*s);s++)
                                if (i < (int)sizeof(password) - 1)
                                        password[i++] = *s;
-                       if(*s <= ' ' && s != endpos) // skip leading ugly space
+                       if(ISWHITESPACE(*s) && s != endpos) // skip leading ugly space
                                ++s;
                        password[i] = 0;
-                       if (password[0] > ' ')
+                       if (!ISWHITESPACE(password[0]))
                        {
                                const char *userlevel = RCon_Authenticate(password, s, endpos);
                                if(userlevel)
index 696ffea0439643df575f5b301f9025f7e9dd2c91..63f17f2909a97d1fdebe64bdd97ebbdd0adfb492 100644 (file)
@@ -45,7 +45,7 @@ void VM_Warning(const char *fmt, ...)
 
 void VM_CheckEmptyString (const char *s)
 {
-       if (s[0] <= ' ')
+       if (ISWHITESPACE(s[0]))
                PRVM_ERROR ("%s: Bad string", PRVM_NAME);
 }
 
@@ -2291,7 +2291,7 @@ void VM_tokenize (void)
                        break;
 
                // skip whitespace here to find token start pos
-               while(*p && (unsigned char) *p <= ' ')
+               while(*p && ISWHITESPACE(*p))
                        ++p;
 
                tokens_startpos[num_tokens] = p - string;
@@ -2323,7 +2323,7 @@ void VM_tokenize_console (void)
                        break;
 
                // skip whitespace here to find token start pos
-               while(*p && (unsigned char) *p <= ' ')
+               while(*p && ISWHITESPACE(*p))
                        ++p;
 
                tokens_startpos[num_tokens] = p - string;
index 742e4b988b9ee3ae77aecb1f9b183fb408a1e287..d8499c4368c98e6028a5e7525b45912b230e8264 100644 (file)
@@ -995,7 +995,7 @@ qboolean PRVM_ED_ParseEpair(prvm_edict_t *ent, ddef_t *key, const char *s, qbool
                break;
 
        case ev_float:
-               while (*s && *s <= ' ')
+               while (*s && ISWHITESPACE(*s))
                        s++;
                val->_float = atof(s);
                break;
@@ -1003,12 +1003,12 @@ qboolean PRVM_ED_ParseEpair(prvm_edict_t *ent, ddef_t *key, const char *s, qbool
        case ev_vector:
                for (i = 0;i < 3;i++)
                {
-                       while (*s && *s <= ' ')
+                       while (*s && ISWHITESPACE(*s))
                                s++;
                        if (!*s)
                                break;
                        val->vector[i] = atof(s);
-                       while (*s > ' ')
+                       while (!ISWHITESPACE(*s))
                                s++;
                        if (!*s)
                                break;
@@ -1016,7 +1016,7 @@ qboolean PRVM_ED_ParseEpair(prvm_edict_t *ent, ddef_t *key, const char *s, qbool
                break;
 
        case ev_entity:
-               while (*s && *s <= ' ')
+               while (*s && ISWHITESPACE(*s))
                        s++;
                i = atoi(s);
                if (i >= prog->limit_edicts)
index 3ddf1c737e4c861c529fe735156b2a9ae33643b0..6dc94ef159eb20005bccdb46b9e4070a87b8cd25 100644 (file)
@@ -354,5 +354,12 @@ void Sys_Shared_Init(void);
 // debug protocol exploits.
 #define DEMOMSG_CLIENT_TO_SERVER 0x80000000
 
+// In Quake, any char in 0..32 counts as whitespace
+//#define ISWHITESPACE(ch) ((unsigned char) ch <= (unsigned char) ' ')
+#define ISWHITESPACE(ch) (!(ch) || (ch) == ' ' || (ch) == '\t' || (ch) == '\r' || (ch) == '\n')
+
+// This also includes extended characters, and ALL control chars
+#define ISWHITESPACEORCONTROL(ch) ((signed char) (ch) <= (signed char) ' ')
+
 #endif
 
index e2bc4e32683365634d40ba77bea36fbd1371ed7f..8e042f405a0c9deba593e0541b33fb48f373a68e 100644 (file)
@@ -68,7 +68,7 @@ void Sys_PrintToTerminal(const char *text)
 #endif
        while(*text)
        {
-               int written = (int)write(1, text, (int)strlen(text));
+               ssize_t written = write(1, text, strlen(text));
                if(written <= 0)
                        break; // sorry, I cannot do anything about this error - without an output
                text += written;
@@ -87,7 +87,7 @@ double Sys_DoubleTime (void)
        if(sys_usenoclockbutbenchmark.integer)
        {
                benchmark_time += 1;
-               return benchmark_time / 1e6;
+               return ((double) benchmark_time) / 1e6;
        }
 #ifdef WIN32
 #include <mmsystem.h>
index 5431221a12d0848455ab2614b45fd8a100577993..928d842266dbc2f7a4fa13e381418f53c835d10a 100644 (file)
--- a/sys_win.c
+++ b/sys_win.c
@@ -269,7 +269,7 @@ char *Sys_ConsoleInput (void)
                                                break;
 
                                        default:
-                                               if (ch >= ' ')
+                                               if (ch >= (int) (unsigned char) ' ')
                                                {
                                                        WriteFile(houtput, &ch, 1, &dummy, NULL);
                                                        text[len] = ch;
@@ -423,7 +423,7 @@ int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLin
        // FIXME: this tokenizer is rather redundent, call a more general one
        while (*lpCmdLine && (com_argc < MAX_NUM_ARGVS))
        {
-               while (*lpCmdLine && *lpCmdLine <= ' ')
+               while (*lpCmdLine && ISWHITESPACE(*lpCmdLine))
                        lpCmdLine++;
 
                if (!*lpCmdLine)
@@ -443,7 +443,7 @@ int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLin
                        // unquoted word
                        argv[com_argc] = lpCmdLine;
                        com_argc++;
-                       while (*lpCmdLine && *lpCmdLine > ' ')
+                       while (*lpCmdLine && !ISWHITESPACE(*lpCmdLine))
                                lpCmdLine++;
                }