1
0
Fork 0
mirror of https://github.com/davatorium/rofi.git synced 2025-02-03 15:34:54 -05:00

Add comment to avoid mistake later, remove alloca

* Add comments so previous mistake is not repeated.
    * Remove alloca, replace it by strdup/asprintf.

Code now passes cppcheck.
This commit is contained in:
Qball Cow 2014-04-25 09:11:10 +02:00
parent a0a5400c72
commit 832c2cb584
2 changed files with 15 additions and 3 deletions

View file

@ -114,6 +114,7 @@ static pid_t exec_cmd ( const char *cmd, int run_in_term )
}
retv = reallocate ( retv, ( index + 2 ) * sizeof ( element* ) );
retv[index] = allocate ( sizeof ( element ) );
// remove trailing \n
buffer[strlen ( buffer ) - 1] = '\0';
char * start = NULL;
retv[index]->index = strtol ( buffer, &start, 10 );
@ -208,6 +209,7 @@ static void delete_entry ( const char *cmd )
}
retv = reallocate ( retv, ( index + 2 ) * sizeof ( element* ) );
retv[index] = allocate ( sizeof ( element ) );
// remove trailing \n
buffer[strlen ( buffer ) - 1] = '\0';
retv[index]->index = strtol ( buffer, &start, 10 );
snprintf ( retv[index]->name, RUN_DIALOG_NAME_LENGTH, "%s", start + 1 );
@ -291,6 +293,7 @@ static char ** get_apps ( )
{
continue;
}
// remove trailing \n
buffer[strlen ( buffer ) - 1] = '\0';
char *start = NULL;
// Don't use result.

View file

@ -210,7 +210,7 @@ void textbox_draw ( textbox *tb )
// clear canvas
XftDrawRect ( draw, &tb->color_bg, 0, 0, tb->w, tb->h );
char *line = tb->text,
char *line = NULL,
*text = tb->text ? tb->text : "",
*prompt = tb->prompt ? tb->prompt : "";
@ -229,8 +229,11 @@ void textbox_draw ( textbox *tb )
length = text_len + prompt_len;
cursor_offset = MIN ( tb->cursor + prompt_len, length );
line = alloca ( length + 10 );
sprintf ( line, "%s %s", prompt, text );
if(asprintf ( &line, "%s %s", prompt, text ) == -1) {
// Something is _really_ wrong.. bail out
fprintf(stderr, "Failed to allocate string\n");
abort();
}
// replace spaces so XftTextExtents8 includes their width
for ( int i = 0; i < length; i++ )
@ -248,6 +251,10 @@ void textbox_draw ( textbox *tb )
// restore correct text string with spaces
sprintf ( line, "%s %s", prompt, text );
}
else
{
line = strdup(text);
}
// calc full input text width
// Calculate the right size, so no characters are cut off.
@ -289,6 +296,8 @@ void textbox_draw ( textbox *tb )
XftDrawRect ( draw, &tb->color_fg, cursor_x + SIDE_MARGIN, 2, cursor_width, line_height - 4 );
}
free(line);
XftDrawRect ( draw, &tb->color_bg, tb->w - SIDE_MARGIN, 0, SIDE_MARGIN, tb->h );
// flip canvas to window
XCopyArea ( display, canvas, tb->window, context, 0, 0, tb->w, tb->h, 0, 0 );