Use snprintf instead of sprintf

This commit is contained in:
Dave Davenport 2016-01-09 19:25:03 +01:00
parent d7dab65e5b
commit c6a9de8c4a
7 changed files with 32 additions and 34 deletions

View File

@ -14,7 +14,7 @@
*/ */
typedef struct _scrollbar typedef struct _scrollbar
{ {
Widget widget; Widget widget;
unsigned int length; unsigned int length;
unsigned int pos; unsigned int pos;
unsigned int pos_length; unsigned int pos_length;

View File

@ -40,14 +40,14 @@ typedef struct
typedef enum typedef enum
{ {
TB_AUTOHEIGHT = 1 << 0, TB_AUTOHEIGHT = 1 << 0,
TB_AUTOWIDTH = 1 << 1, TB_AUTOWIDTH = 1 << 1,
TB_LEFT = 1 << 16, TB_LEFT = 1 << 16,
TB_RIGHT = 1 << 17, TB_RIGHT = 1 << 17,
TB_CENTER = 1 << 18, TB_CENTER = 1 << 18,
TB_EDITABLE = 1 << 19, TB_EDITABLE = 1 << 19,
TB_MARKUP = 1 << 20, TB_MARKUP = 1 << 20,
TB_WRAP = 1 << 21, TB_WRAP = 1 << 21,
} TextboxFlags; } TextboxFlags;
typedef enum typedef enum

View File

@ -21,7 +21,7 @@ typedef struct _Widget
} Widget; } Widget;
/** Macro to get widget from an implementation (e.g. textbox/scrollbar) */ /** 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 * @param widget The widget to check
@ -32,7 +32,7 @@ typedef struct _Widget
* *
* @return TRUE if x,y falls within the 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 * @param widget The widget to move
@ -41,7 +41,7 @@ int widget_intersect ( const Widget *widget, int x, int y);
* *
* Moves the widget. * 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 #endif // ROFI_WIDGET_H

View File

@ -451,11 +451,11 @@ static void _window_mode_load_data ( Mode *sw, unsigned int cd )
desktops = 1; desktops = 1;
} }
if ( pd->config_i3_mode ) { if ( pd->config_i3_mode ) {
sprintf ( pattern, "%%-%ds %%s", MAX ( 5, classfield ) ); snprintf ( pattern, 50, "%%-%ds %%s", MAX ( 5, classfield ) );
} }
else{ else{
sprintf ( pattern, "%%-%ds %%-%ds %%s", desktops < 10 ? 1 : 2, snprintf ( pattern, 50, "%%-%ds %%-%ds %%s", desktops < 10 ? 1 : 2,
MAX ( 5, classfield ) ); MAX ( 5, classfield ) );
} }
pd->cmd_list = g_malloc0_n ( ( pd->ids->len + 1 ), sizeof ( char* ) ); 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; unsigned long wmdesktop;
char desktop[5]; char desktop[5];
desktop[0] = 0; 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 ) { if ( !pd->config_i3_mode ) {
// find client's desktop. // find client's desktop.
if ( !window_get_cardinal_prop ( display, c->window, netatoms[_NET_WM_DESKTOP], &wmdesktop, 1 ) ) { 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 ) { 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{ else{
sprintf ( line, pattern, c->class, c->title ); snprintf ( line, len, pattern, c->class, c->title );
} }
pd->cmd_list[pd->cmd_list_length++] = line; pd->cmd_list[pd->cmd_list_length++] = line;

View File

@ -742,13 +742,13 @@ static void menu_mouse_navigation ( MenuState *state, XButtonEvent *xbe )
return; return;
} }
else { 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->selected = scrollbar_clicked ( state->scrollbar, xbe->y );
state->update = TRUE; state->update = TRUE;
return; return;
} }
for ( unsigned int i = 0; config.sidebar_mode == TRUE && i < num_modi; i++ ) { 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->selected_line ) = 0;
state->retv = MENU_QUICK_SWITCH | ( i & MENU_LOWER_MASK ); state->retv = MENU_QUICK_SWITCH | ( i & MENU_LOWER_MASK );
state->quit = TRUE; 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++ ) { 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. // Only allow items that are visible to be selected.
if ( ( state->last_offset + i ) >= state->filtered_lines ) { if ( ( state->last_offset + i ) >= state->filtered_lines ) {
break; break;
@ -1128,7 +1128,7 @@ static void menu_paste ( MenuState *state, XSelectionEvent *xse )
static void menu_resize ( MenuState *state ) static void menu_resize ( MenuState *state )
{ {
unsigned int sbw = config.line_margin + 8; 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 ) { if ( config.sidebar_mode == TRUE ) {
int width = ( state->w - ( 2 * ( state->border ) + ( num_modi - 1 ) * config.line_margin ) ) / num_modi; int width = ( state->w - ( 2 * ( state->border ) + ( num_modi - 1 ) * config.line_margin ) ) / num_modi;
for ( unsigned int j = 0; j < num_modi; j++ ) { 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 ) int entrybox_width = state->w - ( 2 * ( state->border ) ) - textbox_get_width ( state->prompt_tb )
- textbox_get_width ( state->case_indicator ); - textbox_get_width ( state->case_indicator );
textbox_moveresize ( state->text, state->text->widget.x, state->text->widget.y, entrybox_width, state->line_height ); 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 * 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; state.top_offset = state.border * 1 + state.line_height + 2 + config.line_margin * 2;
// Move indicator to end. // 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 () ); textbox_text ( state.case_indicator, get_matching_state () );
state.message_tb = NULL; state.message_tb = NULL;

View File

@ -1,10 +1,9 @@
#include <glib.h> #include <glib.h>
#include "widget.h" #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; return FALSE;
} }
@ -14,14 +13,12 @@ int widget_intersect ( const Widget *widget, int x, int y)
} }
} }
return FALSE; 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->x = x;
widget->y = y; widget->y = y;
} }
} }

View File

@ -170,7 +170,7 @@ int main ( G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv )
textbox_font ( box, HIGHLIGHT ); textbox_font ( box, HIGHLIGHT );
textbox_draw ( box, draw ); textbox_draw ( box, draw );
widget_move ( WIDGET(box), 12, 13 ); widget_move ( WIDGET ( box ), 12, 13 );
TASSERT ( box->widget.x == 12 ); TASSERT ( box->widget.x == 12 );
TASSERT ( box->widget.y == 13 ); TASSERT ( box->widget.y == 13 );