Skip to content

Commit

Permalink
Merge pull request #60 from chimara/undef
Browse files Browse the repository at this point in the history
Fix undefined behaviour and memory leaks
  • Loading branch information
ptomato authored Feb 22, 2023
2 parents 5db69f5 + 8686611 commit 2571f79
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
8 changes: 4 additions & 4 deletions libchimara/chimara-glk.c
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ allocate_recurse(winid_t win, GtkAllocation *allocation, guint spacing)
for(count = 0; count < lines_to_add; count++)
blanklines[count] = blanks;
blanklines[lines_to_add] = NULL;
gchar *vertical_blanks = g_strjoinv("\n", blanklines);
g_autofree char *vertical_blanks = g_strjoinv("\n", blanklines);
g_free(blanklines);
g_free(blanks);

Expand Down Expand Up @@ -869,11 +869,11 @@ chimara_glk_stopped(ChimaraGlk *self)
{
ChimaraGlkPrivate *priv = chimara_glk_get_instance_private(self);
priv->running = FALSE;
priv->program_name = NULL;
g_clear_pointer(&priv->program_name, g_free);
g_object_notify(G_OBJECT(self), "program-name");
priv->program_info = NULL;
g_clear_pointer(&priv->program_info, g_free);
g_object_notify(G_OBJECT(self), "program-info");
priv->story_name = NULL;
g_clear_pointer(&priv->story_name, g_free);
g_object_notify(G_OBJECT(self), "story-name");
}

Expand Down
1 change: 1 addition & 0 deletions libchimara/chimara-if.c
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ chimara_if_run_game(ChimaraIF *self, const char *game_path, GError **error)
GSList *ptr;
for(count = 0, ptr = args; ptr; count++, ptr = g_slist_next(ptr))
argv[count] = g_strdup(ptr->data);
g_slist_free(args);

/* Set the story name */
g_autofree char *story_name = g_path_get_basename(game_path);
Expand Down
7 changes: 2 additions & 5 deletions libchimara/style.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ static gboolean
style_accept_style_selector(GScanner *scanner, ChimaraGlk *glk)
{
GtkTextTag *current_tag;
gchar *field;
GTokenType token = g_scanner_get_next_token(scanner);
GTokenValue value = g_scanner_cur_value(scanner);

Expand All @@ -115,8 +114,7 @@ style_accept_style_selector(GScanner *scanner, ChimaraGlk *glk)
return FALSE;
}

field = g_strdup(value.v_identifier);
uint32_t wintype = strcmp(field, "buffer") == 0 ? CHIMARA_GLK_TEXT_BUFFER : CHIMARA_GLK_TEXT_GRID;
uint32_t wintype = strcmp(value.v_identifier, "buffer") == 0 ? CHIMARA_GLK_TEXT_BUFFER : CHIMARA_GLK_TEXT_GRID;

