Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/external/mit/lua/dist/src lua: apply upstream bugfix for "'u...



details:   https://anonhg.NetBSD.org/src/rev/b577658937c4
branches:  trunk
changeset: 374301:b577658937c4
user:      nikita <nikita%NetBSD.org@localhost>
date:      Mon Apr 17 20:27:40 2023 +0000

description:
lua: apply upstream bugfix for "'utf8.codes' does not raise an error on spurious continuation bytes."

diffstat:

 external/mit/lua/dist/src/lutf8lib.c |  28 ++++++++++++++++------------
 1 files changed, 16 insertions(+), 12 deletions(-)

diffs (104 lines):

diff -r 789f2fbe8dbe -r b577658937c4 external/mit/lua/dist/src/lutf8lib.c
--- a/external/mit/lua/dist/src/lutf8lib.c      Mon Apr 17 20:17:58 2023 +0000
+++ b/external/mit/lua/dist/src/lutf8lib.c      Mon Apr 17 20:27:40 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: lutf8lib.c,v 1.9 2023/04/16 20:46:17 nikita Exp $      */
+/*     $NetBSD: lutf8lib.c,v 1.10 2023/04/17 20:27:40 nikita Exp $     */
 
 /*
 ** Id: lutf8lib.c 
@@ -29,6 +29,8 @@
 
 #define MAXUTF         0x7FFFFFFFu
 
+#define MSGInvalid     "invalid UTF-8 code"
+
 /*
 ** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits.
 */
@@ -39,7 +41,8 @@ typedef unsigned long utfint;
 #endif
 
 
-#define iscont(p)      ((*(p) & 0xC0) == 0x80)
+#define iscont(c)      (((c) & 0xC0) == 0x80)
+#define iscontp(p)     iscont(*(p))
 
 
 /* from strlib */
@@ -69,7 +72,7 @@ static const char *utf8_decode (const ch
     int count = 0;  /* to count number of continuation bytes */
     for (; c & 0x40; c <<= 1) {  /* while it needs continuation bytes... */
       unsigned int cc = (unsigned char)s[++count];  /* read next byte */
-      if ((cc & 0xC0) != 0x80)  /* not a continuation byte? */
+      if (!iscont(cc))  /* not a continuation byte? */
         return NULL;  /* invalid byte sequence */
       res = (res << 6) | (cc & 0x3F);  /* add lower 6 bits from cont. byte */
     }
@@ -144,7 +147,7 @@ static int codepoint (lua_State *L) {
     utfint code;
     s = utf8_decode(s, &code, !lax);
     if (s == NULL)
-      return luaL_error(L, "invalid UTF-8 code");
+      return luaL_error(L, MSGInvalid);
     lua_pushinteger(L, code);
     n++;
   }
@@ -194,16 +197,16 @@ static int byteoffset (lua_State *L) {
                    "position out of bounds");
   if (n == 0) {
     /* find beginning of current byte sequence */
-    while (posi > 0 && iscont(s + posi)) posi--;
+    while (posi > 0 && iscontp(s + posi)) posi--;
   }
   else {
-    if (iscont(s + posi))
+    if (iscontp(s + posi))
       return luaL_error(L, "initial position is a continuation byte");
     if (n < 0) {
        while (n < 0 && posi > 0) {  /* move back */
          do {  /* find beginning of previous character */
            posi--;
-         } while (posi > 0 && iscont(s + posi));
+         } while (posi > 0 && iscontp(s + posi));
          n++;
        }
      }
@@ -212,7 +215,7 @@ static int byteoffset (lua_State *L) {
        while (n > 0 && posi < (lua_Integer)len) {
          do {  /* find beginning of next character */
            posi++;
-         } while (iscont(s + posi));  /* (cannot pass final '\0') */
+         } while (iscontp(s + posi));  /* (cannot pass final '\0') */
          n--;
        }
      }
@@ -230,15 +233,15 @@ static int iter_aux (lua_State *L, int s
   const char *s = luaL_checklstring(L, 1, &len);
   lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2);
   if (n < len) {
-    while (iscont(s + n)) n++;  /* skip continuation bytes */
+    while (iscontp(s + n)) n++;  /* skip continuation bytes */
   }
   if (n >= len)  /* (also handles original 'n' being negative) */
     return 0;  /* no more codepoints */
   else {
     utfint code;
     const char *next = utf8_decode(s + n, &code, strict);
-    if (next == NULL)
-      return luaL_error(L, "invalid UTF-8 code");
+    if (next == NULL || iscontp(next))
+      return luaL_error(L, MSGInvalid);
     lua_pushinteger(L, n + 1);
     lua_pushinteger(L, code);
     return 2;
@@ -257,7 +260,8 @@ static int iter_auxlax (lua_State *L) {
 
 static int iter_codes (lua_State *L) {
   int lax = lua_toboolean(L, 2);
-  luaL_checkstring(L, 1);
+  const char *s = luaL_checkstring(L, 1);
+  luaL_argcheck(L, !iscontp(s), 1, MSGInvalid);
   lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict);
   lua_pushvalue(L, 1);
   lua_pushinteger(L, 0);



Home | Main Index | Thread Index | Old Index