No need for 'volatile' in string.pack/unpack
Type punning an address to 'char *' should be always safe.
This commit is contained in:
17
lstrlib.c
17
lstrlib.c
@@ -1365,7 +1365,6 @@ typedef union Ftypes {
|
|||||||
float f;
|
float f;
|
||||||
double d;
|
double d;
|
||||||
lua_Number n;
|
lua_Number n;
|
||||||
char buff[5 * sizeof(lua_Number)]; /* enough for any float type */
|
|
||||||
} Ftypes;
|
} Ftypes;
|
||||||
|
|
||||||
|
|
||||||
@@ -1535,12 +1534,10 @@ static void packint (luaL_Buffer *b, lua_Unsigned n,
|
|||||||
** Copy 'size' bytes from 'src' to 'dest', correcting endianness if
|
** Copy 'size' bytes from 'src' to 'dest', correcting endianness if
|
||||||
** given 'islittle' is different from native endianness.
|
** given 'islittle' is different from native endianness.
|
||||||
*/
|
*/
|
||||||
static void copywithendian (volatile char *dest, volatile const char *src,
|
static void copywithendian (char *dest, const char *src,
|
||||||
int size, int islittle) {
|
int size, int islittle) {
|
||||||
if (islittle == nativeendian.little) {
|
if (islittle == nativeendian.little)
|
||||||
while (size-- != 0)
|
memcpy(dest, src, size);
|
||||||
*(dest++) = *(src++);
|
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
dest += size - 1;
|
dest += size - 1;
|
||||||
while (size-- != 0)
|
while (size-- != 0)
|
||||||
@@ -1584,14 +1581,14 @@ static int str_pack (lua_State *L) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Kfloat: { /* floating-point options */
|
case Kfloat: { /* floating-point options */
|
||||||
volatile Ftypes u;
|
Ftypes u;
|
||||||
char *buff = luaL_prepbuffsize(&b, size);
|
char *buff = luaL_prepbuffsize(&b, size);
|
||||||
lua_Number n = luaL_checknumber(L, arg); /* get argument */
|
lua_Number n = luaL_checknumber(L, arg); /* get argument */
|
||||||
if (size == sizeof(u.f)) u.f = (float)n; /* copy it into 'u' */
|
if (size == sizeof(u.f)) u.f = (float)n; /* copy it into 'u' */
|
||||||
else if (size == sizeof(u.d)) u.d = (double)n;
|
else if (size == sizeof(u.d)) u.d = (double)n;
|
||||||
else u.n = n;
|
else u.n = n;
|
||||||
/* move 'u' to final result, correcting endianness if needed */
|
/* move 'u' to final result, correcting endianness if needed */
|
||||||
copywithendian(buff, u.buff, size, h.islittle);
|
copywithendian(buff, (char *)&u, size, h.islittle);
|
||||||
luaL_addsize(&b, size);
|
luaL_addsize(&b, size);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1717,9 +1714,9 @@ static int str_unpack (lua_State *L) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Kfloat: {
|
case Kfloat: {
|
||||||
volatile Ftypes u;
|
Ftypes u;
|
||||||
lua_Number num;
|
lua_Number num;
|
||||||
copywithendian(u.buff, data + pos, size, h.islittle);
|
copywithendian((char *)&u, data + pos, size, h.islittle);
|
||||||
if (size == sizeof(u.f)) num = (lua_Number)u.f;
|
if (size == sizeof(u.f)) num = (lua_Number)u.f;
|
||||||
else if (size == sizeof(u.d)) num = (lua_Number)u.d;
|
else if (size == sizeof(u.d)) num = (lua_Number)u.d;
|
||||||
else num = u.n;
|
else num = u.n;
|
||||||
|
|||||||
Reference in New Issue
Block a user