Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling #13

Merged
merged 6 commits into from
Dec 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions obj_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ cr_get_dash (lua_State *L) {
cnt = cairo_get_dash_count(*obj);
if (cnt > 0) {
dashes = malloc(sizeof(double) * cnt);
assert(dashes);
if (!dashes) {
return luaL_error(L, "out of memory");
}
}

cairo_get_dash(*obj, dashes, &offset);
Expand Down Expand Up @@ -642,7 +644,9 @@ cr_set_dash (lua_State *L) {
num_dashes = lua_objlen(L, 2);
if (num_dashes > 0) {
dashes = malloc(sizeof(double) * num_dashes);
assert(dashes);
if (!dashes) {
return luaL_error(L, "out of memory");
}
dashtotal = 0;

for (i = 0; i < num_dashes; ++i) {
Expand Down
4 changes: 3 additions & 1 deletion obj_font_face.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ user_font_face_create (lua_State *L) {

lua_createtable(L, 4, 0);
info = malloc(sizeof(UserFontInfo));
assert(info);
if (!info) {
return luaL_error(L, "out of memory");
}
info->L = L;
info->ref = LUA_NOREF;

Expand Down
10 changes: 7 additions & 3 deletions obj_surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ image_surface_create_from_data (lua_State *L) {

surface = create_surface_userdata(L);
surface->image_buffer = malloc(data_len);
assert(surface->image_buffer);
if (!surface->image_buffer) {
return luaL_error(L, "out of memory");
}
memcpy(surface->image_buffer, data, data_len);
surface->surface = cairo_image_surface_create_for_data(
surface->image_buffer, fmt, width, height, stride);
Expand Down Expand Up @@ -474,7 +476,9 @@ surface_get_gdk_pixbuf (lua_State *L) {
strideo = ((3 * width) + 7) & ~7; /* align to 8 bytes */
buffer_len = strideo * height;
buffer = malloc(buffer_len);
assert(buffer);
if (!buffer) {
return luaL_error(L, "out of memory");
}

rowpi = (const char *) cairo_image_surface_get_data(*surface);
rowpo = buffer;
Expand Down Expand Up @@ -719,7 +723,7 @@ set_mime_data(lua_State *L)
mime_data = luaL_checklstring(L, 3, &data_length);
mime_priv = malloc(data_length);
if (!mime_priv) {
return push_cairo_status(L, CAIRO_STATUS_NO_MEMORY);
return luaL_error(L, "out of memory");
}
memcpy(mime_priv, mime_data, data_length);
}
Expand Down
13 changes: 9 additions & 4 deletions oocairo.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,9 @@ from_lua_glyph_array (lua_State *L, cairo_glyph_t **glyphs, int *num_glyphs,
return;
}
*glyphs = GLYPHS_ALLOC(*num_glyphs);
assert(*glyphs);
if (!*glyphs) {
return luaL_error(L, "out of memory");
}

for (i = 0; i < *num_glyphs; ++i) {
lua_rawgeti(L, pos, i + 1);
Expand Down Expand Up @@ -595,7 +597,9 @@ from_lua_clusters_table (lua_State *L, cairo_text_cluster_t **clusters,
return;
}
*clusters = cairo_text_cluster_allocate(*num);
assert(*clusters);
if (!*clusters) {
return luaL_error(L, "out of memory");
}

for (i = 0; i < *num; ++i) {
lua_rawgeti(L, pos, i + 1);
Expand Down Expand Up @@ -795,8 +799,9 @@ free_surface_userdata (SurfaceUserdata *ud) {
static char *
my_strdup (const char *s) {
char *copy = malloc(strlen(s) + 1);
assert(copy);
strcpy(copy, s);
if (copy) {
strcpy(copy, s);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Memo to myself: Get rid of my_strdup() and just use strdup() instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let us keep it as it is, because strdup is only C2x or Posix standard conform. I just experienced in another project very confusing problems using strdup (or _strdup?) under windows. Such a small function is not worth the hassle.

return copy;
}

Expand Down