mirror of https://github.com/rusefi/lua.git
Fixed bug in 'string.format("%p")'
The string "(null)" used for non-collectable values must be printed as a
string, not as a pointer. (Bug introduced in commit e0cbaa50fa
).
This commit is contained in:
parent
e460752323
commit
513559cc47
|
@ -1271,8 +1271,10 @@ static int str_format (lua_State *L) {
|
||||||
}
|
}
|
||||||
case 'p': {
|
case 'p': {
|
||||||
const void *p = lua_topointer(L, arg);
|
const void *p = lua_topointer(L, arg);
|
||||||
if (p == NULL)
|
if (p == NULL) { /* avoid calling 'printf' with argument NULL */
|
||||||
p = "(null)"; /* NULL not a valid parameter in ISO C 'printf' */
|
p = "(null)"; /* result */
|
||||||
|
form[strlen(form) - 1] = 's'; /* format it as a string */
|
||||||
|
}
|
||||||
nb = l_sprintf(buff, maxitem, form, p);
|
nb = l_sprintf(buff, maxitem, form, p);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
3
ltests.c
3
ltests.c
|
@ -131,8 +131,7 @@ static void warnf (void *ud, const char *msg, int tocont) {
|
||||||
if (buff[0] != '#' && onoff) /* unexpected warning? */
|
if (buff[0] != '#' && onoff) /* unexpected warning? */
|
||||||
badexit("Unexpected warning in test mode: %s\naborting...\n",
|
badexit("Unexpected warning in test mode: %s\naborting...\n",
|
||||||
buff, NULL);
|
buff, NULL);
|
||||||
/* else */ /* FALLTHROUGH */
|
} /* FALLTHROUGH */
|
||||||
}
|
|
||||||
case 1: { /* allow */
|
case 1: { /* allow */
|
||||||
if (onoff)
|
if (onoff)
|
||||||
fprintf(stderr, "Lua warning: %s\n", buff); /* print warning */
|
fprintf(stderr, "Lua warning: %s\n", buff); /* print warning */
|
||||||
|
|
|
@ -158,28 +158,38 @@ do -- tests for '%p' format
|
||||||
-- not much to test, as C does not specify what '%p' does.
|
-- not much to test, as C does not specify what '%p' does.
|
||||||
-- ("The value of the pointer is converted to a sequence of printing
|
-- ("The value of the pointer is converted to a sequence of printing
|
||||||
-- characters, in an implementation-defined manner.")
|
-- characters, in an implementation-defined manner.")
|
||||||
local null = string.format("%p", nil)
|
local null = "(null)" -- nulls are formatted by Lua
|
||||||
assert(string.format("%p", {}) ~= null)
|
|
||||||
assert(string.format("%p", 4) == null)
|
assert(string.format("%p", 4) == null)
|
||||||
assert(string.format("%p", true) == null)
|
assert(string.format("%p", true) == null)
|
||||||
|
assert(string.format("%p", nil) == null)
|
||||||
|
assert(string.format("%p", {}) ~= null)
|
||||||
assert(string.format("%p", print) ~= null)
|
assert(string.format("%p", print) ~= null)
|
||||||
assert(string.format("%p", coroutine.running()) ~= null)
|
assert(string.format("%p", coroutine.running()) ~= null)
|
||||||
assert(string.format("%p", io.stdin) ~= null)
|
assert(string.format("%p", io.stdin) ~= null)
|
||||||
assert(string.format("%p", io.stdin) == string.format("%p", io.stdin))
|
assert(string.format("%p", io.stdin) == string.format("%p", io.stdin))
|
||||||
|
assert(string.format("%p", print) == string.format("%p", print))
|
||||||
|
assert(string.format("%p", print) ~= string.format("%p", assert))
|
||||||
|
|
||||||
|
assert(#string.format("%90p", {}) == 90)
|
||||||
|
assert(#string.format("%-60p", {}) == 60)
|
||||||
|
assert(string.format("%10p", false) == string.rep(" ", 10 - #null) .. null)
|
||||||
|
assert(string.format("%-12p", 1.5) == null .. string.rep(" ", 12 - #null))
|
||||||
|
|
||||||
do
|
do
|
||||||
local t1 = {}; local t2 = {}
|
local t1 = {}; local t2 = {}
|
||||||
assert(string.format("%p", t1) ~= string.format("%p", t2))
|
assert(string.format("%p", t1) ~= string.format("%p", t2))
|
||||||
end
|
end
|
||||||
|
|
||||||
do -- short strings are internalized
|
do -- short strings are internalized
|
||||||
local s1 = string.rep("a", 10)
|
local s1 = string.rep("a", 10)
|
||||||
local s2 = string.rep("a", 10)
|
local s2 = string.rep("aa", 5)
|
||||||
assert(string.format("%p", s1) == string.format("%p", s2))
|
assert(string.format("%p", s1) == string.format("%p", s2))
|
||||||
end
|
end
|
||||||
|
|
||||||
do -- long strings aren't internalized
|
do -- long strings aren't internalized
|
||||||
local s1 = string.rep("a", 300); local s2 = string.rep("a", 300)
|
local s1 = string.rep("a", 300); local s2 = string.rep("a", 300)
|
||||||
assert(string.format("%p", s1) ~= string.format("%p", s2))
|
assert(string.format("%p", s1) ~= string.format("%p", s2))
|
||||||
end
|
end
|
||||||
assert(#string.format("%90p", {}) == 90)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
x = '"ílo"\n\\'
|
x = '"ílo"\n\\'
|
||||||
|
|
Loading…
Reference in New Issue