Permalink
Browse files

Fix [string tolower] buffer overflow for non-utf8

Reported-by: Andy <jimdevel@hummypkg.org.uk>
Signed-off-by: Steve Bennett <steveb@workware.net.au>
  • Loading branch information...
1 parent 91960fb commit a077f0800f5bbc7ca5f3b368726f7d1757c16549 @msteveb committed Nov 5, 2013
Showing with 15 additions and 17 deletions.
  1. +1 −1 jim-format.c
  2. +2 −2 jim.c
  3. +4 −4 jimregexp.c
  4. +0 −10 linenoise.c
  5. +4 −0 regtest.tcl
  6. +4 −0 utf8.h
View
@@ -302,7 +302,7 @@ Jim_Obj *Jim_FormatString(Jim_Interp *interp, Jim_Obj *fmtObjPtr, int objc, Jim_
goto error;
}
/* Just store the value in the 'spec' buffer */
- formatted_bytes = utf8_fromunicode(spec, code);
+ formatted_bytes = utf8_getchars(spec, code);
formatted_buf = spec;
formatted_chars = 1;
break;
View
4 jim.c
@@ -2744,7 +2744,7 @@ static void JimStrCopyUpperLower(char *dest, const char *str, int uc)
while (*str) {
int c;
str += utf8_tounicode(str, &c);
- dest += utf8_fromunicode(dest, uc ? utf8_upper(c) : utf8_lower(c));
+ dest += utf8_getchars(dest, uc ? utf8_upper(c) : utf8_lower(c));
}
*dest = 0;
}
@@ -2813,7 +2813,7 @@ static Jim_Obj *JimStringToTitle(Jim_Interp *interp, Jim_Obj *strObjPtr)
buf = p = Jim_Alloc(len + 1);
str += utf8_tounicode(str, &c);
- p += utf8_fromunicode(p, utf8_title(c));
+ p += utf8_getchars(p, utf8_title(c));
JimStrCopyUpperLower(p, str, 0);
View
@@ -1665,10 +1665,10 @@ static void regdump(regex_t *preg)
while (preg->program[s]) {
int len = preg->program[s++];
int first = preg->program[s++];
- buf[utf8_fromunicode(buf, first)] = 0;
+ buf[utf8_setunicode(buf, first)] = 0;
printf("%s", buf);
if (len > 1) {
- buf[utf8_fromunicode(buf, first + len - 1)] = 0;
+ buf[utf8_setunicode(buf, first + len - 1)] = 0;
printf("-%s", buf);
}
}
@@ -1678,7 +1678,7 @@ static void regdump(regex_t *preg)
/* Literal string, where present. */
while (preg->program[s]) {
- buf[utf8_fromunicode(buf, preg->program[s])] = 0;
+ buf[utf8_setunicode(buf, preg->program[s])] = 0;
printf("%s", buf);
s++;
}
@@ -1690,7 +1690,7 @@ static void regdump(regex_t *preg)
if (op == END) {
/* Header fields of interest. */
if (preg->regstart) {
- buf[utf8_fromunicode(buf, preg->regstart)] = 0;
+ buf[utf8_setunicode(buf, preg->regstart)] = 0;
printf("start '%s' ", buf);
}
if (preg->reganch)
View
@@ -717,16 +717,6 @@ static int getWindowSize(struct current *current)
}
#endif
-static int utf8_getchars(char *buf, int c)
-{
-#ifdef USE_UTF8
- return utf8_fromunicode(buf, c);
-#else
- *buf = c;
- return 1;
-#endif
-}
-
/**
* Returns the unicode character at the given offset,
* or -1 if none.
View
@@ -210,6 +210,10 @@ set x [lindex {} 0]
info source $x
eval $x
+# REGTEST 30
+# non-UTF8 string tolower
+string tolower "/mod/video/h\303\203\302\244xan_ witchcraft through the ages_20131101_0110.t"
+
# TAKE THE FOLLOWING puts AS LAST LINE
puts "--- ALL TESTS PASSED ---"
View
4 utf8.h
@@ -26,6 +26,7 @@ int utf8_fromunicode(char *p, unsigned uc);
/* No utf-8 support. 1 byte = 1 char */
#define utf8_strlen(S, B) ((B) < 0 ? strlen(S) : (B))
#define utf8_tounicode(S, CP) (*(CP) = (unsigned char)*(S), 1)
+#define utf8_getchars(CP, C) (*(CP) = (C), 1)
#define utf8_upper(C) toupper(C)
#define utf8_title(C) toupper(C)
#define utf8_lower(C) tolower(C)
@@ -35,6 +36,9 @@ int utf8_fromunicode(char *p, unsigned uc);
#else
#if !defined(JIM_BOOTSTRAP)
+
+#define utf8_getchars utf8_fromunicode
+
/**
* Returns the length of the utf-8 sequence starting with 'c'.
*

0 comments on commit a077f08

Please sign in to comment.