From c6a9de8c4a1dd9fb5c179ff323f0508f72974b9e Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Sat, 9 Jan 2016 19:25:03 +0100 Subject: [PATCH] Use snprintf instead of sprintf --- include/scrollbar.h | 2 +- include/textbox.h | 16 ++++++++-------- include/widget.h | 6 +++--- source/dialogs/window.c | 15 ++++++++------- source/rofi.c | 12 ++++++------ source/widget.c | 13 +++++-------- test/textbox-test.c | 2 +- 7 files changed, 32 insertions(+), 34 deletions(-) diff --git a/include/scrollbar.h b/include/scrollbar.h index 960d241c..2c9d732d 100644 --- a/include/scrollbar.h +++ b/include/scrollbar.h @@ -14,7 +14,7 @@ */ typedef struct _scrollbar { - Widget widget; + Widget widget; unsigned int length; unsigned int pos; unsigned int pos_length; diff --git a/include/textbox.h b/include/textbox.h index 94fdcb7b..7c744b14 100644 --- a/include/textbox.h +++ b/include/textbox.h @@ -40,14 +40,14 @@ typedef struct typedef enum { - TB_AUTOHEIGHT = 1 << 0, - TB_AUTOWIDTH = 1 << 1, - TB_LEFT = 1 << 16, - TB_RIGHT = 1 << 17, - TB_CENTER = 1 << 18, - TB_EDITABLE = 1 << 19, - TB_MARKUP = 1 << 20, - TB_WRAP = 1 << 21, + TB_AUTOHEIGHT = 1 << 0, + TB_AUTOWIDTH = 1 << 1, + TB_LEFT = 1 << 16, + TB_RIGHT = 1 << 17, + TB_CENTER = 1 << 18, + TB_EDITABLE = 1 << 19, + TB_MARKUP = 1 << 20, + TB_WRAP = 1 << 21, } TextboxFlags; typedef enum diff --git a/include/widget.h b/include/widget.h index 5540260a..4271fdb2 100644 --- a/include/widget.h +++ b/include/widget.h @@ -21,7 +21,7 @@ typedef struct _Widget } Widget; /** Macro to get widget from an implementation (e.g. textbox/scrollbar) */ -#define WIDGET(a) (a != NULL?&(a->widget):NULL) +#define WIDGET( a ) ( a != NULL ? &( a->widget ) : NULL ) /** * @param widget The widget to check @@ -32,7 +32,7 @@ typedef struct _Widget * * @return TRUE if x,y falls within the widget */ -int widget_intersect ( const Widget *widget, int x, int y); +int widget_intersect ( const Widget *widget, int x, int y ); /** * @param widget The widget to move @@ -41,7 +41,7 @@ int widget_intersect ( const Widget *widget, int x, int y); * * Moves the widget. */ -void widget_move(Widget *widget, short x, short y); +void widget_move ( Widget *widget, short x, short y ); /*@}*/ #endif // ROFI_WIDGET_H diff --git a/source/dialogs/window.c b/source/dialogs/window.c index 670caeea..31d71a55 100644 --- a/source/dialogs/window.c +++ b/source/dialogs/window.c @@ -451,11 +451,11 @@ static void _window_mode_load_data ( Mode *sw, unsigned int cd ) desktops = 1; } if ( pd->config_i3_mode ) { - sprintf ( pattern, "%%-%ds %%s", MAX ( 5, classfield ) ); + snprintf ( pattern, 50, "%%-%ds %%s", MAX ( 5, classfield ) ); } else{ - sprintf ( pattern, "%%-%ds %%-%ds %%s", desktops < 10 ? 1 : 2, - MAX ( 5, classfield ) ); + snprintf ( pattern, 50, "%%-%ds %%-%ds %%s", desktops < 10 ? 1 : 2, + MAX ( 5, classfield ) ); } pd->cmd_list = g_malloc0_n ( ( pd->ids->len + 1 ), sizeof ( char* ) ); @@ -469,7 +469,8 @@ static void _window_mode_load_data ( Mode *sw, unsigned int cd ) unsigned long wmdesktop; char desktop[5]; desktop[0] = 0; - char *line = g_malloc ( strlen ( c->title ) + strlen ( c->class ) + classfield + 50 ); + size_t len = strlen ( c->title ) + strlen ( c->class ) + classfield + 50; + char *line = g_malloc ( len ); if ( !pd->config_i3_mode ) { // find client's desktop. if ( !window_get_cardinal_prop ( display, c->window, netatoms[_NET_WM_DESKTOP], &wmdesktop, 1 ) ) { @@ -482,13 +483,13 @@ static void _window_mode_load_data ( Mode *sw, unsigned int cd ) } if ( wmdesktop < 0xFFFFFFFF ) { - sprintf ( desktop, "%d", (int) wmdesktop ); + snprintf ( desktop, 5, "%d", (int) wmdesktop ); } - sprintf ( line, pattern, desktop, c->class, c->title ); + snprintf ( line, len, pattern, desktop, c->class, c->title ); } else{ - sprintf ( line, pattern, c->class, c->title ); + snprintf ( line, len, pattern, c->class, c->title ); } pd->cmd_list[pd->cmd_list_length++] = line; diff --git a/source/rofi.c b/source/rofi.c index 0fc7f536..f505b052 100644 --- a/source/rofi.c +++ b/source/rofi.c @@ -742,13 +742,13 @@ static void menu_mouse_navigation ( MenuState *state, XButtonEvent *xbe ) return; } else { - if ( state->scrollbar && widget_intersect ( &(state->scrollbar->widget), xbe->x, xbe->y ) ) { + if ( state->scrollbar && widget_intersect ( &( state->scrollbar->widget ), xbe->x, xbe->y ) ) { state->selected = scrollbar_clicked ( state->scrollbar, xbe->y ); state->update = TRUE; return; } for ( unsigned int i = 0; config.sidebar_mode == TRUE && i < num_modi; i++ ) { - if ( widget_intersect ( &(modi[i].tb->widget), xbe->x, xbe->y ) ) { + if ( widget_intersect ( &( modi[i].tb->widget ), xbe->x, xbe->y ) ) { *( state->selected_line ) = 0; state->retv = MENU_QUICK_SWITCH | ( i & MENU_LOWER_MASK ); state->quit = TRUE; @@ -757,7 +757,7 @@ static void menu_mouse_navigation ( MenuState *state, XButtonEvent *xbe ) } } for ( unsigned int i = 0; i < state->max_elements; i++ ) { - if ( widget_intersect ( &(state->boxes[i]->widget), xbe->x, xbe->y ) ) { + if ( widget_intersect ( &( state->boxes[i]->widget ), xbe->x, xbe->y ) ) { // Only allow items that are visible to be selected. if ( ( state->last_offset + i ) >= state->filtered_lines ) { break; @@ -1128,7 +1128,7 @@ static void menu_paste ( MenuState *state, XSelectionEvent *xse ) static void menu_resize ( MenuState *state ) { unsigned int sbw = config.line_margin + 8; - widget_move ( WIDGET(state->scrollbar), state->w - state->border - sbw, state->top_offset ); + widget_move ( WIDGET ( state->scrollbar ), state->w - state->border - sbw, state->top_offset ); if ( config.sidebar_mode == TRUE ) { int width = ( state->w - ( 2 * ( state->border ) + ( num_modi - 1 ) * config.line_margin ) ) / num_modi; for ( unsigned int j = 0; j < num_modi; j++ ) { @@ -1141,7 +1141,7 @@ static void menu_resize ( MenuState *state ) int entrybox_width = state->w - ( 2 * ( state->border ) ) - textbox_get_width ( state->prompt_tb ) - textbox_get_width ( state->case_indicator ); textbox_moveresize ( state->text, state->text->widget.x, state->text->widget.y, entrybox_width, state->line_height ); - widget_move ( WIDGET(state->case_indicator), state->w - state->border - textbox_get_width ( state->case_indicator ), state->border ); + widget_move ( WIDGET ( state->case_indicator ), state->w - state->border - textbox_get_width ( state->case_indicator ), state->border ); /** * Resize in Height */ @@ -1334,7 +1334,7 @@ MenuReturn menu ( Mode *sw, char **input, char *prompt, unsigned int *selected_l state.top_offset = state.border * 1 + state.line_height + 2 + config.line_margin * 2; // Move indicator to end. - widget_move ( WIDGET(state.case_indicator), state.border + textbox_get_width ( state.prompt_tb ) + entrybox_width, state.border ); + widget_move ( WIDGET ( state.case_indicator ), state.border + textbox_get_width ( state.prompt_tb ) + entrybox_width, state.border ); textbox_text ( state.case_indicator, get_matching_state () ); state.message_tb = NULL; diff --git a/source/widget.c b/source/widget.c index 648462a5..15162ba7 100644 --- a/source/widget.c +++ b/source/widget.c @@ -1,10 +1,9 @@ #include #include "widget.h" - -int widget_intersect ( const Widget *widget, int x, int y) +int widget_intersect ( const Widget *widget, int x, int y ) { - if(widget == NULL ){ + if ( widget == NULL ) { return FALSE; } @@ -14,14 +13,12 @@ int widget_intersect ( const Widget *widget, int x, int y) } } return FALSE; - } -void widget_move(Widget *widget, short x, short y) +void widget_move ( Widget *widget, short x, short y ) { - if ( widget != NULL ){ + if ( widget != NULL ) { widget->x = x; - widget->y = y; + widget->y = y; } - } diff --git a/test/textbox-test.c b/test/textbox-test.c index 47a5c83d..e9d86c94 100644 --- a/test/textbox-test.c +++ b/test/textbox-test.c @@ -170,7 +170,7 @@ int main ( G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv ) textbox_font ( box, HIGHLIGHT ); textbox_draw ( box, draw ); - widget_move ( WIDGET(box), 12, 13 ); + widget_move ( WIDGET ( box ), 12, 13 ); TASSERT ( box->widget.x == 12 ); TASSERT ( box->widget.y == 13 );