diff options
author | Qball Cow <qball@gmpclient.org> | 2014-04-25 09:11:10 +0200 |
---|---|---|
committer | Qball Cow <qball@gmpclient.org> | 2014-04-25 09:11:10 +0200 |
commit | 832c2cb584112c007a6bd806226d8466a80506fa (patch) | |
tree | 0cd967551bf33fd2774b71f04b9b1cb0817693ff /source | |
parent | a0a5400c72e6af6b56b07b5a8b0f3c39a11daea7 (diff) |
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.
Diffstat (limited to 'source')
-rw-r--r-- | source/run-dialog.c | 3 | ||||
-rw-r--r-- | source/textbox.c | 15 |
2 files changed, 15 insertions, 3 deletions
diff --git a/source/run-dialog.c b/source/run-dialog.c index 8220a450..15875f41 100644 --- a/source/run-dialog.c +++ b/source/run-dialog.c @@ -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. diff --git a/source/textbox.c b/source/textbox.c index 0e1e7c7d..00c4fe08 100644 --- a/source/textbox.c +++ b/source/textbox.c @@ -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 ); |