From ec905a5a13299094b53bd706ff580f8335148041 Mon Sep 17 00:00:00 2001 From: HW Date: Mon, 16 Apr 2012 22:51:13 +0200 Subject: [PATCH] fixed various bugs in pointer handling --- blitbuffer.c | 1 + cre.cpp | 7 +++++- djvu.c | 4 ++++ einkfb.c | 5 ++++ ft.c | 68 +++++++++++++++++++++++++++++----------------------- mupdfimg.c | 15 ++++++++---- pdf.c | 3 +++ 7 files changed, 68 insertions(+), 35 deletions(-) diff --git a/blitbuffer.c b/blitbuffer.c index b4469314f..6c6ab70cf 100644 --- a/blitbuffer.c +++ b/blitbuffer.c @@ -61,6 +61,7 @@ static int getHeight(lua_State *L) { static int freeBlitBuffer(lua_State *L) { BlitBuffer *bb = (BlitBuffer*) luaL_checkudata(L, 1, "blitbuffer"); + // should be save if called twice if(bb->allocated && bb->data != NULL) { free(bb->data); bb->data = NULL; diff --git a/cre.cpp b/cre.cpp index feea80114..058a7a579 100644 --- a/cre.cpp +++ b/cre.cpp @@ -78,7 +78,12 @@ static int setGammaIndex(lua_State *L) { static int closeDocument(lua_State *L) { CreDocument *doc = (CreDocument*) luaL_checkudata(L, 1, "credocument"); - delete doc->text_view; + + // should be save if called twice + if(doc->text_view != NULL) { + delete doc->text_view; + doc->text_view = NULL; + } return 0; } diff --git a/djvu.c b/djvu.c index 56a34c32f..8632b4872 100644 --- a/djvu.c +++ b/djvu.c @@ -97,6 +97,8 @@ static int openDocument(lua_State *L) { static int closeDocument(lua_State *L) { DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument"); + + // should be save if called twice if(doc->doc_ref != NULL) { ddjvu_document_release(doc->doc_ref); doc->doc_ref = NULL; @@ -352,6 +354,8 @@ static int getPageText(lua_State *L) { static int closePage(lua_State *L) { DjvuPage *page = (DjvuPage*) luaL_checkudata(L, 1, "djvupage"); + + // should be save if called twice if(page->page_ref != NULL) { ddjvu_page_release(page->page_ref); page->page_ref = NULL; diff --git a/einkfb.c b/einkfb.c index cfc268719..114c8cb04 100644 --- a/einkfb.c +++ b/einkfb.c @@ -123,6 +123,7 @@ static int getSize(lua_State *L) { static int closeFrameBuffer(lua_State *L) { FBInfo *fb = (FBInfo*) luaL_checkudata(L, 1, "einkfb"); + // should be save if called twice if(fb->buf != NULL && fb->buf->data != NULL) { #ifndef EMULATE_READER munmap(fb->buf->data, fb->finfo.smem_len); @@ -131,6 +132,10 @@ static int closeFrameBuffer(lua_State *L) { free(fb->buf->data); #endif fb->buf->data = NULL; + // the blitbuffer in fb->buf should be freed + // by the Lua GC when our object is garbage + // collected sice it is visible as an entry + // in the fb table } return 0; } diff --git a/ft.c b/ft.c index 6d4304913..38f6b13a4 100644 --- a/ft.c +++ b/ft.c @@ -29,26 +29,35 @@ FT_Library freetypelib; +typedef struct KPVFace { + FT_Face face; + int allocated_face; +} KPVFace; + static int newFace(lua_State *L) { const char *filename = luaL_checkstring(L, 1); int pxsize = luaL_optint(L, 2, 16*64); - FT_Face *face = (FT_Face*) lua_newuserdata(L, sizeof(FT_Face)); + KPVFace *face = (KPVFace*) lua_newuserdata(L, sizeof(KPVFace)); luaL_getmetatable(L, "ft_face"); lua_setmetatable(L, -2); - FT_Error error = FT_New_Face(freetypelib, filename, 0, face); + face->allocated_face = 0; + + FT_Error error = FT_New_Face(freetypelib, filename, 0, &face->face); if(error) { return luaL_error(L, "freetype error"); } - error = FT_Set_Pixel_Sizes(*face, 0, pxsize); + face->allocated_face = 1; + + error = FT_Set_Pixel_Sizes(face->face, 0, pxsize); if(error) { - error = FT_Done_Face(*face); + error = FT_Done_Face(face->face); return luaL_error(L, "freetype error"); } - if((*face)->charmap == NULL) { + if(face->face->charmap == NULL) { //TODO //fprintf(stderr, "no unicode charmap found, to be implemented.\n"); } @@ -56,15 +65,15 @@ static int newFace(lua_State *L) { } static int renderGlyph(lua_State *L) { - FT_Face *face = (FT_Face*) luaL_checkudata(L, 1, "ft_face"); + KPVFace *face = (KPVFace*) luaL_checkudata(L, 1, "ft_face"); int ch = luaL_checkint(L, 2); - FT_Error error = FT_Load_Char(*face, ch, FT_LOAD_RENDER); + FT_Error error = FT_Load_Char(face->face, ch, FT_LOAD_RENDER); if(error) { return luaL_error(L, "freetype error"); } - int w = (*face)->glyph->bitmap.width; - int h = (*face)->glyph->bitmap.rows; + int w = face->face->glyph->bitmap.width; + int h = face->face->glyph->bitmap.rows; lua_newtable(L); @@ -80,7 +89,7 @@ static int renderGlyph(lua_State *L) { int y; int x; for(y = 0; y < h; y++) { - uint8_t *src = (*face)->glyph->bitmap.buffer + y * (*face)->glyph->bitmap.pitch; + uint8_t *src = face->face->glyph->bitmap.buffer + y * face->face->glyph->bitmap.pitch; for(x = 0; x < (w/2); x++) { *dst = (src[0] & 0xF0) | (src[1] >> 4); src+=2; @@ -92,23 +101,23 @@ static int renderGlyph(lua_State *L) { } } - lua_pushinteger(L, (*face)->glyph->bitmap_left); + lua_pushinteger(L, face->face->glyph->bitmap_left); lua_setfield(L, -2, "l"); - lua_pushinteger(L, (*face)->glyph->bitmap_top); + lua_pushinteger(L, face->face->glyph->bitmap_top); lua_setfield(L, -2, "t"); - lua_pushinteger(L, (*face)->glyph->metrics.horiAdvance >> 6); + lua_pushinteger(L, face->face->glyph->metrics.horiAdvance >> 6); lua_setfield(L, -2, "r"); - lua_pushinteger(L, (*face)->glyph->advance.x >> 6); + lua_pushinteger(L, face->face->glyph->advance.x >> 6); lua_setfield(L, -2, "ax"); - lua_pushinteger(L, (*face)->glyph->advance.y >> 6); + lua_pushinteger(L, face->face->glyph->advance.y >> 6); lua_setfield(L, -2, "ay"); return 1; } static int hasKerning(lua_State *L) { - FT_Face *face = (FT_Face*) luaL_checkudata(L, 1, "ft_face"); - if(FT_HAS_KERNING((*face))) { + KPVFace *face = (KPVFace*) luaL_checkudata(L, 1, "ft_face"); + if(FT_HAS_KERNING((face->face))) { lua_pushinteger(L, 1); } else { lua_pushinteger(L, 0); @@ -117,11 +126,11 @@ static int hasKerning(lua_State *L) { } static int getKerning(lua_State *L) { - FT_Face *face = (FT_Face*) luaL_checkudata(L, 1, "ft_face"); - int left = FT_Get_Char_Index(*face, luaL_checkint(L, 2)); - int right = FT_Get_Char_Index(*face, luaL_checkint(L, 3)); + KPVFace *face = (KPVFace*) luaL_checkudata(L, 1, "ft_face"); + int left = FT_Get_Char_Index(face->face, luaL_checkint(L, 2)); + int right = FT_Get_Char_Index(face->face, luaL_checkint(L, 3)); FT_Vector kerning; - FT_Error error = FT_Get_Kerning(*face, left, right, FT_KERNING_DEFAULT, &kerning); + FT_Error error = FT_Get_Kerning(face->face, left, right, FT_KERNING_DEFAULT, &kerning); if(error) { return luaL_error(L, "freetype error when getting kerning (l=%d, r=%d)", left, right); } @@ -130,18 +139,18 @@ static int getKerning(lua_State *L) { } static int getHeightAndAscender(lua_State *L) { - FT_Face *face = (FT_Face*) luaL_checkudata(L, 1, "ft_face"); + KPVFace *face = (KPVFace*) luaL_checkudata(L, 1, "ft_face"); double pixels_height,pixels_ascender; double em_size, y_scale; /* compute floating point scale factors */ - em_size = 1.0 * (*face)->units_per_EM; - y_scale = (*face)->size->metrics.y_ppem / em_size; + em_size = 1.0 * face->face->units_per_EM; + y_scale = face->face->size->metrics.y_ppem / em_size; /* convert design distances to floating point pixels */ - pixels_height = (*face)->height * y_scale; - pixels_ascender = (*face)->ascender * y_scale; + pixels_height = face->face->height * y_scale; + pixels_ascender = face->face->ascender * y_scale; lua_pushnumber(L, pixels_height); lua_pushnumber(L, pixels_ascender); @@ -149,13 +158,12 @@ static int getHeightAndAscender(lua_State *L) { } static int doneFace(lua_State *L) { - FT_Face *face = (FT_Face*) luaL_checkudata(L, 1, "ft_face"); - if(*face != NULL) { - FT_Error error = FT_Done_Face(*face); + KPVFace *face = (KPVFace*) luaL_checkudata(L, 1, "ft_face"); + if(face->allocated_face) { + FT_Error error = FT_Done_Face(face->face); if(error) { return luaL_error(L, "freetype error when freeing face"); } - *face = NULL; } return 0; } diff --git a/mupdfimg.c b/mupdfimg.c index 8d745e4a2..dcda3e8fa 100644 --- a/mupdfimg.c +++ b/mupdfimg.c @@ -84,11 +84,11 @@ static int toBlitBuffer(lua_State *L) { } else { fz_try(img->context) { pix = fz_new_pixmap(img->context, fz_device_gray, img->pixmap->w, img->pixmap->h); + fz_convert_pixmap(img->context, pix, img->pixmap); } fz_catch(img->context) { - return luaL_error(L, "can't claim new grayscale fz_pixmap"); + return luaL_error(L, "can't claim or convert new grayscale fz_pixmap"); } - fz_convert_pixmap(img->context, img->pixmap, pix); } ret = newBlitBufferNative(L, img->pixmap->w, img->pixmap->h, &bb); @@ -121,10 +121,17 @@ static int toBlitBuffer(lua_State *L) { static int freeImage(lua_State *L) { Image *img = (Image*) luaL_checkudata(L, 1, "image"); - if(img->pixmap) { + + // should be save if called twice + if(img->pixmap != NULL) { fz_drop_pixmap(img->context, img->pixmap); + img->pixmap = NULL; } - fz_free_context(img->context); + if(img->context != NULL) { + fz_free_context(img->context); + img->context = NULL; + } + return 0; } diff --git a/pdf.c b/pdf.c index 9b89dcc3c..9a34d81d0 100644 --- a/pdf.c +++ b/pdf.c @@ -208,6 +208,8 @@ static int authenticatePassword(lua_State *L) { static int closeDocument(lua_State *L) { PdfDocument *doc = (PdfDocument*) luaL_checkudata(L, 1, "pdfdocument"); + + // should be save if called twice if(doc->xref != NULL) { fz_close_document(doc->xref); doc->xref = NULL; @@ -216,6 +218,7 @@ static int closeDocument(lua_State *L) { fz_free_context(doc->context); doc->context = NULL; } + return 0; }