Author: ek.kato
Date: Mon Jul  9 18:06:48 2007
New Revision: 4671

Modified:
   trunk/qt/immodule-quiminputcontext.h
   trunk/qt/immodule-quiminputcontext_compose.cpp
   trunk/xim/compose.cpp
   trunk/xim/xim.h

Log:
* qt/immodule-quiminputcontext.h
* qt/immodule-quiminputcontext_compose.cpp
* xim/xim.h
* xim/compose.cpp
  - Apply fixes of compose token buffer usage as in gtk+ bridge
    on r4670.


Modified: trunk/qt/immodule-quiminputcontext.h
==============================================================================
--- trunk/qt/immodule-quiminputcontext.h        (original)
+++ trunk/qt/immodule-quiminputcontext.h        Mon Jul  9 18:06:48 2007
@@ -136,7 +136,7 @@
     static char *TransFileName( char *name );
     static void ParseComposeStringFile( FILE *fp );
     static void FreeComposeTree( DefTree *top );
-    static int parse_compose_line( FILE *fp, char *tokenbuf );
+    static int parse_compose_line( FILE *fp, char *tokenbuf, size_t *remain );
     static int get_mb_string( char *buf, unsigned int ks );
     static const char *get_encoding( void );
     static char *get_lang_region( void );

Modified: trunk/qt/immodule-quiminputcontext_compose.cpp
==============================================================================
--- trunk/qt/immodule-quiminputcontext_compose.cpp      (original)
+++ trunk/qt/immodule-quiminputcontext_compose.cpp      Mon Jul  9 18:06:48 2007
@@ -264,10 +264,13 @@
 }
 
 static int
