1
0

013-lnum-strtoul-parsing-fixes.patch 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. diff --git a/src/lnum.c b/src/lnum.c
  2. index 1456b6a2ed23..b0632b04c2b7 100644
  3. --- a/src/lnum.c
  4. +++ b/src/lnum.c
  5. @@ -127,6 +127,8 @@ static int luaO_str2i (const char *s, lua_Integer *res, char **endptr_ref) {
  6. #else
  7. return 0; /* Reject the number */
  8. #endif
  9. + } else if (v > LUA_INTEGER_MAX) {
  10. + return TK_NUMBER;
  11. }
  12. } else if ((v > LUA_INTEGER_MAX) || (*endptr && (!isspace(*endptr)))) {
  13. return TK_NUMBER; /* not in signed range, or has '.', 'e' etc. trailing */
  14. @@ -310,3 +312,13 @@ int try_unmint( lua_Integer *r, lua_Integer ib ) {
  15. return 0;
  16. }
  17. +#ifdef LONG_OVERFLOW_LUA_INTEGER
  18. +unsigned LUA_INTEGER lua_str2ul( const char *str, char **endptr, int base ) {
  19. + unsigned long v= strtoul(str, endptr, base);
  20. + if ( v > LUA_INTEGER_MAX ) {
  21. + errno= ERANGE;
  22. + v= ULONG_MAX;
  23. + }
  24. + return (unsigned LUA_INTEGER)v;
  25. +}
  26. +#endif
  27. diff --git a/src/lnum_config.h b/src/lnum_config.h
  28. index 19d7a4231a49..1092eead6629 100644
  29. --- a/src/lnum_config.h
  30. +++ b/src/lnum_config.h
  31. @@ -141,7 +141,12 @@
  32. #endif
  33. #ifndef lua_str2ul
  34. -# define lua_str2ul (unsigned LUA_INTEGER)strtoul
  35. +# if LONG_MAX > LUA_INTEGER_MAX
  36. +# define LONG_OVERFLOW_LUA_INTEGER
  37. + unsigned LUA_INTEGER lua_str2ul( const char *str, char **endptr, int base );
  38. +# else
  39. +# define lua_str2ul (unsigned LUA_INTEGER)strtoul
  40. +# endif
  41. #endif
  42. #ifndef LUA_INTEGER_MIN
  43. # define LUA_INTEGER_MIN (-LUA_INTEGER_MAX -1) /* -2^16|32 */
  44. --
  45. 1.9.1