Sun Oct 11 01:01:46 2015 UTC ()
Get rid of the sprintf() bogus macro and use lengths explicitly when buffers
are involved.


(christos)
diff -r1.5 -r1.6 src/external/mit/lua/dist/src/lobject.c
diff -r1.9 -r1.10 src/external/mit/lua/dist/src/lstrlib.c
diff -r1.4 -r1.5 src/external/mit/lua/dist/src/luac.c
diff -r1.14 -r1.15 src/external/mit/lua/dist/src/luaconf.h

cvs diff -r1.5 -r1.6 src/external/mit/lua/dist/src/lobject.c (expand / switch to unified diff)

--- src/external/mit/lua/dist/src/lobject.c 2015/10/08 13:21:00 1.5
+++ src/external/mit/lua/dist/src/lobject.c 2015/10/11 01:01:45 1.6
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: lobject.c,v 1.5 2015/10/08 13:21:00 mbalmer Exp $ */ 1/* $NetBSD: lobject.c,v 1.6 2015/10/11 01:01:45 christos Exp $ */
2 2
3/* 3/*
4** Id: lobject.c,v 2.104 2015/04/11 18:30:08 roberto Exp  4** Id: lobject.c,v 2.104 2015/04/11 18:30:08 roberto Exp
5** Some generic functions over Lua objects 5** Some generic functions over Lua objects
6** See Copyright Notice in lua.h 6** See Copyright Notice in lua.h
7*/ 7*/
8 8
9#define lobject_c 9#define lobject_c
10#define LUA_CORE 10#define LUA_CORE
11 11
12#include "lprefix.h" 12#include "lprefix.h"
13 13
14 14
@@ -348,29 +348,29 @@ int luaO_utf8esc (char *buff, unsigned l @@ -348,29 +348,29 @@ int luaO_utf8esc (char *buff, unsigned l
348/* maximum length of the conversion of a number to a string */ 348/* maximum length of the conversion of a number to a string */
349#define MAXNUMBER2STR 50 349#define MAXNUMBER2STR 50
350 350
351 351
352/* 352/*
353** Convert a number object to a string 353** Convert a number object to a string
354*/ 354*/
355void luaO_tostring (lua_State *L, StkId obj) { 355void luaO_tostring (lua_State *L, StkId obj) {
356 char buff[MAXNUMBER2STR]; 356 char buff[MAXNUMBER2STR];
357 size_t len; 357 size_t len;
358 lua_assert(ttisnumber(obj)); 358 lua_assert(ttisnumber(obj));
359#ifndef _KERNEL 359#ifndef _KERNEL
360 if (ttisinteger(obj)) 360 if (ttisinteger(obj))
361 len = lua_integer2str(buff, ivalue(obj)); 361 len = lua_integer2str(buff, sizeof(buff), ivalue(obj));
362 else { 362 else {
363 len = lua_number2str(buff, fltvalue(obj)); 363 len = lua_number2str(buff, sizeof(buff), fltvalue(obj));
364#if !defined(LUA_COMPAT_FLOATSTRING) 364#if !defined(LUA_COMPAT_FLOATSTRING)
365 if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */ 365 if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
366 buff[len++] = lua_getlocaledecpoint(); 366 buff[len++] = lua_getlocaledecpoint();
367 buff[len++] = '0'; /* adds '.0' to result */ 367 buff[len++] = '0'; /* adds '.0' to result */
368 } 368 }
369#endif 369#endif
370 } 370 }
371#else /* _KERNEL */ 371#else /* _KERNEL */
372 lua_assert(ttisinteger(obj)); 372 lua_assert(ttisinteger(obj));
373 len = lua_integer2str(buff, ivalue(obj)); 373 len = lua_integer2str(buff, ivalue(obj));
374#endif 374#endif
375 setsvalue2s(L, obj, luaS_newlstr(L, buff, len)); 375 setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
376} 376}
@@ -414,27 +414,27 @@ const char *luaO_pushvfstring (lua_State @@ -414,27 +414,27 @@ const char *luaO_pushvfstring (lua_State
414 setivalue(L->top++, cast(lua_Integer, va_arg(argp, l_uacInt))); 414 setivalue(L->top++, cast(lua_Integer, va_arg(argp, l_uacInt)));
415 luaO_tostring(L, L->top - 1); 415 luaO_tostring(L, L->top - 1);
416 break; 416 break;
417 } 417 }
418#ifndef _KERNEL 418#ifndef _KERNEL
419 case 'f': { 419 case 'f': {
420 setfltvalue(L->top++, cast_num(va_arg(argp, l_uacNumber))); 420 setfltvalue(L->top++, cast_num(va_arg(argp, l_uacNumber)));
421 luaO_tostring(L, L->top - 1); 421 luaO_tostring(L, L->top - 1);
422 break; 422 break;
423 } 423 }
424#endif 424#endif
425 case 'p': { 425 case 'p': {
426 char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */ 426 char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */
427 int l = sprintf(buff, "%p", va_arg(argp, void *)); 427 int l = snprintf(buff, sizeof(buff), "%p", va_arg(argp, void *));
428 pushstr(L, buff, l); 428 pushstr(L, buff, l);
429 break; 429 break;
430 } 430 }
431 case 'U': { 431 case 'U': {
432 char buff[UTF8BUFFSZ]; 432 char buff[UTF8BUFFSZ];
433 int l = luaO_utf8esc(buff, cast(long, va_arg(argp, long))); 433 int l = luaO_utf8esc(buff, cast(long, va_arg(argp, long)));
434 pushstr(L, buff + UTF8BUFFSZ - l, l); 434 pushstr(L, buff + UTF8BUFFSZ - l, l);
435 break; 435 break;
436 } 436 }
437 case '%': { 437 case '%': {
438 pushstr(L, "%", 1); 438 pushstr(L, "%", 1);
439 break; 439 break;
440 } 440 }

