lua-users home
lua-l archive

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


Hi All,

in lauxlib.c we have this function:

433 /*
434 ** returns a pointer to a free area with at least 'sz' bytes
435 */
436 LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) {
437   lua_State *L = B->L;
438   if (B->size - B->n < sz) {  /* not enough space? */
439     char *newbuff;
440     size_t newsize = B->size * 2;  /* double buffer size */
441     if (newsize - B->n < sz)  /* not bit enough? */
442       newsize = B->n + sz;
443     if (newsize < B->n || newsize - B->n < sz)
444       luaL_error(L, "buffer too large");
445     /* create larger buffer */
446     newbuff = (char *)lua_newuserdata(L, newsize * sizeof(char));
447     /* move content to new buffer */
448     memcpy(newbuff, B->b, B->n * sizeof(char));
449     if (buffonstack(B))
450       lua_remove(L, -2);  /* remove old buffer */
451     B->b = newbuff;
452     B->size = newsize;
453   }
454   return &B->b[B->n];
455 }
456 


In line 443 condition (newsize - B->n < sz) is always false for me because of 441-442. Isn't it? Also can (newsize < B->n) be true? For me B->n is always <= B->size and in 440 we assign size_t newsize = B->size * 2. Thanks in advance!

--
BR,
Michał Łowicki