lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sat, Oct 29, 2005 at 03:56:35PM -0300, Alex Queiroz wrote:

>     In function pRead of libhttpd.c:
> 
> ----
>     n = read(sockfd,buffer,sizeof(buffer)-1);
> 
> 
>     lua_pushnumber(L, n );
>     lua_pushstring(L, buffer );
> ----
> 
> Are you sure the buffer is always null-terminated?
> 

  I think I can be pretty sure that the buffer will have a 
 trailing null.  Looking at the code:

    /* Buffer we read() into */
    char buffer[4096];
    memset( buffer, '\0', sizeof(buffer));

    ..

    n = read(sockfd,buffer,sizeof(buffer)-1);

  1.  Create a buffer 4096 bytes long.
  2.  Set it full of '\0'.
  3.  Read no more than 4095 bytes into it:  (sizeof(buffer)-1);

  So there will *always* be at least one trailing NULL on the end
 of the buffer.

  (This from the CVS repository - some things changed, including the 
 size of the read buffer, but the memset/sizeof(-1) is the same in
 the code you quoted).

  A more interesting question is what to do if the data read contains
 NULLs.  So far that hasn't been a problem, but I'm not sure without
 checking whether Lua strings are NULL-clean..

Steve
--