aboutsummaryrefslogtreecommitdiff
path: root/lstrlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-07-27 13:32:59 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-07-27 13:32:59 -0300
commit0acd55898d0aaae8dbc14c8a1bc1e3bdffc8701b (patch)
tree0ac634fed90877130b1f102bf4075af999de2158 /lstrlib.c
parent15231d4fb2f6984b25e0353ff46eda1a180b686d (diff)
downloadlua-0acd55898d0aaae8dbc14c8a1bc1e3bdffc8701b.tar.gz
lua-0acd55898d0aaae8dbc14c8a1bc1e3bdffc8701b.tar.bz2
lua-0acd55898d0aaae8dbc14c8a1bc1e3bdffc8701b.zip
Added gcc option '-Wconversion'
No warnings for standard numerical types. Still pending alternative numerical types.
Diffstat (limited to 'lstrlib.c')
-rw-r--r--lstrlib.c132
1 files changed, 69 insertions, 63 deletions
diff --git a/lstrlib.c b/lstrlib.c
index 8d6573a6..e9421c27 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -113,7 +113,7 @@ static int str_lower (lua_State *L) {
113 const char *s = luaL_checklstring(L, 1, &l); 113 const char *s = luaL_checklstring(L, 1, &l);
114 char *p = luaL_buffinitsize(L, &b, l); 114 char *p = luaL_buffinitsize(L, &b, l);
115 for (i=0; i<l; i++) 115 for (i=0; i<l; i++)
116 p[i] = tolower(cast_uchar(s[i])); 116 p[i] = cast_char(tolower(cast_uchar(s[i])));
117 luaL_pushresultsize(&b, l); 117 luaL_pushresultsize(&b, l);
118 return 1; 118 return 1;
119} 119}
@@ -126,7 +126,7 @@ static int str_upper (lua_State *L) {
126 const char *s = luaL_checklstring(L, 1, &l); 126 const char *s = luaL_checklstring(L, 1, &l);
127 char *p = luaL_buffinitsize(L, &b, l); 127 char *p = luaL_buffinitsize(L, &b, l);
128 for (i=0; i<l; i++) 128 for (i=0; i<l; i++)
129 p[i] = toupper(cast_uchar(s[i])); 129 p[i] = cast_char(toupper(cast_uchar(s[i])));
130 luaL_pushresultsize(&b, l); 130 luaL_pushresultsize(&b, l);
131 return 1; 131 return 1;
132} 132}
@@ -139,7 +139,7 @@ static int str_rep (lua_State *L) {
139 const char *sep = luaL_optlstring(L, 3, "", &lsep); 139 const char *sep = luaL_optlstring(L, 3, "", &lsep);
140 if (n <= 0) 140 if (n <= 0)
141 lua_pushliteral(L, ""); 141 lua_pushliteral(L, "");
142 else if (l_unlikely(l + lsep < l || l + lsep > MAX_SIZE / n)) 142 else if (l_unlikely(l + lsep < l || l + lsep > MAX_SIZE / cast_sizet(n)))
143 return luaL_error(L, "resulting string too large"); 143 return luaL_error(L, "resulting string too large");
144 else { 144 else {
145 size_t totallen = ((size_t)n * (l + lsep)) - lsep; 145 size_t totallen = ((size_t)n * (l + lsep)) - lsep;
@@ -172,7 +172,7 @@ static int str_byte (lua_State *L) {
172 n = (int)(pose - posi) + 1; 172 n = (int)(pose - posi) + 1;
173 luaL_checkstack(L, n, "string slice too long"); 173 luaL_checkstack(L, n, "string slice too long");
174 for (i=0; i<n; i++) 174 for (i=0; i<n; i++)
175 lua_pushinteger(L, cast_uchar(s[posi+i-1])); 175 lua_pushinteger(L, cast_uchar(s[posi + cast_uint(i) - 1]));
176 return n; 176 return n;
177} 177}
178 178
@@ -181,13 +181,13 @@ static int str_char (lua_State *L) {
181 int n = lua_gettop(L); /* number of arguments */ 181 int n = lua_gettop(L); /* number of arguments */
182 int i; 182 int i;
183 luaL_Buffer b; 183 luaL_Buffer b;
184 char *p = luaL_buffinitsize(L, &b, n); 184 char *p = luaL_buffinitsize(L, &b, cast_uint(n));
185 for (i=1; i<=n; i++) { 185 for (i=1; i<=n; i++) {
186 lua_Unsigned c = (lua_Unsigned)luaL_checkinteger(L, i); 186 lua_Unsigned c = (lua_Unsigned)luaL_checkinteger(L, i);
187 luaL_argcheck(L, c <= (lua_Unsigned)UCHAR_MAX, i, "value out of range"); 187 luaL_argcheck(L, c <= (lua_Unsigned)UCHAR_MAX, i, "value out of range");
188 p[i - 1] = cast_uchar(c); 188 p[i - 1] = cast_char(cast_uchar(c));
189 } 189 }
190 luaL_pushresultsize(&b, n); 190 luaL_pushresultsize(&b, cast_uint(n));
191 return 1; 191 return 1;
192} 192}
193 193
@@ -352,10 +352,10 @@ typedef struct MatchState {
352 const char *p_end; /* end ('\0') of pattern */ 352 const char *p_end; /* end ('\0') of pattern */
353 lua_State *L; 353 lua_State *L;
354 int matchdepth; /* control for recursive depth (to avoid C stack overflow) */ 354 int matchdepth; /* control for recursive depth (to avoid C stack overflow) */
355 unsigned char level; /* total number of captures (finished or unfinished) */ 355 int level; /* total number of captures (finished or unfinished) */
356 struct { 356 struct {
357 const char *init; 357 const char *init;
358 ptrdiff_t len; 358 ptrdiff_t len; /* length or special value (CAP_*) */
359 } capture[LUA_MAXCAPTURES]; 359 } capture[LUA_MAXCAPTURES];
360} MatchState; 360} MatchState;
361 361
@@ -550,7 +550,7 @@ static const char *end_capture (MatchState *ms, const char *s,
550static const char *match_capture (MatchState *ms, const char *s, int l) { 550static const char *match_capture (MatchState *ms, const char *s, int l) {
551 size_t len; 551 size_t len;
552 l = check_capture(ms, l); 552 l = check_capture(ms, l);
553 len = ms->capture[l].len; 553 len = cast_sizet(ms->capture[l].len);
554 if ((size_t)(ms->src_end-s) >= len && 554 if ((size_t)(ms->src_end-s) >= len &&
555 memcmp(ms->capture[l].init, s, len) == 0) 555 memcmp(ms->capture[l].init, s, len) == 0)
556 return s+len; 556 return s+len;
@@ -674,7 +674,7 @@ static const char *lmemfind (const char *s1, size_t l1,
674 if (memcmp(init, s2+1, l2) == 0) 674 if (memcmp(init, s2+1, l2) == 0)
675 return init-1; 675 return init-1;
676 else { /* correct 'l1' and 's1' to try again */ 676 else { /* correct 'l1' and 's1' to try again */
677 l1 -= init-s1; 677 l1 -= ct_diff2sz(init - s1);
678 s1 = init; 678 s1 = init;
679 } 679 }
680 } 680 }
@@ -690,13 +690,13 @@ static const char *lmemfind (const char *s1, size_t l1,
690** its length and put its address in '*cap'. If it is an integer 690** its length and put its address in '*cap'. If it is an integer
691** (a position), push it on the stack and return CAP_POSITION. 691** (a position), push it on the stack and return CAP_POSITION.
692*/ 692*/
693static size_t get_onecapture (MatchState *ms, int i, const char *s, 693static ptrdiff_t get_onecapture (MatchState *ms, int i, const char *s,
694 const char *e, const char **cap) { 694 const char *e, const char **cap) {
695 if (i >= ms->level) { 695 if (i >= ms->level) {
696 if (l_unlikely(i != 0)) 696 if (l_unlikely(i != 0))
697 luaL_error(ms->L, "invalid capture index %%%d", i + 1); 697 luaL_error(ms->L, "invalid capture index %%%d", i + 1);
698 *cap = s; 698 *cap = s;
699 return e - s; 699 return (e - s);
700 } 700 }
701 else { 701 else {
702 ptrdiff_t capl = ms->capture[i].len; 702 ptrdiff_t capl = ms->capture[i].len;
@@ -718,7 +718,7 @@ static void push_onecapture (MatchState *ms, int i, const char *s,
718 const char *cap; 718 const char *cap;
719 ptrdiff_t l = get_onecapture(ms, i, s, e, &cap); 719 ptrdiff_t l = get_onecapture(ms, i, s, e, &cap);
720 if (l != CAP_POSITION) 720 if (l != CAP_POSITION)
721 lua_pushlstring(ms->L, cap, l); 721 lua_pushlstring(ms->L, cap, cast_sizet(l));
722 /* else position was already pushed */ 722 /* else position was already pushed */
723} 723}
724 724
@@ -776,7 +776,7 @@ static int str_find_aux (lua_State *L, int find) {
776 const char *s2 = lmemfind(s + init, ls - init, p, lp); 776 const char *s2 = lmemfind(s + init, ls - init, p, lp);
777 if (s2) { 777 if (s2) {
778 lua_pushinteger(L, (s2 - s) + 1); 778 lua_pushinteger(L, (s2 - s) + 1);
779 lua_pushinteger(L, (s2 - s) + lp); 779 lua_pushinteger(L, cast_st2S(ct_diff2sz(s2 - s) + lp));
780 return 2; 780 return 2;
781 } 781 }
782 } 782 }
@@ -866,23 +866,23 @@ static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
866 const char *news = lua_tolstring(L, 3, &l); 866 const char *news = lua_tolstring(L, 3, &l);
867 const char *p; 867 const char *p;
868 while ((p = (char *)memchr(news, L_ESC, l)) != NULL) { 868 while ((p = (char *)memchr(news, L_ESC, l)) != NULL) {
869 luaL_addlstring(b, news, p - news); 869 luaL_addlstring(b, news, ct_diff2sz(p - news));
870 p++; /* skip ESC */ 870 p++; /* skip ESC */
871 if (*p == L_ESC) /* '%%' */ 871 if (*p == L_ESC) /* '%%' */
872 luaL_addchar(b, *p); 872 luaL_addchar(b, *p);
873 else if (*p == '0') /* '%0' */ 873 else if (*p == '0') /* '%0' */
874 luaL_addlstring(b, s, e - s); 874 luaL_addlstring(b, s, ct_diff2sz(e - s));
875 else if (isdigit(cast_uchar(*p))) { /* '%n' */ 875 else if (isdigit(cast_uchar(*p))) { /* '%n' */
876 const char *cap; 876 const char *cap;
877 ptrdiff_t resl = get_onecapture(ms, *p - '1', s, e, &cap); 877 ptrdiff_t resl = get_onecapture(ms, *p - '1', s, e, &cap);
878 if (resl == CAP_POSITION) 878 if (resl == CAP_POSITION)
879 luaL_addvalue(b); /* add position to accumulated result */ 879 luaL_addvalue(b); /* add position to accumulated result */
880 else 880 else
881 luaL_addlstring(b, cap, resl); 881 luaL_addlstring(b, cap, cast_sizet(resl));
882 } 882 }
883 else 883 else
884 luaL_error(L, "invalid use of '%c' in replacement string", L_ESC); 884 luaL_error(L, "invalid use of '%c' in replacement string", L_ESC);
885 l -= p + 1 - news; 885 l -= ct_diff2sz(p + 1 - news);
886 news = p + 1; 886 news = p + 1;
887 } 887 }
888 luaL_addlstring(b, news, l); 888 luaL_addlstring(b, news, l);
@@ -917,7 +917,7 @@ static int add_value (MatchState *ms, luaL_Buffer *b, const char *s,
917 } 917 }
918 if (!lua_toboolean(L, -1)) { /* nil or false? */ 918 if (!lua_toboolean(L, -1)) { /* nil or false? */
919 lua_pop(L, 1); /* remove value */ 919 lua_pop(L, 1); /* remove value */
920 luaL_addlstring(b, s, e - s); /* keep original text */ 920 luaL_addlstring(b, s, ct_diff2sz(e - s)); /* keep original text */
921 return 0; /* no changes */ 921 return 0; /* no changes */
922 } 922 }
923 else if (l_unlikely(!lua_isstring(L, -1))) 923 else if (l_unlikely(!lua_isstring(L, -1)))
@@ -936,7 +936,8 @@ static int str_gsub (lua_State *L) {
936 const char *p = luaL_checklstring(L, 2, &lp); /* pattern */ 936 const char *p = luaL_checklstring(L, 2, &lp); /* pattern */
937 const char *lastmatch = NULL; /* end of last match */ 937 const char *lastmatch = NULL; /* end of last match */
938 int tr = lua_type(L, 3); /* replacement type */ 938 int tr = lua_type(L, 3); /* replacement type */
939 lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); /* max replacements */ 939 /* max replacements */
940 lua_Integer max_s = luaL_optinteger(L, 4, cast_st2S(srcl) + 1);
940 int anchor = (*p == '^'); 941 int anchor = (*p == '^');
941 lua_Integer n = 0; /* replacement count */ 942 lua_Integer n = 0; /* replacement count */
942 int changed = 0; /* change flag */ 943 int changed = 0; /* change flag */
@@ -966,7 +967,7 @@ static int str_gsub (lua_State *L) {
966 if (!changed) /* no changes? */ 967 if (!changed) /* no changes? */
967 lua_pushvalue(L, 1); /* return original string */ 968 lua_pushvalue(L, 1); /* return original string */
968 else { /* something changed */ 969 else { /* something changed */
969 luaL_addlstring(&b, src, ms.src_end-src); 970 luaL_addlstring(&b, src, ct_diff2sz(ms.src_end - src));
970 luaL_pushresult(&b); /* create and return new string */ 971 luaL_pushresult(&b); /* create and return new string */
971 } 972 }
972 lua_pushinteger(L, n); /* number of substitutions */ 973 lua_pushinteger(L, n); /* number of substitutions */
@@ -1004,15 +1005,15 @@ static int str_gsub (lua_State *L) {
1004/* 1005/*
1005** Add integer part of 'x' to buffer and return new 'x' 1006** Add integer part of 'x' to buffer and return new 'x'
1006*/ 1007*/
1007static lua_Number adddigit (char *buff, int n, lua_Number x) { 1008static lua_Number adddigit (char *buff, unsigned n, lua_Number x) {
1008 lua_Number dd = l_mathop(floor)(x); /* get integer part from 'x' */ 1009 lua_Number dd = l_mathop(floor)(x); /* get integer part from 'x' */
1009 int d = (int)dd; 1010 int d = (int)dd;
1010 buff[n] = (d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */ 1011 buff[n] = cast_char(d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */
1011 return x - dd; /* return what is left */ 1012 return x - dd; /* return what is left */
1012} 1013}
1013 1014
1014 1015
1015static int num2straux (char *buff, int sz, lua_Number x) { 1016static int num2straux (char *buff, unsigned sz, lua_Number x) {
1016 /* if 'inf' or 'NaN', format it like '%g' */ 1017 /* if 'inf' or 'NaN', format it like '%g' */
1017 if (x != x || x == (lua_Number)HUGE_VAL || x == -(lua_Number)HUGE_VAL) 1018 if (x != x || x == (lua_Number)HUGE_VAL || x == -(lua_Number)HUGE_VAL)
1018 return l_sprintf(buff, sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)x); 1019 return l_sprintf(buff, sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)x);
@@ -1023,7 +1024,7 @@ static int num2straux (char *buff, int sz, lua_Number x) {
1023 else { 1024 else {
1024 int e; 1025 int e;
1025 lua_Number m = l_mathop(frexp)(x, &e); /* 'x' fraction and exponent */ 1026 lua_Number m = l_mathop(frexp)(x, &e); /* 'x' fraction and exponent */
1026 int n = 0; /* character count */ 1027 unsigned n = 0; /* character count */
1027 if (m < 0) { /* is number negative? */ 1028 if (m < 0) { /* is number negative? */
1028 buff[n++] = '-'; /* add sign */ 1029 buff[n++] = '-'; /* add sign */
1029 m = -m; /* make it positive */ 1030 m = -m; /* make it positive */
@@ -1037,20 +1038,20 @@ static int num2straux (char *buff, int sz, lua_Number x) {
1037 m = adddigit(buff, n++, m * 16); 1038 m = adddigit(buff, n++, m * 16);
1038 } while (m > 0); 1039 } while (m > 0);
1039 } 1040 }
1040 n += l_sprintf(buff + n, sz - n, "p%+d", e); /* add exponent */ 1041 n += cast_uint(l_sprintf(buff + n, sz - n, "p%+d", e)); /* add exponent */
1041 lua_assert(n < sz); 1042 lua_assert(n < sz);
1042 return n; 1043 return cast_int(n);
1043 } 1044 }
1044} 1045}
1045 1046
1046 1047
1047static int lua_number2strx (lua_State *L, char *buff, int sz, 1048static int lua_number2strx (lua_State *L, char *buff, unsigned sz,
1048 const char *fmt, lua_Number x) { 1049 const char *fmt, lua_Number x) {
1049 int n = num2straux(buff, sz, x); 1050 int n = num2straux(buff, sz, x);
1050 if (fmt[SIZELENMOD] == 'A') { 1051 if (fmt[SIZELENMOD] == 'A') {
1051 int i; 1052 int i;
1052 for (i = 0; i < n; i++) 1053 for (i = 0; i < n; i++)
1053 buff[i] = toupper(cast_uchar(buff[i])); 1054 buff[i] = cast_char(toupper(cast_uchar(buff[i])));
1054 } 1055 }
1055 else if (l_unlikely(fmt[SIZELENMOD] != 'a')) 1056 else if (l_unlikely(fmt[SIZELENMOD] != 'a'))
1056 return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented"); 1057 return luaL_error(L, "modifiers for format '%%a'/'%%A' not implemented");
@@ -1151,9 +1152,9 @@ static int quotefloat (lua_State *L, char *buff, lua_Number n) {
1151 int nb = lua_number2strx(L, buff, MAX_ITEM, 1152 int nb = lua_number2strx(L, buff, MAX_ITEM,
1152 "%" LUA_NUMBER_FRMLEN "a", n); 1153 "%" LUA_NUMBER_FRMLEN "a", n);
1153 /* ensures that 'buff' string uses a dot as the radix character */ 1154 /* ensures that 'buff' string uses a dot as the radix character */
1154 if (memchr(buff, '.', nb) == NULL) { /* no dot? */ 1155 if (memchr(buff, '.', cast_uint(nb)) == NULL) { /* no dot? */
1155 char point = lua_getlocaledecpoint(); /* try locale point */ 1156 char point = lua_getlocaledecpoint(); /* try locale point */
1156 char *ppoint = (char *)memchr(buff, point, nb); 1157 char *ppoint = (char *)memchr(buff, point, cast_uint(nb));
1157 if (ppoint) *ppoint = '.'; /* change it to a dot */ 1158 if (ppoint) *ppoint = '.'; /* change it to a dot */
1158 } 1159 }
1159 return nb; 1160 return nb;
@@ -1183,7 +1184,7 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) {
1183 : LUA_INTEGER_FMT; /* else use default format */ 1184 : LUA_INTEGER_FMT; /* else use default format */
1184 nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n); 1185 nb = l_sprintf(buff, MAX_ITEM, format, (LUAI_UACINT)n);
1185 } 1186 }
1186 luaL_addsize(b, nb); 1187 luaL_addsize(b, cast_uint(nb));
1187 break; 1188 break;
1188 } 1189 }
1189 case LUA_TNIL: case LUA_TBOOLEAN: { 1190 case LUA_TNIL: case LUA_TBOOLEAN: {
@@ -1277,7 +1278,7 @@ static int str_format (lua_State *L) {
1277 luaL_addchar(&b, *strfrmt++); /* %% */ 1278 luaL_addchar(&b, *strfrmt++); /* %% */
1278 else { /* format item */ 1279 else { /* format item */
1279 char form[MAX_FORMAT]; /* to store the format ('%...') */ 1280 char form[MAX_FORMAT]; /* to store the format ('%...') */
1280 int maxitem = MAX_ITEM; /* maximum length for the result */ 1281 unsigned maxitem = MAX_ITEM; /* maximum length for the result */
1281 char *buff = luaL_prepbuffsize(&b, maxitem); /* to put result */ 1282 char *buff = luaL_prepbuffsize(&b, maxitem); /* to put result */
1282 int nb = 0; /* number of bytes in result */ 1283 int nb = 0; /* number of bytes in result */
1283 if (++arg > top) 1284 if (++arg > top)
@@ -1360,8 +1361,8 @@ static int str_format (lua_State *L) {
1360 return luaL_error(L, "invalid conversion '%s' to 'format'", form); 1361 return luaL_error(L, "invalid conversion '%s' to 'format'", form);
1361 } 1362 }
1362 } 1363 }
1363 lua_assert(nb < maxitem); 1364 lua_assert(cast_uint(nb) < maxitem);
1364 luaL_addsize(&b, nb); 1365 luaL_addsize(&b, cast_uint(nb));
1365 } 1366 }
1366 } 1367 }
1367 luaL_pushresult(&b); 1368 luaL_pushresult(&b);
@@ -1409,7 +1410,7 @@ static const union {
1409typedef struct Header { 1410typedef struct Header {
1410 lua_State *L; 1411 lua_State *L;
1411 int islittle; 1412 int islittle;
1412 int maxalign; 1413 unsigned maxalign;
1413} Header; 1414} Header;
1414 1415
1415 1416
@@ -1443,7 +1444,7 @@ static size_t getnum (const char **fmt, size_t df) {
1443 else { 1444 else {
1444 size_t a = 0; 1445 size_t a = 0;
1445 do { 1446 do {
1446 a = a*10 + (*((*fmt)++) - '0'); 1447 a = a*10 + cast_uint(*((*fmt)++) - '0');
1447 } while (digit(**fmt) && a <= (MAX_SIZE - 9)/10); 1448 } while (digit(**fmt) && a <= (MAX_SIZE - 9)/10);
1448 return a; 1449 return a;
1449 } 1450 }
@@ -1454,12 +1455,12 @@ static size_t getnum (const char **fmt, size_t df) {
1454** Read an integer numeral and raises an error if it is larger 1455** Read an integer numeral and raises an error if it is larger
1455** than the maximum size of integers. 1456** than the maximum size of integers.
1456*/ 1457*/
1457static int getnumlimit (Header *h, const char **fmt, int df) { 1458static unsigned getnumlimit (Header *h, const char **fmt, size_t df) {
1458 size_t sz = getnum(fmt, df); 1459 size_t sz = getnum(fmt, df);
1459 if (l_unlikely((sz - 1u) >= MAXINTSIZE)) 1460 if (l_unlikely((sz - 1u) >= MAXINTSIZE))
1460 return luaL_error(h->L, "integral size (%d) out of limits [1,%d]", 1461 return cast_uint(luaL_error(h->L,
1461 sz, MAXINTSIZE); 1462 "integral size (%d) out of limits [1,%d]", sz, MAXINTSIZE));
1462 return cast_int(sz); 1463 return cast_uint(sz);
1463} 1464}
1464 1465
1465 1466
@@ -1510,7 +1511,7 @@ static KOption getoption (Header *h, const char **fmt, size_t *size) {
1510 case '>': h->islittle = 0; break; 1511 case '>': h->islittle = 0; break;
1511 case '=': h->islittle = nativeendian.little; break; 1512 case '=': h->islittle = nativeendian.little; break;
1512 case '!': { 1513 case '!': {
1513 const int maxalign = offsetof(struct cD, u); 1514 const size_t maxalign = offsetof(struct cD, u);
1514 h->maxalign = getnumlimit(h, fmt, maxalign); 1515 h->maxalign = getnumlimit(h, fmt, maxalign);
1515 break; 1516 break;
1516 } 1517 }
@@ -1529,8 +1530,8 @@ static KOption getoption (Header *h, const char **fmt, size_t *size) {
1529** the maximum alignment ('maxalign'). Kchar option needs no alignment 1530** the maximum alignment ('maxalign'). Kchar option needs no alignment
1530** despite its size. 1531** despite its size.
1531*/ 1532*/
1532static KOption getdetails (Header *h, size_t totalsize, 1533static KOption getdetails (Header *h, size_t totalsize, const char **fmt,
1533 const char **fmt, size_t *psize, int *ntoalign) { 1534 size_t *psize, unsigned *ntoalign) {
1534 KOption opt = getoption(h, fmt, psize); 1535 KOption opt = getoption(h, fmt, psize);
1535 size_t align = *psize; /* usually, alignment follows size */ 1536 size_t align = *psize; /* usually, alignment follows size */
1536 if (opt == Kpaddalign) { /* 'X' gets alignment from following option */ 1537 if (opt == Kpaddalign) { /* 'X' gets alignment from following option */
@@ -1540,11 +1541,15 @@ static KOption getdetails (Header *h, size_t totalsize,
1540 if (align <= 1 || opt == Kchar) /* need no alignment? */ 1541 if (align <= 1 || opt == Kchar) /* need no alignment? */
1541 *ntoalign = 0; 1542 *ntoalign = 0;
1542 else { 1543 else {
1543 if (align > cast_sizet(h->maxalign)) /* enforce maximum alignment */ 1544 if (align > h->maxalign) /* enforce maximum alignment */
1544 align = h->maxalign; 1545 align = h->maxalign;
1545 if (l_unlikely(!ispow2(align))) /* not a power of 2? */ 1546 if (l_unlikely(!ispow2(align))) /* not a power of 2? */
1546 luaL_argerror(h->L, 1, "format asks for alignment not power of 2"); 1547 luaL_argerror(h->L, 1, "format asks for alignment not power of 2");
1547 *ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1); 1548 else {
1549 /* 'szmoda' = totalsize % align */
1550 unsigned szmoda = cast_uint(totalsize & (align - 1));
1551 *ntoalign = cast_uint((align - szmoda) & (align - 1));
1552 }
1548 } 1553 }
1549 return opt; 1554 return opt;
1550} 1555}
@@ -1557,9 +1562,9 @@ static KOption getdetails (Header *h, size_t totalsize,
1557** bytes if necessary (by default they would be zeros). 1562** bytes if necessary (by default they would be zeros).
1558*/ 1563*/
1559static void packint (luaL_Buffer *b, lua_Unsigned n, 1564static void packint (luaL_Buffer *b, lua_Unsigned n,
1560 int islittle, int size, int neg) { 1565 int islittle, unsigned size, int neg) {
1561 char *buff = luaL_prepbuffsize(b, size); 1566 char *buff = luaL_prepbuffsize(b, size);
1562 int i; 1567 unsigned i;
1563 buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */ 1568 buff[islittle ? 0 : size - 1] = (char)(n & MC); /* first byte */
1564 for (i = 1; i < size; i++) { 1569 for (i = 1; i < size; i++) {
1565 n >>= NB; 1570 n >>= NB;
@@ -1578,7 +1583,7 @@ static void packint (luaL_Buffer *b, lua_Unsigned n,
1578** given 'islittle' is different from native endianness. 1583** given 'islittle' is different from native endianness.
1579*/ 1584*/
1580static void copywithendian (char *dest, const char *src, 1585static void copywithendian (char *dest, const char *src,
1581 int size, int islittle) { 1586 unsigned size, int islittle) {
1582 if (islittle == nativeendian.little) 1587 if (islittle == nativeendian.little)
1583 memcpy(dest, src, size); 1588 memcpy(dest, src, size);
1584 else { 1589 else {
@@ -1599,7 +1604,7 @@ static int str_pack (lua_State *L) {
1599 lua_pushnil(L); /* mark to separate arguments from string buffer */ 1604 lua_pushnil(L); /* mark to separate arguments from string buffer */
1600 luaL_buffinit(L, &b); 1605 luaL_buffinit(L, &b);
1601 while (*fmt != '\0') { 1606 while (*fmt != '\0') {
1602 int ntoalign; 1607 unsigned ntoalign;
1603 size_t size; 1608 size_t size;
1604 KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); 1609 KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
1605 luaL_argcheck(L, size + ntoalign <= MAX_SIZE - totalsize, arg, 1610 luaL_argcheck(L, size + ntoalign <= MAX_SIZE - totalsize, arg,
@@ -1615,7 +1620,7 @@ static int str_pack (lua_State *L) {
1615 lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1); 1620 lua_Integer lim = (lua_Integer)1 << ((size * NB) - 1);
1616 luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow"); 1621 luaL_argcheck(L, -lim <= n && n < lim, arg, "integer overflow");
1617 } 1622 }
1618 packint(&b, (lua_Unsigned)n, h.islittle, size, (n < 0)); 1623 packint(&b, (lua_Unsigned)n, h.islittle, cast_uint(size), (n < 0));
1619 break; 1624 break;
1620 } 1625 }
1621 case Kuint: { /* unsigned integers */ 1626 case Kuint: { /* unsigned integers */
@@ -1623,7 +1628,7 @@ static int str_pack (lua_State *L) {
1623 if (size < SZINT) /* need overflow check? */ 1628 if (size < SZINT) /* need overflow check? */
1624 luaL_argcheck(L, (lua_Unsigned)n < ((lua_Unsigned)1 << (size * NB)), 1629 luaL_argcheck(L, (lua_Unsigned)n < ((lua_Unsigned)1 << (size * NB)),
1625 arg, "unsigned overflow"); 1630 arg, "unsigned overflow");
1626 packint(&b, (lua_Unsigned)n, h.islittle, size, 0); 1631 packint(&b, (lua_Unsigned)n, h.islittle, cast_uint(size), 0);
1627 break; 1632 break;
1628 } 1633 }
1629 case Kfloat: { /* C float */ 1634 case Kfloat: { /* C float */
@@ -1669,7 +1674,8 @@ static int str_pack (lua_State *L) {
1669 luaL_argcheck(L, size >= sizeof(lua_Unsigned) || 1674 luaL_argcheck(L, size >= sizeof(lua_Unsigned) ||
1670 len < ((lua_Unsigned)1 << (size * NB)), 1675 len < ((lua_Unsigned)1 << (size * NB)),
1671 arg, "string length does not fit in given size"); 1676 arg, "string length does not fit in given size");
1672 packint(&b, (lua_Unsigned)len, h.islittle, size, 0); /* pack length */ 1677 /* pack length */
1678 packint(&b, (lua_Unsigned)len, h.islittle, cast_uint(size), 0);
1673 luaL_addlstring(&b, s, len); 1679 luaL_addlstring(&b, s, len);
1674 totalsize += len; 1680 totalsize += len;
1675 break; 1681 break;
@@ -1697,20 +1703,20 @@ static int str_pack (lua_State *L) {
1697static int str_packsize (lua_State *L) { 1703static int str_packsize (lua_State *L) {
1698 Header h; 1704 Header h;
1699 const char *fmt = luaL_checkstring(L, 1); /* format string */ 1705 const char *fmt = luaL_checkstring(L, 1); /* format string */
1700 lua_Integer totalsize = 0; /* accumulate total size of result */ 1706 size_t totalsize = 0; /* accumulate total size of result */
1701 initheader(L, &h); 1707 initheader(L, &h);
1702 while (*fmt != '\0') { 1708 while (*fmt != '\0') {
1703 int ntoalign; 1709 unsigned ntoalign;
1704 size_t size; 1710 size_t size;
1705 KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign); 1711 KOption opt = getdetails(&h, totalsize, &fmt, &size, &ntoalign);
1706 luaL_argcheck(L, opt != Kstring && opt != Kzstr, 1, 1712 luaL_argcheck(L, opt != Kstring && opt != Kzstr, 1,
1707 "variable-length format"); 1713 "variable-length format");
1708 size += ntoalign; /* total space used by option */ 1714 size += ntoalign; /* total space used by option */
1709 luaL_argcheck(L, totalsize <= LUA_MAXINTEGER - cast(lua_Integer, size), 1715 luaL_argcheck(L, totalsize <= LUA_MAXINTEGER - size,
1710 1, "format result too large"); 1716 1, "format result too large");
1711 totalsize += size; 1717 totalsize += size;
1712 } 1718 }
1713 lua_pushinteger(L, totalsize); 1719 lua_pushinteger(L, cast_st2S(totalsize));
1714 return 1; 1720 return 1;
1715} 1721}
1716 1722
@@ -1759,7 +1765,7 @@ static int str_unpack (lua_State *L) {
1759 luaL_argcheck(L, pos <= ld, 3, "initial position out of string"); 1765 luaL_argcheck(L, pos <= ld, 3, "initial position out of string");
1760 initheader(L, &h); 1766 initheader(L, &h);
1761 while (*fmt != '\0') { 1767 while (*fmt != '\0') {
1762 int ntoalign; 1768 unsigned ntoalign;
1763 size_t size; 1769 size_t size;
1764 KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign); 1770 KOption opt = getdetails(&h, pos, &fmt, &size, &ntoalign);
1765 luaL_argcheck(L, ntoalign + size <= ld - pos, 2, 1771 luaL_argcheck(L, ntoalign + size <= ld - pos, 2,
@@ -1771,8 +1777,8 @@ static int str_unpack (lua_State *L) {
1771 switch (opt) { 1777 switch (opt) {
1772 case Kint: 1778 case Kint:
1773 case Kuint: { 1779 case Kuint: {
1774 lua_Integer res = unpackint(L, data + pos, h.islittle, size, 1780 lua_Integer res = unpackint(L, data + pos, h.islittle,
1775 (opt == Kint)); 1781 cast_int(size), (opt == Kint));
1776 lua_pushinteger(L, res); 1782 lua_pushinteger(L, res);
1777 break; 1783 break;
1778 } 1784 }
@@ -1800,7 +1806,7 @@ static int str_unpack (lua_State *L) {
1800 } 1806 }
1801 case Kstring: { 1807 case Kstring: {
1802 lua_Unsigned len = (lua_Unsigned)unpackint(L, data + pos, 1808 lua_Unsigned len = (lua_Unsigned)unpackint(L, data + pos,
1803 h.islittle, size, 0); 1809 h.islittle, cast_int(size), 0);
1804 luaL_argcheck(L, len <= ld - pos - size, 2, "data string too short"); 1810 luaL_argcheck(L, len <= ld - pos - size, 2, "data string too short");
1805 lua_pushlstring(L, data + pos + size, len); 1811 lua_pushlstring(L, data + pos + size, len);
1806 pos += len; /* skip string */ 1812 pos += len; /* skip string */
@@ -1820,7 +1826,7 @@ static int str_unpack (lua_State *L) {
1820 } 1826 }
1821 pos += size; 1827 pos += size;
1822 } 1828 }
1823 lua_pushinteger(L, pos + 1); /* next position */ 1829 lua_pushinteger(L, cast_st2S(pos) + 1); /* next position */
1824 return n + 1; 1830 return n + 1;
1825} 1831}
1826 1832