fix memleak on realloc and uninit variable

This commit is contained in:
iceman1001 2020-05-27 20:21:42 +02:00
commit dded3953ef

View file

@ -955,7 +955,7 @@ static int l_T55xx_readblock(lua_State *L) {
if (n != 4) if (n != 4)
return returnToLuaWithError(L, "Wrong number of arguments, got %d bytes, expected 4", n); return returnToLuaWithError(L, "Wrong number of arguments, got %d bytes, expected 4", n);
uint32_t block, usepage1, override, password; uint32_t block, usepage1, override, password = 0;
bool usepwd; bool usepwd;
size_t size; size_t size;
@ -1179,15 +1179,17 @@ static int l_cwd(lua_State *L) {
uint16_t path_len = FILENAME_MAX; // should be a good starting point uint16_t path_len = FILENAME_MAX; // should be a good starting point
bool error = false; bool error = false;
char *cwd = NULL; char *cwd = (char *)calloc(path_len, sizeof(uint8_t));
cwd = (char *)calloc(path_len, sizeof(uint8_t));
while (!error && (GetCurrentDir(cwd, path_len) == NULL)) { while (!error && (GetCurrentDir(cwd, path_len) == NULL)) {
if (errno == ERANGE) { // Need bigger buffer if (errno == ERANGE) { // Need bigger buffer
path_len += 10; // if buffer was too small add 10 characters and try again path_len += 10; // if buffer was too small add 10 characters and try again
cwd = realloc(cwd, path_len); cwd = realloc(cwd, path_len);
if (cwd == NULL) {
free(cwd);
return returnToLuaWithError(L, "Failed to allocate memory");
}
} else { } else {
error = true;
free(cwd); free(cwd);
return returnToLuaWithError(L, "Failed to get current working directory"); return returnToLuaWithError(L, "Failed to get current working directory");
} }