summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTeruaki Kawashima <teru@rockbox.org>2009-12-04 12:53:14 +0000
committerTeruaki Kawashima <teru@rockbox.org>2009-12-04 12:53:14 +0000
commitcc9e336fa14437bae8edcf11946472318d89ad9a (patch)
tree57427955df233d33d18f37218f40ff47ca3c6a88
parent31f2981d99c7af391388124196bdfee31269e11b (diff)
downloadrockbox-cc9e336fa14437bae8edcf11946472318d89ad9a.tar.gz
rockbox-cc9e336fa14437bae8edcf11946472318d89ad9a.zip
text editor: Return pointer to buffer but position in buffer for ACTION_GET to simpify code.
Use strlcpy instead of strcpy to prevent buffer overflow. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@23845 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/plugins/text_editor.c54
1 files changed, 26 insertions, 28 deletions
diff --git a/apps/plugins/text_editor.c b/apps/plugins/text_editor.c
index a55165e63e..a8c8c2d836 100644
--- a/apps/plugins/text_editor.c
+++ b/apps/plugins/text_editor.c
@@ -41,13 +41,13 @@ static int last_char_index = 0;
41#define ACTION_UPDATE 3 41#define ACTION_UPDATE 3
42#define ACTION_CONCAT 4 42#define ACTION_CONCAT 4
43 43
44int _do_action(int action, char* str, int line); 44char* _do_action(int action, char* str, int line);
45#ifndef HAVE_ADJUSTABLE_CPU_FREQ 45#ifndef HAVE_ADJUSTABLE_CPU_FREQ
46#define do_action _do_action 46#define do_action _do_action
47#else 47#else
48int do_action(int action, char* str, int line) 48char* do_action(int action, char* str, int line)
49{ 49{
50 int r; 50 char *r;
51 rb->cpu_boost(1); 51 rb->cpu_boost(1);
52 r = _do_action(action,str,line); 52 r = _do_action(action,str,line);
53 rb->cpu_boost(0); 53 rb->cpu_boost(0);
@@ -55,7 +55,7 @@ int do_action(int action, char* str, int line)
55} 55}
56#endif 56#endif
57 57
58int _do_action(int action, char* str, int line) 58char* _do_action(int action, char* str, int line)
59{ 59{
60 int len, lennew; 60 int len, lennew;
61 int i=0,c=0; 61 int i=0,c=0;
@@ -74,7 +74,7 @@ int _do_action(int action, char* str, int line)
74 case ACTION_INSERT: 74 case ACTION_INSERT:
75 len = rb->strlen(str)+1; 75 len = rb->strlen(str)+1;
76 if ( char_count+ len > MAX_CHARS ) 76 if ( char_count+ len > MAX_CHARS )
77 return 0; 77 return NULL;
78 rb->memmove(&buffer[c+len],&buffer[c],char_count-c); 78 rb->memmove(&buffer[c+len],&buffer[c],char_count-c);
79 rb->strcpy(&buffer[c],str); 79 rb->strcpy(&buffer[c],str);
80 char_count += len; 80 char_count += len;
@@ -82,10 +82,7 @@ int _do_action(int action, char* str, int line)
82 break; 82 break;
83 case ACTION_GET: 83 case ACTION_GET:
84 if (line > line_count) 84 if (line > line_count)
85 return 0; 85 return &buffer[0];
86 last_action_line = i;
87 last_char_index = c;
88 return c;
89 break; 86 break;
90 case ACTION_REMOVE: 87 case ACTION_REMOVE:
91 if (line > line_count) 88 if (line > line_count)
@@ -97,34 +94,34 @@ int _do_action(int action, char* str, int line)
97 break; 94 break;
98 case ACTION_UPDATE: 95 case ACTION_UPDATE:
99 if (line > line_count) 96 if (line > line_count)
100 return 0; 97 return NULL;
101 len = rb->strlen(&buffer[c])+1; 98 len = rb->strlen(&buffer[c])+1;
102 lennew = rb->strlen(str)+1; 99 lennew = rb->strlen(str)+1;
103 if ( char_count+ lennew-len > MAX_CHARS ) 100 if ( char_count+ lennew-len > MAX_CHARS )
104 return 0; 101 return NULL;
105 rb->memmove(&buffer[c+lennew],&buffer[c+len],char_count-c-len); 102 rb->memmove(&buffer[c+lennew],&buffer[c+len],char_count-c-len);
106 rb->strcpy(&buffer[c],str); 103 rb->strcpy(&buffer[c],str);
107 char_count += lennew-len; 104 char_count += lennew-len;
108 break; 105 break;
109 case ACTION_CONCAT: 106 case ACTION_CONCAT:
110 if (line > line_count) 107 if (line > line_count)
111 return 0; 108 return NULL;
112 rb->memmove(&buffer[c-1],&buffer[c],char_count-c); 109 rb->memmove(&buffer[c-1],&buffer[c],char_count-c);
113 char_count--; 110 char_count--;
114 line_count--; 111 line_count--;
115 break; 112 break;
116 default: 113 default:
117 return 0; 114 return NULL;
118 } 115 }
119 last_action_line = i; 116 last_action_line = i;
120 last_char_index = c; 117 last_char_index = c;
121 return 1; 118 return &buffer[c];
122} 119}
123static const char* list_get_name_cb(int selected_item, void* data, 120static const char* list_get_name_cb(int selected_item, void* data,
124 char* buf, size_t buf_len) 121 char* buf, size_t buf_len)
125{ 122{
126 (void)data; 123 (void)data;
127 char *b = &buffer[do_action(ACTION_GET,0,selected_item)]; 124 char *b = do_action(ACTION_GET, 0, selected_item);
128 /* strlcpy(dst, src, siz) returns strlen(src) */ 125 /* strlcpy(dst, src, siz) returns strlen(src) */
129 if (rb->strlcpy(buf, b, buf_len) >= buf_len) 126 if (rb->strlcpy(buf, b, buf_len) >= buf_len)
130 { 127 {
@@ -201,7 +198,7 @@ bool save_changes(int overwrite)
201#endif 198#endif
202 for (i=0;i<line_count;i++) 199 for (i=0;i<line_count;i++)
203 { 200 {
204 rb->fdprintf(fd,"%s%s",&buffer[do_action(ACTION_GET,0,i)],eol); 201 rb->fdprintf(fd,"%s%s", do_action(ACTION_GET, 0, i), eol);
205 } 202 }
206#ifdef HAVE_ADJUSTABLE_CPU_FREQ 203#ifdef HAVE_ADJUSTABLE_CPU_FREQ
207 rb->cpu_boost(0); 204 rb->cpu_boost(0);
@@ -238,12 +235,14 @@ int do_item_menu(int cur_sel, char* copy_buffer)
238 switch (rb->do_menu(&menu, NULL, NULL, false)) 235 switch (rb->do_menu(&menu, NULL, NULL, false))
239 { 236 {
240 case 0: /* cut */ 237 case 0: /* cut */
241 rb->strcpy(copy_buffer,&buffer[do_action(ACTION_GET,0,cur_sel)]); 238 rb->strlcpy(copy_buffer, do_action(ACTION_GET, 0, cur_sel),
242 do_action(ACTION_REMOVE,0,cur_sel); 239 MAX_LINE_LEN);
240 do_action(ACTION_REMOVE, 0, cur_sel);
243 ret = MENU_RET_UPDATE; 241 ret = MENU_RET_UPDATE;
244 break; 242 break;
245 case 1: /* copy */ 243 case 1: /* copy */
246 rb->strcpy(copy_buffer,&buffer[do_action(ACTION_GET,0,cur_sel)]); 244 rb->strlcpy(copy_buffer, do_action(ACTION_GET, 0, cur_sel),
245 MAX_LINE_LEN);
247 ret = MENU_RET_NO_UPDATE; 246 ret = MENU_RET_NO_UPDATE;
248 break; 247 break;
249 case 2: /* insert above */ 248 case 2: /* insert above */
@@ -346,12 +345,12 @@ enum plugin_status plugin_start(const void* parameter)
346#ifdef HAVE_LCD_COLOR 345#ifdef HAVE_LCD_COLOR
347 char *c = NULL; 346 char *c = NULL;
348#endif 347#endif
349 rb->strcpy(filename,(char*)parameter); 348 rb->strlcpy(filename, (char*)parameter, MAX_PATH);
350 get_eol_string(filename); 349 get_eol_string(filename);
351 fd = rb->open(filename,O_RDONLY); 350 fd = rb->open(filename,O_RDONLY);
352 if (fd<0) 351 if (fd<0)
353 { 352 {
354 rb->splashf(HZ*2,"Couldnt open file: %s",(char*)parameter); 353 rb->splashf(HZ*2, "Couldnt open file: %s", filename);
355 return PLUGIN_ERROR; 354 return PLUGIN_ERROR;
356 } 355 }
357#ifdef HAVE_LCD_COLOR 356#ifdef HAVE_LCD_COLOR
@@ -397,14 +396,15 @@ enum plugin_status plugin_start(const void* parameter)
397 case ACTION_STD_OK: 396 case ACTION_STD_OK:
398 { 397 {
399 if (line_count) 398 if (line_count)
400 rb->strcpy(temp_line,&buffer[do_action(ACTION_GET,0,cur_sel)]); 399 rb->strlcpy(temp_line, do_action(ACTION_GET, 0, cur_sel),
400 MAX_LINE_LEN);
401#ifdef HAVE_LCD_COLOR 401#ifdef HAVE_LCD_COLOR
402 if (edit_colors_file && line_count) 402 if (edit_colors_file && line_count)
403 { 403 {
404 char *name = temp_line, *value = NULL; 404 char *name = temp_line, *value = NULL;
405 char extension[MAX_LINE_LEN]; 405 char extension[MAX_LINE_LEN];
406 int color, old_color; 406 int color, old_color;
407 bool temp_changed; 407 bool temp_changed = false;
408 rb->settings_parseline(temp_line, &name, &value); 408 rb->settings_parseline(temp_line, &name, &value);
409 if (line_count) 409 if (line_count)
410 { 410 {
@@ -426,9 +426,6 @@ enum plugin_status plugin_start(const void* parameter)
426 rb->set_color(rb->screens[SCREEN_MAIN], name, &color, -1); 426 rb->set_color(rb->screens[SCREEN_MAIN], name, &color, -1);
427 temp_changed = (value == NULL) || (color != old_color); 427 temp_changed = (value == NULL) || (color != old_color);
428 break; 428 break;
429 default:
430 /* Should never happen but makes compiler happy */
431 temp_changed = false;
432 } 429 }
433 430
434 if (temp_changed) 431 if (temp_changed)
@@ -456,8 +453,9 @@ enum plugin_status plugin_start(const void* parameter)
456 break; 453 break;
457 case ACTION_STD_CONTEXT: 454 case ACTION_STD_CONTEXT:
458 if (!line_count) break; 455 if (!line_count) break;
459 rb->strcpy(copy_buffer,&buffer[do_action(ACTION_GET,0,cur_sel)]); 456 rb->strlcpy(copy_buffer, do_action(ACTION_GET, 0, cur_sel),
460 do_action(ACTION_REMOVE,0,cur_sel); 457 MAX_LINE_LEN);
458 do_action(ACTION_REMOVE, 0, cur_sel);
461 changed = true; 459 changed = true;
462 break; 460 break;
463 case ACTION_STD_MENU: 461 case ACTION_STD_MENU: