lua: fix CVE-2014-5461
[openwrt/staging/jow.git] / package / utils / lua / patches / 013-lnum-strtoul-parsing-fixes.patch
1 --- a/src/lnum.c
2 +++ b/src/lnum.c
3 @@ -127,6 +127,8 @@ static int luaO_str2i (const char *s, lu
4 #else
5 return 0; /* Reject the number */
6 #endif
7 + } else if (v > LUA_INTEGER_MAX) {
8 + return TK_NUMBER;
9 }
10 } else if ((v > LUA_INTEGER_MAX) || (*endptr && (!isspace(*endptr)))) {
11 return TK_NUMBER; /* not in signed range, or has '.', 'e' etc. trailing */
12 @@ -310,3 +312,13 @@ int try_unmint( lua_Integer *r, lua_Inte
13 return 0;
14 }
15
16 +#ifdef LONG_OVERFLOW_LUA_INTEGER
17 +unsigned LUA_INTEGER lua_str2ul( const char *str, char **endptr, int base ) {
18 + unsigned long v= strtoul(str, endptr, base);
19 + if ( v > LUA_INTEGER_MAX ) {
20 + errno= ERANGE;
21 + v= ULONG_MAX;
22 + }
23 + return (unsigned LUA_INTEGER)v;
24 +}
25 +#endif
26 --- a/src/lnum_config.h
27 +++ b/src/lnum_config.h
28 @@ -141,7 +141,12 @@
29 #endif
30
31 #ifndef lua_str2ul
32 -# define lua_str2ul (unsigned LUA_INTEGER)strtoul
33 +# if LONG_MAX > LUA_INTEGER_MAX
34 +# define LONG_OVERFLOW_LUA_INTEGER
35 + unsigned LUA_INTEGER lua_str2ul( const char *str, char **endptr, int base );
36 +# else
37 +# define lua_str2ul (unsigned LUA_INTEGER)strtoul
38 +# endif
39 #endif
40 #ifndef LUA_INTEGER_MIN
41 # define LUA_INTEGER_MIN (-LUA_INTEGER_MAX -1) /* -2^16|32 */