From 0e176199fcc05cf0eac3d43e0e4d9e679a74960c Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Mon, 17 Oct 2016 18:21:03 +0200 Subject: [PATCH] Fix some possible null pointer dereference. (clang-check) --- include/widgets/widget.h | 14 ++++++++++++++ source/widgets/box.c | 1 + source/widgets/listview.c | 10 +++++----- source/widgets/widget.c | 14 ++++++++++++++ 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/include/widgets/widget.h b/include/widgets/widget.h index 8edc3de5..2b871ec1 100644 --- a/include/widgets/widget.h +++ b/include/widgets/widget.h @@ -108,6 +108,20 @@ int widget_get_height ( widget *widget ); */ int widget_get_width ( widget *widget ); +/** + * @param widget The widget handle + * + * @returns the y postion of the widget relative to its parent. + */ +int widget_get_y_pos ( widget *widget ); + +/** + * @param widget The widget handle + * + * @returns the x postion of the widget relative to its parent. + */ +int widget_get_x_pos ( widget *widget ); + /** * @param widget The widget handle * diff --git a/source/widgets/box.c b/source/widgets/box.c index 8aec12b1..6f5b12de 100644 --- a/source/widgets/box.c +++ b/source/widgets/box.c @@ -208,6 +208,7 @@ static void box_free ( widget *wid ) void box_add ( box *box, widget *child, gboolean expand, gboolean end ) { + if ( box == NULL ) return; child->expand = expand; child->end = end; child->parent = WIDGET ( box ); diff --git a/source/widgets/listview.c b/source/widgets/listview.c index a5184a18..055be2f6 100644 --- a/source/widgets/listview.c +++ b/source/widgets/listview.c @@ -151,7 +151,7 @@ static void listview_draw ( widget *wid, cairo_t *draw ) unsigned int width = lv->widget.w - lv->padding * ( lv->cur_columns - 1 ); if ( widget_enabled ( WIDGET ( lv->scrollbar ) ) ) { width -= lv->padding; - width -= lv->scrollbar->widget.w; + width -= widget_get_width ( WIDGET ( lv->scrollbar ) ); } unsigned int element_width = ( width ) / lv->cur_columns; for ( unsigned int i = 0; i < max; i++ ) { @@ -237,8 +237,8 @@ static void listview_resize ( widget *wid, short w, short h ) lv->max_rows = ( lv->padding + lv->widget.h ) / ( lv->element_height + lv->padding ); lv->max_elements = lv->max_rows * lv->menu_columns; - widget_move ( WIDGET ( lv->scrollbar ), lv->widget.w - lv->scrollbar->widget.w, 0 ); - widget_resize ( WIDGET ( lv->scrollbar ), lv->scrollbar->widget.w, h ); + widget_move ( WIDGET ( lv->scrollbar ), lv->widget.w - widget_get_width ( WIDGET (lv->scrollbar ) ), 0 ); + widget_resize ( WIDGET ( lv->scrollbar ), widget_get_width ( WIDGET ( lv->scrollbar ) ), h ); listview_recompute_elements ( lv ); widget_queue_redraw ( wid ); @@ -260,8 +260,8 @@ static gboolean listview_clicked ( widget *wid, xcb_button_press_event_t *xce, G if ( widget_enabled ( WIDGET ( lv->scrollbar ) ) && widget_intersect ( WIDGET ( lv->scrollbar ), xce->event_x, xce->event_y ) ) { // Forward to handler of scrollbar. xcb_button_press_event_t xce2 = *xce; - xce->event_x -= lv->scrollbar->widget.x; - xce->event_y -= lv->scrollbar->widget.y; + xce->event_x -= widget_get_x_pos ( WIDGET ( lv->scrollbar ) ); + xce->event_y -= widget_get_y_pos ( WIDGET ( lv->scrollbar ) ); return widget_clicked ( WIDGET ( lv->scrollbar ), &xce2 ); } // Handle the boxes. diff --git a/source/widgets/widget.c b/source/widgets/widget.c index 916ce61c..48642cb7 100644 --- a/source/widgets/widget.c +++ b/source/widgets/widget.c @@ -93,6 +93,20 @@ int widget_get_width ( widget *widget ) } return 0; } +int widget_get_x_pos ( widget *widget ) +{ + if ( widget ) { + return widget->x; + } + return 0; +} +int widget_get_y_pos ( widget *widget ) +{ + if ( widget ) { + return widget->y; + } + return 0; +} void widget_update ( widget *widget ) {