Revision: 861 http://svn.sourceforge.net/geany/?rev=861&view=rev Author: ntrel Date: 2006-10-02 08:22:29 -0700 (Mon, 02 Oct 2006)
Log Message: ----------- Change sci_get_line to return a NULL terminated string. Fix memory leak and possible invalid read in sci_cb_auto_latex().
Modified Paths: -------------- trunk/ChangeLog trunk/src/callbacks.c trunk/src/document.c trunk/src/sci_cb.c trunk/src/sciwrappers.c trunk/src/sciwrappers.h
Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2006-10-02 11:31:54 UTC (rev 860) +++ trunk/ChangeLog 2006-10-02 15:22:29 UTC (rev 861) @@ -6,6 +6,10 @@ src/makefile.win32, src/Makefile.am, po/POTFILES.in: Move global tags code to symbols.c. Added symbols_global_tags_loaded(), symbols_get_global_keywords(). + * src/callbacks.c, src/sci_cb.c, src/sciwrappers.c, src/sciwrappers.h, + src/document.c: + Change sci_get_line to return a NULL terminated string. + Fix memory leak and possible invalid read in sci_cb_auto_latex().
2006-10-01 Enrico Tröger enrico.troeger@uvena.de
Modified: trunk/src/callbacks.c =================================================================== --- trunk/src/callbacks.c 2006-10-02 11:31:54 UTC (rev 860) +++ trunk/src/callbacks.c 2006-10-02 15:22:29 UTC (rev 861) @@ -1296,8 +1296,7 @@ if (pos == -1) break;
line = sci_get_line_from_position(doc_list[i].sci, pos); - buffer = g_malloc0(sci_get_line_length(doc_list[i].sci, line) + 1); - sci_get_line(doc_list[i].sci, line, buffer); + buffer = sci_get_line(doc_list[i].sci, line);
if (doc_list[i].file_name == NULL) short_file_name = g_strdup(GEANY_STRING_UNTITLED);
Modified: trunk/src/document.c =================================================================== --- trunk/src/document.c 2006-10-02 11:31:54 UTC (rev 860) +++ trunk/src/document.c 2006-10-02 15:22:29 UTC (rev 861) @@ -1335,13 +1335,10 @@ sci_get_line_length(doc_list[idx].sci, line) == utils_get_eol_char_len(idx)) return;
+ // don't set the indicator on whitespace len = end - start; - linebuf = g_malloc(len); + linebuf = sci_get_line(doc_list[idx].sci, line);
- // don't set the indicator on whitespace - sci_get_line(doc_list[idx].sci, line, linebuf); - if (linebuf == NULL) return; - while (isspace(linebuf[i])) i++; while (isspace(linebuf[len-1])) len--; g_free(linebuf);
Modified: trunk/src/sci_cb.c =================================================================== --- trunk/src/sci_cb.c 2006-10-02 11:31:54 UTC (rev 860) +++ trunk/src/sci_cb.c 2006-10-02 15:22:29 UTC (rev 861) @@ -330,9 +330,7 @@
if (! use_this_line) prev_line--; len = sci_get_line_length(sci, prev_line); - linebuf = g_malloc(len + 1); - sci_get_line(sci, prev_line, linebuf); - linebuf[len] = '\0'; + linebuf = sci_get_line(sci, prev_line);
for (i = 0; i < len; i++) { @@ -438,8 +436,7 @@ utils_get_eol_char_len(document_find_by_sci(sci));
// check that the line is empty, to not kill text in the line - line_buf = g_malloc(line_len + 1); - sci_get_line(sci, line, line_buf); + line_buf = sci_get_line(sci, line); line_buf[line_len - eol_char_len] = '\0'; while (x < (line_len - eol_char_len)) { @@ -469,11 +466,10 @@ gint line_start = sci_get_position_from_line(sci, line); gint startword = pos - line_start; gint endword = pos - line_start; - gchar *chunk = g_malloc(sci_get_line_length(sci, line) + 1); + gchar *chunk;
word[0] = '\0'; - sci_get_line(sci, line, chunk); - chunk[sci_get_line_length(sci, line)] = '\0'; + chunk = sci_get_line(sci, line);
while (startword > 0 && strchr(GEANY_WORDCHARS, chunk[startword - 1])) startword--; @@ -664,8 +660,7 @@ else if (lexer == SCLEX_RUBY && style == SCE_MAKE_COMMENT) return FALSE;
- linebuf = g_malloc(line_len + 1); - sci_get_line(sci, line, linebuf); + linebuf = sci_get_line(sci, line);
// find the start of the current word while ((startword > 0) && (strchr(GEANY_WORDCHARS, linebuf[startword - 1]))) @@ -706,8 +701,7 @@ gint i, start;
// get the line - buf = g_malloc0(line_len + 1); - sci_get_line(sci, line, buf); + buf = sci_get_line(sci, line);
// get to the first non-blank char (some kind of ltrim()) start = 0; @@ -722,7 +716,7 @@
// take also "\begingroup" (or whatever there can be) and append "\endgroup" and so on. i = start + 6; - while (buf[i] != '{' && j < (sizeof(full_cmd) - 1)) + while (i < line_len && buf[i] != '{' && j < (sizeof(full_cmd) - 1)) { // copy all between "\begin" and "{" to full_cmd full_cmd[j] = buf[i]; i++; @@ -757,13 +751,11 @@ SSM(sci, SCI_INSERTTEXT, pos, (sptr_t) construct); sci_goto_pos(sci, pos + 1, TRUE); g_free(construct); - g_free(buf); g_free(eol); } - else - { // later there could be some else ifs for other keywords - return; - } + // later there could be some else ifs for other keywords + + g_free(buf); } }
@@ -1107,9 +1099,7 @@ // check whether the line is empty and can be deleted line = sci_get_line_from_position(doc_list[idx].sci, pos); len = sci_get_line_length(doc_list[idx].sci, line); - linebuf = g_malloc(len + 1); - sci_get_line(doc_list[idx].sci, line, linebuf); - linebuf[len] = '\0'; + linebuf = sci_get_line(doc_list[idx].sci, line); x = 0; while (linebuf[x] != '\0' && isspace(linebuf[x])) x++; if (x == len) SSM(doc_list[idx].sci, SCI_LINEDELETE, 0, 0); @@ -1122,9 +1112,7 @@ // check whether the line is empty and can be deleted line = sci_get_line_from_position(doc_list[idx].sci, pos); len = sci_get_line_length(doc_list[idx].sci, line); - linebuf = g_malloc(len + 1); - sci_get_line(doc_list[idx].sci, line, linebuf); - linebuf[len] = '\0'; + linebuf = sci_get_line(doc_list[idx].sci, line); x = 0; while (linebuf[x] != '\0' && isspace(linebuf[x])) x++; if (x == len) SSM(doc_list[idx].sci, SCI_LINEDELETE, 0, 0);
Modified: trunk/src/sciwrappers.c =================================================================== --- trunk/src/sciwrappers.c 2006-10-02 11:31:54 UTC (rev 860) +++ trunk/src/sciwrappers.c 2006-10-02 15:22:29 UTC (rev 861) @@ -469,11 +469,18 @@ }
-void sci_get_line(ScintillaObject* sci, gint line, gchar* text) +// Returns: a NULL-terminated copy of the line text +gchar *sci_get_line(ScintillaObject* sci, gint line_num) { - SSM(sci,SCI_GETLINE, line, (sptr_t) text); + gint len = sci_get_line_length(sci, line_num); + gchar *linebuf = g_malloc(len + 1); + + SSM(sci, SCI_GETLINE, line_num, (sptr_t) linebuf); + linebuf[len] = '\0'; + return linebuf; }
+ // the last char will be null terminated void sci_get_text(ScintillaObject* sci, gint len, gchar* text) {
Modified: trunk/src/sciwrappers.h =================================================================== --- trunk/src/sciwrappers.h 2006-10-02 11:31:54 UTC (rev 860) +++ trunk/src/sciwrappers.h 2006-10-02 15:22:29 UTC (rev 861) @@ -81,7 +81,7 @@ void sci_get_text (ScintillaObject* sci,gint len,gchar* text); void sci_get_selected_text (ScintillaObject* sci, gchar* text); gint sci_get_selected_text_length(ScintillaObject* sci); -void sci_get_line (ScintillaObject* sci, gint line, gchar* text); +gchar * sci_get_line (ScintillaObject* sci, gint line_num); gint sci_get_line_length (ScintillaObject* sci, gint line); gint sci_get_line_count ( ScintillaObject* sci ); void sci_get_xy_from_position (ScintillaObject* sci,gint pos, gint* x, gint* y);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.