-nextch(FILE *fp, int *lastch)
+nextch(FILE *fp, int *lastch, size_t *remain)
 {
     int c;
 
+    if (*remain <= 1)
+       return EOF;
+
     if (*lastch != 0) {
        c = *lastch;
        *lastch = 0;
@@ -313,14 +316,14 @@
 #endif
 
 static int
-nexttoken(FILE *fp, char *tokenbuf, int *lastch)
+nexttoken(FILE *fp, char *tokenbuf, int *lastch, size_t *remain)
 {
     int c;
     int token;
     char *p;
     int i, j;
 
-    while ((c = nextch(fp, lastch)) == ' ' || c == '\t') {
+    while ((c = nextch(fp, lastch, remain)) == ' ' || c == '\t') {
     }
     switch (c) {
     case EOF:
@@ -346,26 +349,30 @@
        break;
     case '"':
        p = tokenbuf;
-       while ((c = nextch(fp, lastch)) != '"') {
+       while ((c = nextch(fp, lastch, remain)) != '"') {
            if (c == '\n' || c == EOF) {
                putbackch(c, lastch);
                token = ERROR;
                goto string_error;
            } else if (c == '\\') {
-               c = nextch(fp, lastch);
+               c = nextch(fp, lastch, remain);
                switch (c) {
                case '\\':
                case '"':
                    *p++ = c;
+                   (*remain)--;
                    break;
                case 'n':
                    *p++ = '\n';
+                   (*remain)--;
                    break;
                case 'r':
                    *p++ = '\r';
+                   (*remain)--;
                    break;
                case 't':
                    *p++ = '\t';
+                   (*remain)--;
                    break;
                case '0':
                case '1':
@@ -376,20 +383,21 @@
                case '6':
                case '7':
                    i = c - '0';
-                   c = nextch(fp, lastch);
+                   c = nextch(fp, lastch, remain);
                    for (j = 0; j < 2 && c >= '0' && c <= '7'; j++) {
                        i <<= 3;
                        i += c - '0';
-                       c = nextch(fp, lastch);
+                       c = nextch(fp, lastch, remain);
                    }
                    putbackch(c, lastch);
                    *p++ = (char)i;
+                   (*remain)--;
                    break;
                case 'X':
                case 'x':
                    i = 0;
                    for (j = 0; j < 2; j++) {
-                       c = nextch(fp, lastch);
+                       c = nextch(fp, lastch, remain);
                        i <<= 4;
                        if (c >= '0' && c <= '9') {
                            i += c - '0';
@@ -408,6 +416,7 @@
                        goto string_error;
                    }
                    *p++ = (char)i;
+                   (*remain)--;
                    break;
                case EOF:
                    putbackch(c, lastch);
@@ -415,17 +424,19 @@
                    goto string_error;
                default:
                    *p++ = c;
+                   (*remain)--;
                    break;
                }
            } else {
                *p++ = c;
+               (*remain)--;
            }
        }
        *p = '\0';
        token = STRING;
        break;
     case '#':
-       while ((c = nextch(fp, lastch)) != '\n' && c != EOF) {
+       while ((c = nextch(fp, lastch, remain)) != '\n' && c != EOF) {
        }
        if (c == '\n') {
            token = ENDOFLINE;
@@ -437,10 +448,17 @@
        if (isalnum(c) || c == '_' || c == '-') {
            p = tokenbuf;
            *p++ = c;
-           c = nextch(fp, lastch);
+           (*remain)--;
+           c = nextch(fp, lastch, remain);
            while (isalnum(c) || c == '_' || c == '-') {
                *p++ = c;
-               c = nextch(fp, lastch);
+               (*remain)--;
+               c = nextch(fp, lastch, remain);
+           }
+           if (c == '\n' || c == EOF) {
+               putbackch(c, lastch);
+               token = ERROR;
+               goto string_error;
            }
            *p = '\0';
            putbackch(c, lastch);
@@ -604,7 +622,7 @@
 #define SEQUENCE_MAX    10
 
 int
-QUimInputContext::parse_compose_line(FILE *fp, char* tokenbuf)
+QUimInputContext::parse_compose_line(FILE *fp, char* tokenbuf, size_t *remain)
 {
     int token;
     unsigned modifier_mask;
@@ -633,7 +651,7 @@
     QString qs;
 
     do {
-       token = nexttoken(fp, tokenbuf, &lastch);
+       token = nexttoken(fp, tokenbuf, &lastch, remain);
     } while (token == ENDOFLINE);
 
     if (token == ENDOFFILE) {
@@ -645,7 +663,7 @@
        if ((token == KEY) && (strcmp("include", tokenbuf) == 0)) {
            char *filename;
            FILE *infp;
-           token = nexttoken(fp, tokenbuf, &lastch);
+           token = nexttoken(fp, tokenbuf, &lastch, remain);
            if (token != KEY && token != STRING)
                goto error;
 
@@ -660,19 +678,19 @@
        } else if ((token == KEY) && (strcmp("None", tokenbuf) == 0)) {
            modifier = 0;
            modifier_mask = AllMask;
-           token = nexttoken(fp, tokenbuf, &lastch);
+           token = nexttoken(fp, tokenbuf, &lastch, remain);
        } else {
            modifier_mask = modifier = 0;
            exclam = False;
            if (token == EXCLAM) {
                exclam = True;
-               token = nexttoken(fp, tokenbuf, &lastch);
+               token = nexttoken(fp, tokenbuf, &lastch, remain);
            }
            while (token == TILDE || token == KEY) {
                tilde = False;
                if (token == TILDE) {
                    tilde = True;
-                   token = nexttoken(fp, tokenbuf, &lastch);
+                   token = nexttoken(fp, tokenbuf, &lastch, remain);
                    if (token != KEY)
                        goto error;
                }
@@ -686,7 +704,7 @@
                } else {
                    modifier |= tmp;
                }
-               token = nexttoken(fp, tokenbuf, &lastch);
+               token = nexttoken(fp, tokenbuf, &lastch, remain);
            }
            if (exclam) {
                modifier_mask = AllMask;
@@ -698,12 +716,12 @@
            goto error;
        }
 
-       token = nexttoken(fp, tokenbuf, &lastch);
+       token = nexttoken(fp, tokenbuf, &lastch, remain);
        if (token != KEY) {
            goto error;
        }
 
-       token = nexttoken(fp, tokenbuf, &lastch);
+       token = nexttoken(fp, tokenbuf, &lastch, remain);
        if (token != GREATER) {
            goto error;
        }
@@ -719,22 +737,22 @@
        n++;
        if (n >= SEQUENCE_MAX)
            goto error;
-       token = nexttoken(fp, tokenbuf, &lastch);
+       token = nexttoken(fp, tokenbuf, &lastch, remain);
     } while (token != COLON);
 
-    token = nexttoken(fp, tokenbuf, &lastch);
+    token = nexttoken(fp, tokenbuf, &lastch, remain);
     if (token == STRING) {
        if ((rhs_string_mb = (char *)malloc(strlen(tokenbuf) + 1)) == NULL)
            goto error;
        strcpy(rhs_string_mb, tokenbuf);
-       token = nexttoken(fp, tokenbuf, &lastch);
+       token = nexttoken(fp, tokenbuf, &lastch, remain);
        if (token == KEY) {
            rhs_keysym = XStringToKeysym(tokenbuf);
            if (rhs_keysym == NoSymbol) {
                free(rhs_string_mb);
                goto error;
            }
-           token = nexttoken(fp, tokenbuf, &lastch);
+           token = nexttoken(fp, tokenbuf, &lastch, remain);
        }
        if (token != ENDOFLINE && token != ENDOFFILE) {
            free(rhs_string_mb);
@@ -745,7 +763,7 @@
        if (rhs_keysym == NoSymbol) {
            goto error;
        }
-       token = nexttoken(fp, tokenbuf, &lastch);
+       token = nexttoken(fp, tokenbuf, &lastch, remain);
        if (token != ENDOFLINE && token != ENDOFFILE) {
            goto error;
        }
@@ -806,7 +824,7 @@
     return n;
 error:
     while (token != ENDOFLINE && token != ENDOFFILE) {
-       token = nexttoken(fp, tokenbuf, &lastch);
+       token = nexttoken(fp, tokenbuf, &lastch, remain);
     }
     return 0;
 }
@@ -829,22 +847,21 @@
 void
 QUimInputContext::ParseComposeStringFile(FILE *fp)
 {
-    char tb[8192];
     char* tbp;
     struct stat st;
+    size_t tb_remain;
 
-    if (fstat(fileno(fp), &st) != -1) {
-       unsigned long size = (unsigned long)st.st_size;
-       if (size <= sizeof tb)
-           tbp = tb;
-       else
-           tbp = (char *)malloc(size);
+    if (fstat(fileno(fp), &st) != -1 &&
+       S_ISREG(st.st_mode) &&
+       st.st_size > 0 &&
+       st.st_size + (size_t)0 < (off_t)0 + (size_t)-1) {
 
+       tbp = (char *)malloc(st.st_size);
+       tb_remain = st.st_size;
        if (tbp != NULL) {
-           while (parse_compose_line(fp, tbp) >= 0) {
+           while (parse_compose_line(fp, tbp, &tb_remain) >= 0) {
            }
-           if (tbp != tb)
-               free (tbp);
+           free (tbp);
        }
     }
 }

Modified: trunk/xim/compose.cpp
==============================================================================
--- trunk/xim/compose.cpp       (original)
+++ trunk/xim/compose.cpp       Mon Jul  9 18:06:48 2007
@@ -117,10 +117,13 @@
 }
 
 static int
-nextch(FILE *fp, int *lastch)
+nextch(FILE *fp, int *lastch, size_t *remain)
 {
     int c;
 
+    if (*remain <= 1)
+       return EOF;
+
     if (*lastch != 0) {
        c = *lastch;
        *lastch = 0;
@@ -166,14 +169,14 @@
 #endif
 
 static int
-nexttoken(FILE *fp, char *tokenbuf, int *lastch)
+nexttoken(FILE *fp, char *tokenbuf, int *lastch, size_t *remain)
 {
     int c;
     int token;
     char *p;
     int i, j;
 
-    while ((c = nextch(fp, lastch)) == ' ' || c == '\t') {
+    while ((c = nextch(fp, lastch, remain)) == ' ' || c == '\t') {
     }
     switch (c) {
     case EOF:
@@ -199,26 +202,30 @@
        break;
     case '"':
        p = tokenbuf;
-       while ((c = nextch(fp, lastch)) != '"') {
+       while ((c = nextch(fp, lastch, remain)) != '"') {
            if (c == '\n' || c == EOF) {
                putbackch(c, lastch);
                token = ERROR;
                goto string_error;
            } else if (c == '\\') {
-               c = nextch(fp, lastch);
+               c = nextch(fp, lastch, remain);
                switch (c) {
                case '\\':
                case '"':
                    *p++ = c;
+                   (*remain)--;
                    break;
                case 'n':
                    *p++ = '\n';
+                   (*remain)--;
                    break;
                case 'r':
                    *p++ = '\r';
+                   (*remain)--;
                    break;
                case 't':
                    *p++ = '\t';
+                   (*remain)--;
                    break;
                case '0':
                case '1':
@@ -229,20 +236,21 @@
                case '6':
                case '7':
                    i = c - '0';
-                   c = nextch(fp, lastch);
+                   c = nextch(fp, lastch, remain);
                    for (j = 0; j < 2 && c >= '0' && c <= '7'; j++) {
                        i <<= 3;
                        i += c - '0';
-                       c = nextch(fp, lastch);
+                       c = nextch(fp, lastch, remain);
                    }
                    putbackch(c, lastch);
                    *p++ = (char)i;
+                   (*remain)--;
                    break;
                case 'X':
                case 'x':
                    i = 0;
                    for (j = 0; j < 2; j++) {
-                       c = nextch(fp, lastch);
+                       c = nextch(fp, lastch, remain);
                        i <<= 4;
                        if (c >= '0' && c <= '9') {
                            i += c - '0';
@@ -261,6 +269,7 @@
                        goto string_error;
                    }
                    *p++ = (char)i;
+                   (*remain)--;
                    break;
                case EOF:
                    putbackch(c, lastch);
@@ -268,17 +277,19 @@
                    goto string_error;
                default:
                    *p++ = c;
+                   (*remain)--;
                    break;
                }
            } else {
                *p++ = c;
+               (*remain)--;
            }
        }
        *p = '\0';
        token = STRING;
        break;
     case '#':
-       while ((c = nextch(fp, lastch)) != '\n' && c != EOF) {
+       while ((c = nextch(fp, lastch, remain)) != '\n' && c != EOF) {
        }
        if (c == '\n') {
            token = ENDOFLINE;
@@ -290,10 +301,17 @@
        if (isalnum(c) || c == '_' || c == '-') {
            p = tokenbuf;
            *p++ = c;
-           c = nextch(fp, lastch);
+           (*remain)--;
+           c = nextch(fp, lastch, remain);
            while (isalnum(c) || c == '_' || c == '-') {
                *p++ = c;
-               c = nextch(fp, lastch);
+               (*remain)--;
+               c = nextch(fp, lastch, remain);
+           }
+           if (c == '\n' || c == EOF) {
+               putbackch(c, lastch);
+               token = ERROR;
+               goto string_error;
            }
            *p = '\0';
            putbackch(c, lastch);
@@ -433,7 +451,7 @@
 #define SEQUENCE_MAX    10
 
 int
-XimIM::parse_compose_line(FILE *fp, char* tokenbuf)
+XimIM::parse_compose_line(FILE *fp, char* tokenbuf, size_t *remain)
 {
     int token;
     unsigned modifier_mask;
@@ -461,7 +479,7 @@
     const char *encoding = get_encoding();;
 
     do {
-       token = nexttoken(fp, tokenbuf, &lastch);
+       token = nexttoken(fp, tokenbuf, &lastch, remain);
     } while (token == ENDOFLINE);
 
     if (token == ENDOFFILE) {
@@ -473,7 +491,7 @@
        if ((token == KEY) && (strcmp("include", tokenbuf) == 0)) {
            char *filename;
            FILE *infp;
-           token = nexttoken(fp, tokenbuf, &lastch);
+           token = nexttoken(fp, tokenbuf, &lastch, remain);
            if (token != KEY && token != STRING)
                goto error;
 
@@ -488,19 +506,19 @@
        } else if ((token == KEY) && (strcmp("None", tokenbuf) == 0)) {
            modifier = 0;
            modifier_mask = AllMask;
-           token = nexttoken(fp, tokenbuf, &lastch);
+           token = nexttoken(fp, tokenbuf, &lastch, remain);
        } else {
            modifier_mask = modifier = 0;
            exclam = False;
            if (token == EXCLAM) {
                exclam = True;
-               token = nexttoken(fp, tokenbuf, &lastch);
+               token = nexttoken(fp, tokenbuf, &lastch, remain);
            }
            while (token == TILDE || token == KEY) {
                tilde = False;
                if (token == TILDE) {
                    tilde = True;
-                   token = nexttoken(fp, tokenbuf, &lastch);
+                   token = nexttoken(fp, tokenbuf, &lastch, remain);
                    if (token != KEY)
                        goto error;
                }
@@ -514,7 +532,7 @@
                } else {
                    modifier |= tmp;
                }
-               token = nexttoken(fp, tokenbuf, &lastch);
+               token = nexttoken(fp, tokenbuf, &lastch, remain);
            }
            if (exclam) {
                modifier_mask = AllMask;
@@ -526,12 +544,12 @@
            goto error;
        }
 
-       token = nexttoken(fp, tokenbuf, &lastch);
+       token = nexttoken(fp, tokenbuf, &lastch, remain);
        if (token != KEY) {
            goto error;
        }
 
-       token = nexttoken(fp, tokenbuf, &lastch);
+       token = nexttoken(fp, tokenbuf, &lastch, remain);
        if (token != GREATER) {
            goto error;
        }
@@ -547,22 +565,22 @@
        n++;
        if (n >= SEQUENCE_MAX)
            goto error;
-       token = nexttoken(fp, tokenbuf, &lastch);
+       token = nexttoken(fp, tokenbuf, &lastch, remain);
     } while (token != COLON);
 
-    token = nexttoken(fp, tokenbuf, &lastch);
+    token = nexttoken(fp, tokenbuf, &lastch, remain);
     if (token == STRING) {
        if ((rhs_string_mb = (char *)malloc(strlen(tokenbuf) + 1)) == NULL)
            goto error;
        strcpy(rhs_string_mb, tokenbuf);
-       token = nexttoken(fp, tokenbuf, &lastch);
+       token = nexttoken(fp, tokenbuf, &lastch, remain);
        if (token == KEY) {
            rhs_keysym = XStringToKeysym(tokenbuf);
            if (rhs_keysym == NoSymbol) {
                free(rhs_string_mb);
                goto error;
            }
-           token = nexttoken(fp, tokenbuf, &lastch);
+           token = nexttoken(fp, tokenbuf, &lastch, remain);
        }
        if (token != ENDOFLINE && token != ENDOFFILE) {
            free(rhs_string_mb);
@@ -573,7 +591,7 @@
        if (rhs_keysym == NoSymbol) {
            goto error;
        }
-       token = nexttoken(fp, tokenbuf, &lastch);
+       token = nexttoken(fp, tokenbuf, &lastch, remain);
        if (token != ENDOFLINE && token != ENDOFFILE) {
            goto error;
        }
@@ -641,7 +659,7 @@
     return n;
 error:
     while (token != ENDOFLINE && token != ENDOFFILE) {
-       token = nexttoken(fp, tokenbuf, &lastch);
+       token = nexttoken(fp, tokenbuf, &lastch, remain);
     }
     return 0;
 }
@@ -649,22 +667,21 @@
 void
 XimIM::ParseComposeStringFile(FILE *fp)
 {
-    char tb[8192];
     char* tbp;
     struct stat st;
+    size_t tb_remain;
 
-    if (fstat(fileno(fp), &st) != -1) {
-       unsigned long size = (unsigned long)st.st_size;
-       if (size <= sizeof tb)
-           tbp = tb;
-       else
-           tbp = (char *)malloc(size);
+    if (fstat(fileno(fp), &st) != -1 &&
+       S_ISREG(st.st_mode) &&
+       st.st_size > 0 &&
+       st.st_size + (size_t)0 < (off_t)0 + (size_t)-1) {
 
+       tbp = (char *)malloc(st.st_size);
+       tb_remain = st.st_size;
        if (tbp != NULL) {
-           while (parse_compose_line(fp, tbp) >= 0) {
+           while (parse_compose_line(fp, tbp, &tb_remain) >= 0) {
            }
-           if (tbp != tb)
-               free (tbp);
+           free (tbp);
        }
     }
 }

Modified: trunk/xim/xim.h
==============================================================================
--- trunk/xim/xim.h     (original)
+++ trunk/xim/xim.h     Mon Jul  9 18:06:48 2007
@@ -211,7 +211,7 @@
     char *TransFileName(char *name);
     void ParseComposeStringFile(FILE *fp);
     void FreeComposeTree(DefTree *top);
-    int parse_compose_line(FILE *fp, char *tokenbuf);
+    int parse_compose_line(FILE *fp, char *tokenbuf, size_t *remain);
     int get_mb_string(char *buf, KeySym ks);
     DefTree *mTreeTop;
 };

Reply via email to