Skip to content

Commit

Permalink
add missing fclose() in loadfile()
Browse files Browse the repository at this point in the history
  • Loading branch information
starwing committed Jan 27, 2018
1 parent 5b1a648 commit 103d68e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
35 changes: 24 additions & 11 deletions pb.c
Expand Up @@ -319,6 +319,20 @@ static void lpb_readtype(lua_State *L, int type, pb_SliceExt *s) {
# define setmode(a,b) ((void)0)
#endif

static int io_read(lua_State *L) {
FILE *fp = (FILE*)lua_touserdata(L, 1);
size_t nr;
luaL_Buffer b;
luaL_buffinit(L, &b);
do { /* read file in chunks of LUAL_BUFFERSIZE bytes */
char *p = luaL_prepbuffer(&b);
nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, fp);
luaL_addsize(&b, nr);
} while (nr == LUAL_BUFFERSIZE);
luaL_pushresult(&b); /* close buffer */
return 1;
}

static int io_write(lua_State *L, FILE *f, int idx) {
int nargs = lua_gettop(L) - idx + 1;
int status = 1;
Expand All @@ -332,22 +346,18 @@ static int io_write(lua_State *L, FILE *f, int idx) {

static int Lio_read(lua_State *L) {
const char *fname = luaL_optstring(L, 1, NULL);
luaL_Buffer b;
FILE *fp = stdin;
size_t nr;
int ret;
if (fname == NULL)
(void)setmode(fileno(stdin), O_BINARY);
else if ((fp = fopen(fname, "rb")) == NULL)
return luaL_fileresult(L, 0, fname);
luaL_buffinit(L, &b);
do { /* read file in chunks of LUAL_BUFFERSIZE bytes */
char *p = luaL_prepbuffer(&b);
nr = fread(p, sizeof(char), LUAL_BUFFERSIZE, fp);
luaL_addsize(&b, nr);
} while (nr == LUAL_BUFFERSIZE);
lua_pushcfunction(L, io_read);
lua_pushlightuserdata(L, fp);
ret = lua_pcall(L, 1, 1, 0);
if (fp != stdin) fclose(fp);
else (void)setmode(fileno(stdin), O_TEXT);
luaL_pushresult(&b); /* close buffer */
if (ret != LUA_OK) { lua_pushnil(L); lua_insert(L, -2); return 2; }
return 1;
}

Expand Down Expand Up @@ -998,9 +1008,12 @@ static int Lpb_loadfile(lua_State *L) {
return luaL_fileresult(L, 0, filename);
pb_initbuffer(&b);
do {
size = fread(pb_prepbuffsize(&b, BUFSIZ), 1, BUFSIZ, fp);
void *d = pb_prepbuffsize(&b, BUFSIZ);
if (d == NULL) { fclose(fp); return luaL_error(L, "out of memory"); }
size = fread(d, 1, BUFSIZ, fp);
pb_addsize(&b, size);
} while (size == BUFSIZ);
fclose(fp);
s = lpb_initext(pb_result(&b));
ret = pb_load(S, &s.base);
pb_resetbuffer(&b);
Expand Down Expand Up @@ -1417,5 +1430,5 @@ LUALIB_API int luaopen_pb(lua_State *L) {

/* cc: flags+='-O3 -ggdb -pedantic -std=c90 -Wall -Wextra --coverage'
* maccc: flags+='-shared -undefined dynamic_lookup' output='pb.so'
* win32cc: flags+='-mdll -DLUA_BUILD_AS_DLL ' output='pb.dll' libs+='-llua53' */
* win32cc: flags+='-s -mdll -DLUA_BUILD_AS_DLL ' output='pb.dll' libs+='-llua53' */

35 changes: 14 additions & 21 deletions pb.h
Expand Up @@ -954,11 +954,10 @@ static size_t pbN_resize(pb_State *S, size_t size) {
static pb_NameEntry *pbN_newname(pb_State *S, const char *name, size_t len, unsigned hash) {
pb_NameTable *nt = &S->nametable;
pb_NameEntry **list, *newobj;
if (nt->count >= nt->size && pbN_resize(S, nt->size * 2) == 0)
return NULL;
if (nt->count >= nt->size && !pbN_resize(S, nt->size * 2)) return NULL;
list = &nt->hash[hash & (nt->size - 1)];
newobj = (pb_NameEntry*)malloc(sizeof(pb_NameEntry) + len + 1);
if (newobj == NULL) return NULL;
if (newobj == NULL) return NULL;
newobj->next = *list;
newobj->length = (unsigned)len;
newobj->refcount = 1;
Expand Down Expand Up @@ -1064,30 +1063,24 @@ PB_API void pb_free(pb_State *S) {
}

PB_API pb_Type *pb_type(pb_State *S, pb_Name *tname) {
if (S != NULL && tname != NULL) {
pb_TypeEntry *te = (pb_TypeEntry*)pb_gettable(
&S->types, (pb_Key)tname);
if (te) return te->value;
}
return NULL;
pb_TypeEntry *te = NULL;
if (S != NULL && tname != NULL)
te = (pb_TypeEntry*)pb_gettable(&S->types, (pb_Key)tname);
return te ? te->value : NULL;
}

PB_API pb_Field *pb_fname(pb_Type *t, pb_Name *name) {
if (t != NULL && name != NULL) {
pb_FieldEntry *fe = (pb_FieldEntry*)pb_gettable(
&t->field_names, (pb_Key)name);
if (fe) return fe->value;
}
return NULL;
pb_FieldEntry *fe = NULL;
if (t != NULL && name != NULL)
fe = (pb_FieldEntry*)pb_gettable(&t->field_names, (pb_Key)name);
return fe ? fe->value : NULL;
}

PB_API pb_Field *pb_field(pb_Type *t, int32_t number) {
if (t != NULL) {
pb_FieldEntry *fe = (pb_FieldEntry*)pb_gettable(
&t->field_tags, number);
if (fe) return fe->value;
}
return NULL;
pb_FieldEntry *fe = NULL;
if (t != NULL && number != 0)
fe = (pb_FieldEntry*)pb_gettable(&t->field_tags, number);
return fe ? fe->value : NULL;
}

PB_API int pb_nexttype(pb_State *S, pb_Type **ptype) {
Expand Down

0 comments on commit 103d68e

Please sign in to comment.