cvs diff -r1.9 -r1.10 src/external/mit/lua/dist/src/lstrlib.c (expand / switch to unified diff)

--- src/external/mit/lua/dist/src/lstrlib.c 2015/10/08 13:40:16 1.9
+++ src/external/mit/lua/dist/src/lstrlib.c 2015/10/11 01:01:45 1.10
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: lstrlib.c,v 1.9 2015/10/08 13:40:16 mbalmer Exp $ */ 1/* $NetBSD: lstrlib.c,v 1.10 2015/10/11 01:01:45 christos Exp $ */
2 2
3/* 3/*
4** Id: lstrlib.c,v 1.229 2015/05/20 17:39:23 roberto Exp  4** Id: lstrlib.c,v 1.229 2015/05/20 17:39:23 roberto Exp
5** Standard library for string operations and pattern-matching 5** Standard library for string operations and pattern-matching
6** See Copyright Notice in lua.h 6** See Copyright Notice in lua.h
7*/ 7*/
8 8
9#define lstrlib_c 9#define lstrlib_c
10#define LUA_LIB 10#define LUA_LIB
11 11
12#include "lprefix.h" 12#include "lprefix.h"
13 13
14 14
@@ -824,52 +824,53 @@ static int str_gsub (lua_State *L) { @@ -824,52 +824,53 @@ static int str_gsub (lua_State *L) {
824 824
825 825
826/* 826/*
827** Add integer part of 'x' to buffer and return new 'x' 827** Add integer part of 'x' to buffer and return new 'x'
828*/ 828*/
829static lua_Number adddigit (char *buff, int n, lua_Number x) { 829static lua_Number adddigit (char *buff, int n, lua_Number x) {
830 lua_Number dd = l_mathop(floor)(x); /* get integer part from 'x' */ 830 lua_Number dd = l_mathop(floor)(x); /* get integer part from 'x' */
831 int d = (int)dd; 831 int d = (int)dd;
832 buff[n] = (d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */ 832 buff[n] = (d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */
833 return x - dd; /* return what is left */ 833 return x - dd; /* return what is left */
834} 834}
835 835
836 836
837static int num2straux (char *buff, lua_Number x) { 837static int num2straux (char *buff, size_t len, lua_Number x) {
838 if (x != x || x == HUGE_VAL || x == -HUGE_VAL) /* inf or NaN? */ 838 if (x != x || x == HUGE_VAL || x == -HUGE_VAL) /* inf or NaN? */
839 return sprintf(buff, LUA_NUMBER_FMT, x); /* equal to '%g' */ 839 return snprintf(buff, len, LUA_NUMBER_FMT, x); /* equal to '%g' */
840 else if (x == 0) { /* can be -0... */ 840 else if (x == 0) { /* can be -0... */
841 sprintf(buff, LUA_NUMBER_FMT, x); 841 snprintf(buff, len, LUA_NUMBER_FMT, x);
842 strcat(buff, "x0p+0"); /* reuses '0/-0' from 'sprintf'... */ 842 strlcat(buff, "x0p+0", len); /* reuses '0/-0' from 'snprintf'... */
843 return strlen(buff); 843 return strlen(buff);
844 } 844 }
845 else { 845 else {
846 int e; 846 int e;
847 lua_Number m = l_mathop(frexp)(x, &e); /* 'x' fraction and exponent */ 847 lua_Number m = l_mathop(frexp)(x, &e); /* 'x' fraction and exponent */
848 int n = 0; /* character count */ 848 int n = 0; /* character count */
849 if (m < 0) { /* is number negative? */ 849 if (m < 0) { /* is number negative? */
850 buff[n++] = '-'; /* add signal */ 850 buff[n++] = '-'; /* add signal */
851 m = -m; /* make it positive */ 851 m = -m; /* make it positive */
852 } 852 }
853 buff[n++] = '0'; buff[n++] = 'x'; /* add "0x" */ 853 buff[n++] = '0'; buff[n++] = 'x'; /* add "0x" */
854 m = adddigit(buff, n++, m * (1 << L_NBFD)); /* add first digit */ 854 m = adddigit(buff, n++, m * (1 << L_NBFD)); /* add first digit */
855 e -= L_NBFD; /* this digit goes before the radix point */ 855 e -= L_NBFD; /* this digit goes before the radix point */
856 if (m > 0) { /* more digits? */ 856 if (m > 0) { /* more digits? */
857 buff[n++] = lua_getlocaledecpoint(); /* add radix point */ 857 buff[n++] = lua_getlocaledecpoint(); /* add radix point */
858 do { /* add as many digits as needed */ 858 do { /* add as many digits as needed */
859 m = adddigit(buff, n++, m * 16); 859 m = adddigit(buff, n++, m * 16);
860 } while (m > 0); 860 } while (m > 0);
861 } 861 }
862 n += sprintf(buff + n, "p%+d", e); /* add exponent */ 862 if (len > (size_t)n)
 863 n += snprintf(buff + n, len - n, "p%+d", e); /* add exponent */
863 return n; 864 return n;
864 } 865 }
865} 866}
866 867
867 868
868static int lua_number2strx (lua_State *L, char *buff, const char *fmt, 869static int lua_number2strx (lua_State *L, char *buff, const char *fmt,
869 lua_Number x) { 870 lua_Number x) {
870 int n = num2straux(buff, x); 871 int n = num2straux(buff, x);
871 if (fmt[SIZELENMOD] == 'A') { 872 if (fmt[SIZELENMOD] == 'A') {
872 int i; 873 int i;
873 for (i = 0; i < n; i++) 874 for (i = 0; i < n; i++)
874 buff[i] = toupper(uchar(buff[i])); 875 buff[i] = toupper(uchar(buff[i]));
875 } 876 }
@@ -903,29 +904,29 @@ static int lua_number2strx (lua_State *L @@ -903,29 +904,29 @@ static int lua_number2strx (lua_State *L
903 904
904static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { 905static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
905 size_t l; 906 size_t l;
906 const char *s = luaL_checklstring(L, arg, &l); 907 const char *s = luaL_checklstring(L, arg, &l);
907 luaL_addchar(b, '"'); 908 luaL_addchar(b, '"');
908 while (l--) { 909 while (l--) {
909 if (*s == '"' || *s == '\\' || *s == '\n') { 910 if (*s == '"' || *s == '\\' || *s == '\n') {
910 luaL_addchar(b, '\\'); 911 luaL_addchar(b, '\\');
911 luaL_addchar(b, *s); 912 luaL_addchar(b, *s);
912 } 913 }
913 else if (*s == '\0' || iscntrl(uchar(*s))) { 914 else if (*s == '\0' || iscntrl(uchar(*s))) {
914 char buff[10]; 915 char buff[10];
915 if (!isdigit(uchar(*(s+1)))) 916 if (!isdigit(uchar(*(s+1))))
916 sprintf(buff, "\\%d", (int)uchar(*s)); 917 snprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s));
917 else 918 else
918 sprintf(buff, "\\%03d", (int)uchar(*s)); 919 snprintf(buff, sizeof(buff), "\\%03d", (int)uchar(*s));
919 luaL_addstring(b, buff); 920 luaL_addstring(b, buff);
920 } 921 }
921 else 922 else
922 luaL_addchar(b, *s); 923 luaL_addchar(b, *s);
923 s++; 924 s++;
924 } 925 }
925 luaL_addchar(b, '"'); 926 luaL_addchar(b, '"');
926} 927}
927 928
928static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { 929static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
929 const char *p = strfrmt; 930 const char *p = strfrmt;
930 while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */ 931 while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
931 if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char)) 932 if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char))
@@ -972,62 +973,62 @@ static int str_format (lua_State *L) { @@ -972,62 +973,62 @@ static int str_format (lua_State *L) {
972 if (*strfrmt != L_ESC) 973 if (*strfrmt != L_ESC)
973 luaL_addchar(&b, *strfrmt++); 974 luaL_addchar(&b, *strfrmt++);
974 else if (*++strfrmt == L_ESC) 975 else if (*++strfrmt == L_ESC)
975 luaL_addchar(&b, *strfrmt++); /* %% */ 976 luaL_addchar(&b, *strfrmt++); /* %% */
976 else { /* format item */ 977 else { /* format item */
977 char form[MAX_FORMAT]; /* to store the format ('%...') */ 978 char form[MAX_FORMAT]; /* to store the format ('%...') */
978 char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */ 979 char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */
979 int nb = 0; /* number of bytes in added item */ 980 int nb = 0; /* number of bytes in added item */
980 if (++arg > top) 981 if (++arg > top)
981 luaL_argerror(L, arg, "no value"); 982 luaL_argerror(L, arg, "no value");
982 strfrmt = scanformat(L, strfrmt, form); 983 strfrmt = scanformat(L, strfrmt, form);
983 switch (*strfrmt++) { 984 switch (*strfrmt++) {
984 case 'c': { 985 case 'c': {
985 nb = sprintf(buff, form, (int)luaL_checkinteger(L, arg)); 986 nb = snprintf(buff, MAX_ITEM, form, (int)luaL_checkinteger(L, arg));
986 break; 987 break;
987 } 988 }
988 case 'd': case 'i': 989 case 'd': case 'i':
989 case 'o': case 'u': case 'x': case 'X': { 990 case 'o': case 'u': case 'x': case 'X': {
990 lua_Integer n = luaL_checkinteger(L, arg); 991 lua_Integer n = luaL_checkinteger(L, arg);
991 addlenmod(form, LUA_INTEGER_FRMLEN); 992 addlenmod(form, LUA_INTEGER_FRMLEN);
992 nb = sprintf(buff, form, n); 993 nb = snprintf(buff, MAX_ITEM, form, n);
993 break; 994 break;
994 } 995 }
995#ifndef _KERNEL 996#ifndef _KERNEL
996 case 'a': case 'A': 997 case 'a': case 'A':
997 addlenmod(form, LUA_NUMBER_FRMLEN); 998 addlenmod(form, LUA_NUMBER_FRMLEN);
998 nb = lua_number2strx(L, buff, form, luaL_checknumber(L, arg)); 999 nb = lua_number2strx(L, buff, MAX_ITEM, form, luaL_checknumber(L, arg));
999 break; 1000 break;
1000 case 'e': case 'E': case 'f': 1001 case 'e': case 'E': case 'f':
1001 case 'g': case 'G': { 1002 case 'g': case 'G': {
1002 addlenmod(form, LUA_NUMBER_FRMLEN); 1003 addlenmod(form, LUA_NUMBER_FRMLEN);
1003 nb = sprintf(buff, form, luaL_checknumber(L, arg)); 1004 nb = snprintf(buff, MAX_ITEM, form, luaL_checknumber(L, arg));
1004 break; 1005 break;
1005 } 1006 }
1006#endif 1007#endif
1007 case 'q': { 1008 case 'q': {
1008 addquoted(L, &b, arg); 1009 addquoted(L, &b, arg);
1009 break; 1010 break;
1010 } 1011 }
1011 case 's': { 1012 case 's': {
1012 size_t l; 1013 size_t l;
1013 const char *s = luaL_tolstring(L, arg, &l); 1014 const char *s = luaL_tolstring(L, arg, &l);
1014 if (!strchr(form, '.') && l >= 100) { 1015 if (!strchr(form, '.') && l >= 100) {
1015 /* no precision and string is too long to be formatted; 1016 /* no precision and string is too long to be formatted;
1016 keep original string */ 1017 keep original string */
1017 luaL_addvalue(&b); 1018 luaL_addvalue(&b);
1018 } 1019 }
1019 else { 1020 else {
1020 nb = sprintf(buff, form, s); 1021 nb = snprintf(buff, MAX_ITEM, form, s);
1021 lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ 1022 lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
1022 } 1023 }
1023 break; 1024 break;
1024 } 1025 }
1025 default: { /* also treat cases 'pnLlh' */ 1026 default: { /* also treat cases 'pnLlh' */
1026 return luaL_error(L, "invalid option '%%%c' to 'format'", 1027 return luaL_error(L, "invalid option '%%%c' to 'format'",
1027 *(strfrmt - 1)); 1028 *(strfrmt - 1));
1028 } 1029 }
1029 } 1030 }
1030 luaL_addsize(&b, nb); 1031 luaL_addsize(&b, nb);
1031 } 1032 }
1032 } 1033 }
1033 luaL_pushresult(&b); 1034 luaL_pushresult(&b);

cvs diff -r1.4 -r1.5 src/external/mit/lua/dist/src/luac.c (expand / switch to unified diff)

--- src/external/mit/lua/dist/src/luac.c 2015/10/08 13:21:00 1.4
+++ src/external/mit/lua/dist/src/luac.c 2015/10/11 01:01:45 1.5
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: luac.c,v 1.4 2015/10/08 13:21:00 mbalmer Exp $ */ 1/* $NetBSD: luac.c,v 1.5 2015/10/11 01:01:45 christos Exp $ */
2 2
3/* 3/*
4** Id: luac.c,v 1.75 2015/03/12 01:58:27 lhf Exp  4** Id: luac.c,v 1.75 2015/03/12 01:58:27 lhf Exp
5** Lua compiler (saves bytecodes to files; also lists bytecodes) 5** Lua compiler (saves bytecodes to files; also lists bytecodes)
6** See Copyright Notice in lua.h 6** See Copyright Notice in lua.h
7*/ 7*/
8 8
9#define luac_c 9#define luac_c
10#define LUA_CORE 10#define LUA_CORE
11 11
12#include "lprefix.h" 12#include "lprefix.h"
13 13
14#include <ctype.h> 14#include <ctype.h>
@@ -257,27 +257,27 @@ static void PrintConstant(const Proto* f @@ -257,27 +257,27 @@ static void PrintConstant(const Proto* f
257{ 257{
258 const TValue* o=&f->k[i]; 258 const TValue* o=&f->k[i];
259 switch (ttype(o)) 259 switch (ttype(o))
260 { 260 {
261 case LUA_TNIL: 261 case LUA_TNIL:
262 printf("nil"); 262 printf("nil");
263 break; 263 break;
264 case LUA_TBOOLEAN: 264 case LUA_TBOOLEAN:
265 printf(bvalue(o) ? "true" : "false"); 265 printf(bvalue(o) ? "true" : "false");
266 break; 266 break;
267 case LUA_TNUMFLT: 267 case LUA_TNUMFLT:
268 { 268 {
269 char buff[100]; 269 char buff[100];
270 sprintf(buff,LUA_NUMBER_FMT,fltvalue(o)); 270 snprintf(buff, sizeof(buff), LUA_NUMBER_FMT,fltvalue(o));
271 printf("%s",buff); 271 printf("%s",buff);
272 if (buff[strspn(buff,"-0123456789")]=='\0') printf(".0"); 272 if (buff[strspn(buff,"-0123456789")]=='\0') printf(".0");
273 break; 273 break;
274 } 274 }
275 case LUA_TNUMINT: 275 case LUA_TNUMINT:
276 printf(LUA_INTEGER_FMT,ivalue(o)); 276 printf(LUA_INTEGER_FMT,ivalue(o));
277 break; 277 break;
278 case LUA_TSHRSTR: case LUA_TLNGSTR: 278 case LUA_TSHRSTR: case LUA_TLNGSTR:
279 PrintString(tsvalue(o)); 279 PrintString(tsvalue(o));
280 break; 280 break;
281 default: /* cannot happen */ 281 default: /* cannot happen */
282 printf("? type=%d",ttype(o)); 282 printf("? type=%d",ttype(o));
283 break; 283 break;

cvs diff -r1.14 -r1.15 src/external/mit/lua/dist/src/luaconf.h (expand / switch to unified diff)

--- src/external/mit/lua/dist/src/luaconf.h 2015/10/08 13:21:00 1.14
+++ src/external/mit/lua/dist/src/luaconf.h 2015/10/11 01:01:45 1.15
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: luaconf.h,v 1.14 2015/10/08 13:21:00 mbalmer Exp $ */ 1/* $NetBSD: luaconf.h,v 1.15 2015/10/11 01:01:45 christos Exp $ */
2 2
3/* 3/*
4** Id: luaconf.h,v 1.251 2015/05/20 17:39:23 roberto Exp  4** Id: luaconf.h,v 1.251 2015/05/20 17:39:23 roberto Exp
5** Configuration file for Lua 5** Configuration file for Lua
6** See Copyright Notice in lua.h 6** See Copyright Notice in lua.h
7*/ 7*/
8 8
9 9
10#ifndef luaconf_h 10#ifndef luaconf_h
11#define luaconf_h 11#define luaconf_h
12 12
13#ifndef _KERNEL 13#ifndef _KERNEL
14#include <limits.h> 14#include <limits.h>
@@ -468,27 +468,27 @@ @@ -468,27 +468,27 @@
468#define l_mathop(op) op 468#define l_mathop(op) op
469 469
470#define lua_str2number(s,p) strtod((s), (p)) 470#define lua_str2number(s,p) strtod((s), (p))
471 471
472#else /* }{ */ 472#else /* }{ */
473 473
474#error "numeric float type not defined" 474#error "numeric float type not defined"
475 475
476#endif /* } */ 476#endif /* } */
477 477
478 478
479#define l_floor(x) (l_mathop(floor)(x)) 479#define l_floor(x) (l_mathop(floor)(x))
480 480
481#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) 481#define lua_number2str(s,l, n) snprintf((s), (l), LUA_NUMBER_FMT, (n))
482 482
483 483
484/* 484/*
485@@ lua_numbertointeger converts a float number to an integer, or 485@@ lua_numbertointeger converts a float number to an integer, or
486** returns 0 if float is not within the range of a lua_Integer. 486** returns 0 if float is not within the range of a lua_Integer.
487** (The range comparisons are tricky because of rounding. The tests 487** (The range comparisons are tricky because of rounding. The tests
488** here assume a two-complement representation, where MININTEGER always 488** here assume a two-complement representation, where MININTEGER always
489** has an exact representation as a float; MAXINTEGER may not have one, 489** has an exact representation as a float; MAXINTEGER may not have one,
490** and therefore its conversion to float may have an ill-defined value.) 490** and therefore its conversion to float may have an ill-defined value.)
491*/ 491*/
492#define lua_numbertointeger(n,p) \ 492#define lua_numbertointeger(n,p) \
493 ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \ 493 ((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \
494 (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \ 494 (n) < -(LUA_NUMBER)(LUA_MININTEGER) && \
@@ -505,27 +505,27 @@ @@ -505,27 +505,27 @@
505@@ LUAI_UACINT is the result of an 'usual argument conversion' 505@@ LUAI_UACINT is the result of an 'usual argument conversion'
506@@ over a lUA_INTEGER. 506@@ over a lUA_INTEGER.
507@@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers. 507@@ LUA_INTEGER_FRMLEN is the length modifier for reading/writing integers.
508@@ LUA_INTEGER_FMT is the format for writing integers. 508@@ LUA_INTEGER_FMT is the format for writing integers.
509@@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER. 509@@ LUA_MAXINTEGER is the maximum value for a LUA_INTEGER.
510@@ LUA_MININTEGER is the minimum value for a LUA_INTEGER. 510@@ LUA_MININTEGER is the minimum value for a LUA_INTEGER.
511@@ lua_integer2str converts an integer to a string. 511@@ lua_integer2str converts an integer to a string.
512*/ 512*/
513 513
514 514
515/* The following definitions are good for most cases here */ 515/* The following definitions are good for most cases here */
516 516
517#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d" 517#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d"
518#define lua_integer2str(s,n) sprintf((s), LUA_INTEGER_FMT, (n)) 518#define lua_integer2str(s,l,n) snprintf((s), (l), LUA_INTEGER_FMT, (n))
519 519
520#define LUAI_UACINT LUA_INTEGER 520#define LUAI_UACINT LUA_INTEGER
521 521
522/* 522/*
523** use LUAI_UACINT here to avoid problems with promotions (which 523** use LUAI_UACINT here to avoid problems with promotions (which
524** can turn a comparison between unsigneds into a signed comparison) 524** can turn a comparison between unsigneds into a signed comparison)
525*/ 525*/
526#define LUA_UNSIGNED unsigned LUAI_UACINT 526#define LUA_UNSIGNED unsigned LUAI_UACINT
527 527
528 528
529/* now the variable definitions */ 529/* now the variable definitions */
530 530
531#if LUA_INT_TYPE == LUA_INT_INT /* { int */ 531#if LUA_INT_TYPE == LUA_INT_INT /* { int */
@@ -589,32 +589,32 @@ @@ -589,32 +589,32 @@
589/* 589/*
590@@ lua_strx2number converts an hexadecimal numeric string to a number. 590@@ lua_strx2number converts an hexadecimal numeric string to a number.
591** In C99, 'strtod' does that conversion. Otherwise, you can 591** In C99, 'strtod' does that conversion. Otherwise, you can
592** leave 'lua_strx2number' undefined and Lua will provide its own 592** leave 'lua_strx2number' undefined and Lua will provide its own
593** implementation. 593** implementation.
594*/ 594*/
595#if !defined(LUA_USE_C89) 595#if !defined(LUA_USE_C89)
596#define lua_strx2number(s,p) lua_str2number(s,p) 596#define lua_strx2number(s,p) lua_str2number(s,p)
597#endif 597#endif
598 598
599 599
600/* 600/*
601@@ lua_number2strx converts a float to an hexadecimal numeric string.  601@@ lua_number2strx converts a float to an hexadecimal numeric string.
602** In C99, 'sprintf' (with format specifiers '%a'/'%A') does that. 602** In C99, 'snprintf' (with format specifiers '%a'/'%A') does that.
603** Otherwise, you can leave 'lua_number2strx' undefined and Lua will 603** Otherwise, you can leave 'lua_number2strx' undefined and Lua will
604** provide its own implementation. 604** provide its own implementation.
605*/ 605*/
606#if !defined(LUA_USE_C89) 606#if !defined(LUA_USE_C89)
607#define lua_number2strx(L,b,f,n) sprintf(b,f,n) 607#define lua_number2strx(L,b,l,f,n) snprintf(b,l,f,n)
608#endif 608#endif
609 609
610 610
611/* 611/*
612** 'strtof' and 'opf' variants for math functions are not valid in 612** 'strtof' and 'opf' variants for math functions are not valid in
613** C89. Otherwise, the macro 'HUGE_VALF' is a good proxy for testing the 613** C89. Otherwise, the macro 'HUGE_VALF' is a good proxy for testing the
614** availability of these variants. ('math.h' is already included in 614** availability of these variants. ('math.h' is already included in
615** all files that use these macros.) 615** all files that use these macros.)
616*/ 616*/
617#if defined(LUA_USE_C89) || (defined(HUGE_VAL) && !defined(HUGE_VALF)) 617#if defined(LUA_USE_C89) || (defined(HUGE_VAL) && !defined(HUGE_VALF))
618#undef l_mathop /* variants not available */ 618#undef l_mathop /* variants not available */
619#undef lua_str2number 619#undef lua_str2number
620#define l_mathop(op) (lua_Number)op /* no variant */ 620#define l_mathop(op) (lua_Number)op /* no variant */
@@ -792,27 +792,25 @@ @@ -792,27 +792,25 @@
792/* setjmp.h */ 792/* setjmp.h */
793#define LUAI_THROW(L,c) longjmp(&((c)->b)) 793#define LUAI_THROW(L,c) longjmp(&((c)->b))
794#define LUAI_TRY(L,c,a) if (setjmp(&((c)->b)) == 0) { a } 794#define LUAI_TRY(L,c,a) if (setjmp(&((c)->b)) == 0) { a }
795#define luai_jmpbuf label_t 795#define luai_jmpbuf label_t
796 796
797/* time.h */ 797/* time.h */
798#include <sys/time.h> 798#include <sys/time.h>
799#define time(p) (time_uptime) 799#define time(p) (time_uptime)
800 800
801/* stdio.h */ 801/* stdio.h */
802#define lua_writestring(s,l) printf("%s", (s)) 802#define lua_writestring(s,l) printf("%s", (s))
803#define lua_writeline() printf("\n") 803#define lua_writeline() printf("\n")
804 804
805#define sprintf(s,fmt,...) snprintf(s, sizeof(s), fmt, __VA_ARGS__) 
806 
807/* string.h */ 805/* string.h */
808#define strcoll strcmp 806#define strcoll strcmp
809 807
810/* stdlib.h */ 808/* stdlib.h */
811#define abort() panic("Lua has aborted!") 809#define abort() panic("Lua has aborted!")
812 810
813#endif /* _KERNEL */ 811#endif /* _KERNEL */
814 812
815#endif /* __NetBSD__ */ 813#endif /* __NetBSD__ */
816 814
817#endif 815#endif
818 816