aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2020-10-05 10:38:41 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2020-10-12 12:29:09 -0300
commitfb172d0a929432856983a51d4139f705d4c01365 (patch)
treed6f1e18a818b12689c785b8c0789a868339a6753
parent0085db4596df99b43fc245f71ee444da68cb830b (diff)
downloadlua-fb172d0a929432856983a51d4139f705d4c01365.tar.gz
lua-fb172d0a929432856983a51d4139f705d4c01365.tar.bz2
lua-fb172d0a929432856983a51d4139f705d4c01365.zip
No need for 'volatile' in string.pack/unpack
Type punning an address to 'char *' should be always safe.
-rw-r--r--lstrlib.c17
1 files changed, 7 insertions, 10 deletions
diff --git a/lstrlib.c b/lstrlib.c
index a30ec5af..940a14ca 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1365,7 +1365,6 @@ typedef union Ftypes {
1365 float f; 1365 float f;
1366 double d; 1366 double d;
1367 lua_Number n; 1367 lua_Number n;
1368 char buff[5 * sizeof(lua_Number)]; /* enough for any float type */
1369} Ftypes; 1368} Ftypes;
1370 1369
1371 1370
@@ -1535,12 +1534,10 @@ static void packint (luaL_Buffer *b, lua_Unsigned n,
1535** Copy 'size' bytes from 'src' to 'dest', correcting endianness if 1534** Copy 'size' bytes from 'src' to 'dest', correcting endianness if
1536** given 'islittle' is different from native endianness. 1535** given 'islittle' is different from native endianness.
1537*/ 1536*/
1538static void copywithendian (volatile char *dest, volatile const char *src, 1537static void copywithendian (char *dest, const char *src,
1539 int size, int islittle) { 1538 int size, int islittle) {
1540 if (islittle == nativeendian.little) { 1539 if (islittle == nativeendian.little)
1541 while (size-- != 0) 1540 memcpy(dest, src, size);
1542 *(dest++) = *(src++);
1543 }
1544 else { 1541 else {
1545 dest += size - 1; 1542 dest += size - 1;
1546 while (size-- != 0) 1543 while (size-- != 0)
@@ -1584,14 +1581,14 @@ static int str_pack (lua_State *L) {
1584 break; 1581 break;
1585 } 1582 }
1586 case Kfloat: { /* floating-point options */ 1583 case Kfloat: { /* floating-point options */
1587 volatile Ftypes u; 1584 Ftypes u;
1588 char *buff = luaL_prepbuffsize(&b, size); 1585 char *buff = luaL_prepbuffsize(&b, size);
1589 lua_Number n = luaL_checknumber(L, arg); /* get argument */ 1586 lua_Number n = luaL_checknumber(L, arg); /* get argument */
1590 if (size == sizeof(u.f)) u.f = (float)n; /* copy it into 'u' */ 1587 if (size == sizeof(u.f)) u.f = (float)n; /* copy it into 'u' */
1591 else if (size == sizeof(u.d)) u.d = (double)n; 1588 else if (size == sizeof(u.d)) u.d = (double)n;
1592 else u.n = n; 1589 else u.n = n;
1593 /* move 'u' to final result, correcting endianness if needed */ 1590 /* move 'u' to final result, correcting endianness if needed */
1594 copywithendian(buff, u.buff, size, h.islittle); 1591 copywithendian(buff, (char *)&u, size, h.islittle);
1595 luaL_addsize(&b, size); 1592 luaL_addsize(&b, size);
1596 break; 1593 break;
1597 } 1594 }
@@ -1717,9 +1714,9 @@ static int str_unpack (lua_State *L) {
1717 break; 1714 break;
1718 } 1715 }
1719 case Kfloat: { 1716 case Kfloat: {
1720 volatile Ftypes u; 1717 Ftypes u;
1721 lua_Number num; 1718 lua_Number num;
1722 copywithendian(u.buff, data + pos, size, h.islittle); 1719 copywithendian((char *)&u, data + pos, size, h.islittle);
1723 if (size == sizeof(u.f)) num = (lua_Number)u.f; 1720 if (size == sizeof(u.f)) num = (lua_Number)u.f;
1724 else if (size == sizeof(u.d)) num = (lua_Number)u.d; 1721 else if (size == sizeof(u.d)) num = (lua_Number)u.d;
1725 else num = u.n; 1722 else num = u.n;