diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-10-06 13:10:22 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-10-06 13:10:22 -0300 |
commit | 8949904783c2fdda1b6c6cec99637cf6d5471359 (patch) | |
tree | 3d5e5a7c91f325c3f66e87acd551eb29cb7657da | |
parent | 9294466234f4304fd1d31e343fb5ca48aec03b16 (diff) | |
download | lua-8949904783c2fdda1b6c6cec99637cf6d5471359.tar.gz lua-8949904783c2fdda1b6c6cec99637cf6d5471359.tar.bz2 lua-8949904783c2fdda1b6c6cec99637cf6d5471359.zip |
allow NULL string when length is zero in 'lua_pushlstring' and
'luaL_addlstring'
-rw-r--r-- | lapi.c | 10 | ||||
-rw-r--r-- | lauxlib.c | 10 |
2 files changed, 11 insertions, 9 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 2.254 2015/08/25 18:50:37 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.255 2015/09/09 13:45:50 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -472,15 +472,15 @@ LUA_API void lua_pushinteger (lua_State *L, lua_Integer n) { | |||
472 | 472 | ||
473 | 473 | ||
474 | /* | 474 | /* |
475 | ** Pushes on the stack a string with given length. Even when 'len' == 0, | 475 | ** Pushes on the stack a string with given length. Avoid using 's' when |
476 | ** 's' cannot be NULL due to later use of 'memcmp' and 'memcpy'. | 476 | ** 'len' == 0 (as 's' can be NULL in that case), due to later use of |
477 | ** 'memcmp' and 'memcpy'. | ||
477 | */ | 478 | */ |
478 | LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { | 479 | LUA_API const char *lua_pushlstring (lua_State *L, const char *s, size_t len) { |
479 | TString *ts; | 480 | TString *ts; |
480 | lua_lock(L); | 481 | lua_lock(L); |
481 | luaC_checkGC(L); | 482 | luaC_checkGC(L); |
482 | api_check(L, s != NULL, "pointer cannot be NULL"); | 483 | ts = (len == 0) ? luaS_new(L, "") : luaS_newlstr(L, s, len); |
483 | ts = luaS_newlstr(L, s, len); | ||
484 | setsvalue2s(L, L->top, ts); | 484 | setsvalue2s(L, L->top, ts); |
485 | api_incr_top(L); | 485 | api_incr_top(L); |
486 | lua_unlock(L); | 486 | lua_unlock(L); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.281 2015/06/18 14:23:14 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.282 2015/10/02 15:46:49 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -512,9 +512,11 @@ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { | |||
512 | 512 | ||
513 | 513 | ||
514 | LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { | 514 | LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { |
515 | char *b = luaL_prepbuffsize(B, l); | 515 | if (l > 0) { /* avoid 'memcpy' when 's' can be NULL */ |
516 | memcpy(b, s, l * sizeof(char)); | 516 | char *b = luaL_prepbuffsize(B, l); |
517 | luaL_addsize(B, l); | 517 | memcpy(b, s, l * sizeof(char)); |
518 | luaL_addsize(B, l); | ||
519 | } | ||
518 | } | 520 | } |
519 | 521 | ||
520 | 522 | ||