CID 22442 Unchecked return value from library

This commit is contained in:
Enno Rehling 2015-11-03 23:01:09 +01:00
parent 307c5ec85b
commit 870d42ccfe
1 changed files with 12 additions and 5 deletions

View File

@ -34,6 +34,7 @@ static int tolua_storage_create(lua_State * L)
const char *filename = tolua_tostring(L, 1, 0); const char *filename = tolua_tostring(L, 1, 0);
const char *type = tolua_tostring(L, 2, "rb"); const char *type = tolua_tostring(L, 2, "rb");
FILE * F; FILE * F;
int err = 0;
F = fopen(filename, type); F = fopen(filename, type);
if (F) { if (F) {
@ -42,7 +43,7 @@ static int tolua_storage_create(lua_State * L)
data->store = store; data->store = store;
if (strchr(type, 'r')) { if (strchr(type, 'r')) {
fread(&data->version, sizeof(int), 1, F); fread(&data->version, sizeof(int), 1, F);
fseek(F, sizeof(int), SEEK_CUR); err = fseek(F, sizeof(int), SEEK_CUR);
} }
else if (strchr(type, 'w')) { else if (strchr(type, 'w')) {
int n = STREAM_VERSION; int n = STREAM_VERSION;
@ -50,11 +51,17 @@ static int tolua_storage_create(lua_State * L)
fwrite(&data->version, sizeof(int), 1, F); fwrite(&data->version, sizeof(int), 1, F);
fwrite(&n, sizeof(int), 1, F); fwrite(&n, sizeof(int), 1, F);
} }
if (err) {
fclose(F);
free(data);
free(store);
} else {
fstream_init(&data->strm, F); fstream_init(&data->strm, F);
binstore_init(store, &data->strm); binstore_init(store, &data->strm);
tolua_pushusertype(L, (void *)data, TOLUA_CAST "storage"); tolua_pushusertype(L, (void *)data, TOLUA_CAST "storage");
return 1; return 1;
} }
}
return 0; return 0;
} }