trying to avoid assumption that sizeof(char)==1

This commit is contained in:
Roberto Ierusalimschy 2011-02-07 17:15:24 -02:00
parent fd6c1f4898
commit e7a9c45a48
5 changed files with 13 additions and 12 deletions

View File

@ -1,5 +1,5 @@
/*
** $Id: loadlib.c,v 1.94 2010/11/10 20:00:04 roberto Exp roberto $
** $Id: loadlib.c,v 1.95 2011/01/07 18:54:49 roberto Exp roberto $
** Dynamic library loader for Lua
** See Copyright Notice in lua.h
**
@ -164,7 +164,7 @@ static void pusherror (lua_State *L) {
int error = GetLastError();
char buffer[128];
if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, error, 0, buffer, sizeof(buffer), NULL))
NULL, error, 0, buffer, sizeof(buffer)/sizeof(char), NULL))
lua_pushstring(L, buffer);
else
lua_pushfstring(L, "system error %d\n", error);

View File

@ -1,5 +1,5 @@
/*
** $Id: lobject.c,v 2.44 2010/12/06 21:08:36 roberto Exp roberto $
** $Id: lobject.c,v 2.45 2010/12/10 19:03:46 roberto Exp roberto $
** Some generic functions over Lua objects
** See Copyright Notice in lua.h
*/
@ -265,7 +265,7 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
#define LL(x) (sizeof(x) - 1)
#define LL(x) ((sizeof(x) - 1)/sizeof(char))
#define RETS "..."
#define PRE "[string \""
#define POS "\"]"

View File

@ -1,5 +1,5 @@
/*
** $Id: lstrlib.c,v 1.162 2011/01/12 20:36:01 roberto Exp roberto $
** $Id: lstrlib.c,v 1.163 2011/01/26 16:30:02 roberto Exp roberto $
** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h
*/
@ -808,7 +808,7 @@ static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
const char *p = strfrmt;
while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
if ((size_t)(p - strfrmt) >= sizeof(FLAGS))
if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char))
luaL_error(L, "invalid format (repeated flags)");
if (isdigit(uchar(*p))) p++; /* skip width */
if (isdigit(uchar(*p))) p++; /* (2 digits at most) */

View File

@ -1,5 +1,5 @@
/*
** $Id: lundump.c,v 1.67 2010/10/13 21:04:52 lhf Exp $
** $Id: lundump.c,v 2.14 2010/10/25 14:33:38 roberto Exp roberto $
** load precompiled Lua chunks
** See Copyright Notice in lua.h
*/
@ -211,8 +211,8 @@ Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
void luaU_header (char* h)
{
int x=1;
memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
h+=sizeof(LUA_SIGNATURE)-1;
memcpy(h,LUA_SIGNATURE,(sizeof(LUA_SIGNATURE)-1)*sizeof(char));
h+=(sizeof(LUA_SIGNATURE)-1)*sizeof(char);
*h++=(char)LUAC_VERSION;
*h++=(char)LUAC_FORMAT;
*h++=(char)*(char*)&x; /* endianness */

7
lvm.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lvm.c,v 2.129 2011/02/01 18:32:55 roberto Exp roberto $
** $Id: lvm.c,v 2.130 2011/02/07 12:24:42 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -288,14 +288,15 @@ void luaV_concat (lua_State *L, int total) {
/* collect total length */
for (n = 1; n < total && tostring(L, top-n-1); n++) {
size_t l = tsvalue(top-n-1)->len;
if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
if (l >= (MAX_SIZET/sizeof(char)) - tl)
luaG_runerror(L, "string length overflow");
tl += l;
}
buffer = luaZ_openspace(L, &G(L)->buff, tl);
tl = 0;
for (i=n; i>0; i--) { /* concat all strings */
size_t l = tsvalue(top-i)->len;
memcpy(buffer+tl, svalue(top-i), l);
memcpy(buffer+tl, svalue(top-i), l * sizeof(char));
tl += l;
}
setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));