/* Parse the tag name to change */
if( g_scanner_peek_next_token(scanner) == '{') {
Expand Down Expand Up @@ -162,14 +160,13 @@ style_accept_style_hint(GScanner *scanner, GtkTextTag *current_tag)
{
GTokenType token = g_scanner_get_next_token(scanner);
GTokenValue value = g_scanner_cur_value(scanner);
gchar *hint;

if(token != G_TOKEN_IDENTIFIER) {
g_scanner_error(scanner, "CSS Error: style hint expected");
return FALSE;
}

hint = g_strdup(value.v_identifier);
g_autofree char *hint = g_strdup(value.v_identifier);

if( !style_accept(scanner, ':') )
return FALSE;
Expand Down
6 changes: 3 additions & 3 deletions libchimara/ui-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ ui_buffer_finish_line_input(winid_t win, gboolean emit_signal)
gtk_text_buffer_delete(window_buffer, &start_iter, &end_iter);

/* Don't include the newline in the input */
char *last_char = inserted_text + strlen(inserted_text) - 1;
if(*last_char == '\n')
*last_char = '\0';
size_t inserted_len = strlen(inserted_text);
if (inserted_len >= 1 && inserted_text[inserted_len] == '\n')
inserted_text[inserted_len] = '\0';

int chars_written = ui_textwin_finish_line_input(win, inserted_text, emit_signal);
g_free(inserted_text);
Expand Down
45 changes: 35 additions & 10 deletions libchimara/ui-style.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,51 @@ style_cascade_colors(GtkTextTag *tag, GtkTextTag *glk_tag, GtkTextTag *default_t
gboolean foreground_set = FALSE;
gboolean background_set = FALSE;
gint reverse_color;
GdkRGBA *fg = NULL;
GdkRGBA *bg = NULL;

// Default color
reverse_color = GPOINTER_TO_INT( g_object_get_data(G_OBJECT(default_tag), "reverse-color") );
g_object_get(default_tag, "foreground-set", &foreground_set, "background-set", &background_set, NULL);
if(foreground_set)
g_object_get(default_tag, "foreground-rgba", reverse_color ? background : foreground, NULL);
g_object_get(default_tag, "foreground-rgba", reverse_color ? &bg : &fg, NULL);
if(background_set)
g_object_get(default_tag, "background-rgba", reverse_color ? foreground : background, NULL);
g_object_get(default_tag, "background-rgba", reverse_color ? &fg : &bg, NULL);

// Player defined color
reverse_color = GPOINTER_TO_INT( g_object_get_data(G_OBJECT(tag), "reverse-color") );
g_object_get(tag, "foreground-set", &foreground_set, "background-set", &background_set, NULL);
if(foreground_set)
g_object_get(tag, "foreground-rgba", reverse_color ? background : foreground, NULL);
if(background_set)
g_object_get(tag, "background-rgba", reverse_color ? foreground : background, NULL);
if(foreground_set) {
GdkRGBA **dest = reverse_color ? &bg : &fg;
if (*dest != NULL)
gdk_rgba_free(*dest);
g_object_get(tag, "foreground-rgba", dest, NULL);
}
if(background_set) {
GdkRGBA **dest = reverse_color ? &fg : &bg;
if (*dest != NULL)
gdk_rgba_free(*dest);
g_object_get(tag, "background-rgba", dest, NULL);
}

// GLK defined color
reverse_color = GPOINTER_TO_INT( g_object_get_data(G_OBJECT(glk_tag), "reverse-color") );
g_object_get(glk_tag, "foreground-set", &foreground_set, "background-set", &background_set, NULL);
if(foreground_set)
g_object_get(glk_tag, "foreground-rgba", reverse_color ? background : foreground, NULL);
if(background_set)
g_object_get(glk_tag, "background-rgba", reverse_color ? foreground : background, NULL);
if(foreground_set) {
GdkRGBA **dest = reverse_color ? &bg : &fg;
if (*dest != NULL)
gdk_rgba_free(*dest);
g_object_get(glk_tag, "foreground-rgba", dest, NULL);
}
if(background_set) {
GdkRGBA **dest = reverse_color ? &fg : &bg;
if (*dest != NULL)
gdk_rgba_free(*dest);
g_object_get(glk_tag, "background-rgba", dest, NULL);
}

*foreground = fg;
*background = bg;
}

/* Internal function: changes a GTK tag to correspond with the given style. */
Expand Down Expand Up @@ -369,6 +390,7 @@ text_tag_to_attr_list(GtkTextTag *tag, PangoAttrList *list)
pango_attr_list_insert(list,
pango_attr_foreground_new(foreground->red, foreground->green, foreground->blue));
}
gdk_rgba_free(foreground);

g_object_get(tag,
"background-set", &set,
Expand All @@ -378,6 +400,7 @@ text_tag_to_attr_list(GtkTextTag *tag, PangoAttrList *list)
pango_attr_list_insert(list,
pango_attr_background_new(background->red, background->green, background->blue));
}
gdk_rgba_free(background);

g_object_get(tag,
"language-set", &set,
Expand All @@ -387,13 +410,15 @@ text_tag_to_attr_list(GtkTextTag *tag, PangoAttrList *list)
pango_attr_list_insert(list,
pango_attr_language_new( pango_language_from_string(string) ));
}
g_free(string);

/* Font description updates the following properties simultaniously:
* family, style, weight, variant, stretch, size.
* FIXME: Except it doesn't really.
*/
g_object_get(tag, "font-desc", &font_desc, NULL);
pango_attr_list_insert(list, pango_attr_font_desc_new(font_desc));
pango_font_description_free(font_desc);

g_object_get(tag,
"strikethrough-set", &set,
Expand Down

0 comments on commit 2571f79

Please sign in to comment.