Skip to content

Commit

Permalink
lminiz: return nil on inflate/deflate failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilal2453 committed Aug 31, 2024
1 parent eb1d7c9 commit 5882474
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/lminiz.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include "./luvi.h"
#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES
#include "../deps/miniz/miniz.h"
#include "../deps/miniz.c"

typedef struct {
mz_zip_archive archive;
Expand All @@ -34,7 +34,7 @@ typedef struct {
static size_t lmz_file_read(void *pOpaque, mz_uint64 file_ofs, void *pBuf, size_t n) {
lmz_file_t* zip = pOpaque;
const uv_buf_t buf = uv_buf_init(pBuf, n);
file_ofs += mz_zip_get_archive_file_start_offset(&zip->archive);
file_ofs += zip->archive.m_pState->m_file_archive_start_ofs;
uv_fs_read(zip->loop, &(zip->req), zip->fd, &buf, 1, file_ofs, NULL);
return zip->req.result;
}
Expand Down Expand Up @@ -179,8 +179,7 @@ static int lmz_reader_extract(lua_State *L) {
static int lmz_reader_get_offset(lua_State *L) {
lmz_file_t* zip = luaL_checkudata(L, 1, "miniz_reader");
mz_zip_archive* archive = &(zip->archive);

lua_pushinteger(L, mz_zip_get_archive_file_start_offset(archive));
lua_pushinteger(L, archive->m_pState->m_file_archive_start_ofs);
return 1;
}

Expand Down Expand Up @@ -340,6 +339,11 @@ static int ltinfl(lua_State* L) {
size_t out_len;
int flags = luaL_optinteger(L, 2, 0);
char* out_buf = tinfl_decompress_mem_to_heap(in_buf, in_len, &out_len, flags);
if (!out_buf) {
lua_pushnil(L);
lua_pushstring(L, "Problem inflating data into memory");
return 2;
}
lua_pushlstring(L, out_buf, out_len);
free(out_buf);
return 1;
Expand All @@ -351,6 +355,11 @@ static int ltdefl(lua_State* L) {
size_t out_len;
int flags = luaL_optinteger(L, 2, 0);
char* out_buf = tdefl_compress_mem_to_heap(in_buf, in_len, &out_len, flags);
if (!out_buf) {
lua_pushnil(L);
lua_pushstring(L, "Problem deflating data into memory");
return 2;
}
lua_pushlstring(L, out_buf, out_len);
free(out_buf);
return 1;
Expand Down

0 comments on commit 5882474

Please sign in to comment.