Remove uselles cast (lstrlib.c)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Remove uselles cast (lstrlib.c)

Ranier Vilela-2
Hi,

strlen already returns size_t.
We can securely remove this useless cast?

diff --git a/lstrlib.c b/lstrlib.c
index 2ba8bde4..6b77f332 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1738,7 +1738,7 @@ static int str_unpack (lua_State *L) {
         break;
       }
       case Kzstr: {
-        size_t len = (int)strlen(data + pos);
+        size_t len = strlen(data + pos);
         luaL_argcheck(L, pos + len < ld, 2,
                          "unfinished string for format 'z'");
         lua_pushlstring(L, data + pos, len);

regards,
Ranier Vilela
Reply | Threaded
Open this post in threaded view
|

Re: Remove uselles cast (lstrlib.c)

Roberto Ierusalimschy
> strlen already returns size_t.
> We can securely remove this useless cast?
>
> [...]
>        case Kzstr: {
> -        size_t len = (int)strlen(data + pos);
> +        size_t len = strlen(data + pos);
>          luaL_argcheck(L, pos + len < ld, 2,
>                           "unfinished string for format 'z'");

This is more than useless, it seems to be a bug. (C actually truncates
the result.) But we need a quite long string to hit it.

-- Roberto
Reply | Threaded
Open this post in threaded view
|

Re: Remove uselles cast (lstrlib.c)

Ranier Vilela-2
Em qua., 30 de set. de 2020 às 13:18, Roberto Ierusalimschy <[hidden email]> escreveu:
> strlen already returns size_t.
> We can securely remove this useless cast?
>
> [...]
>        case Kzstr: {
> -        size_t len = (int)strlen(data + pos);
> +        size_t len = strlen(data + pos);
>          luaL_argcheck(L, pos + len < ld, 2,
>                           "unfinished string for format 'z'");

This is more than useless, it seems to be a bug. (C actually truncates
the result.) But we need a quite long string to hit it.
True, need a string with more than 4GB.

regards,
Ranier Vilela