Lua new suggestions

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

Lua new suggestions

Ranier Vilela-2
Hi,

There are more suggestions.

1. In function luaL_execresult (lauxlib.c), var  "what", can be reduced scope.
2. In function pusherror (loadlib.c), var "error", shadows function error.
3. In function l_str2d (lobject.c), is possible avoid call strlen,
    if we reverse the test and pdot is equal to NULL.
4. In function checkoption (loslib.c) the correct type for pointer operations is
   ptrdiff_t or size_t.

regards,
Ranier Vilela

reduce_scope_var_what_lauxlib.patch (878 bytes) Download Attachment
avoid_shadow_function_error_loadlib.patch (914 bytes) Download Attachment
avoid_strlen_lobject.patch (832 bytes) Download Attachment
use_correct_type_with_pointer_operations_loslib.patch (898 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Lua new suggestions

Roberto Ierusalimschy
> 4. In function checkoption (loslib.c) the correct type for pointer
> operations is
>    ptrdiff_t or size_t.

When you say "the correct", you imply the current one is incorrect.
Can you explain what is incorrect there?

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

Re: Lua new suggestions

Ranier Vilela-2
int type.
Varies enormously on various platforms, with 32 bits on almost all and 64 bits on some.
See at:

We have a string with more than 4GB, in pointers (64 bits) "const char * conv" and "char * buff" .
The convlen size is int (32 bits).

int memcmp ( const void * ptr1, const void * ptr2, size_t num )
void * memcpy ( void * destination, const void * source, size_t num );




Both expect size_t, as a length parameter (num).
oplen is declared as int type.

While int addresses all the need for options, correcting convlen,
oplen is compared to convlen and used as the index of a string (size_t),
the appropriate type of which is used is size_t or ptrdiff_t.

      return conv + oplen;  /* pointer arithmetic with int type */

#include <stdio.h>
int main()
{
   printf ("sizeof(size_t)=%llu\n", sizeof(size_t));
   printf ("sizeof(int)=%llu\n", sizeof(int));
   return 0;
}
sizeof(size_t)=8
sizeof(int)=4

Thus, the code follows good programming practices in C, and becomes 64-bit compatible.

regards,
Ranier Vilela