]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.1346: missing out-of-memory check in textformat.c v9.1.1346
authorJohn Marriott <basilisk@internode.on.net>
Fri, 25 Apr 2025 17:14:38 +0000 (19:14 +0200)
committerChristian Brabandt <cb@256bit.org>
Fri, 25 Apr 2025 17:14:38 +0000 (19:14 +0200)
Problem:  missing out-of-memory check in textformat.c
Solution: add out-of-memory check, add small optimizations to
          internal_format() and same_leader() (John Marriott)

closes: #17200

Signed-off-by: John Marriott <basilisk@internode.on.net>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/textformat.c
src/version.c

index 018565f72417c7b6b922ea4e4b091359bc16ed51..1af4b2f4d11e13a5779ad2051ba30af54007f45c 100644 (file)
@@ -369,7 +369,7 @@ internal_format(
        {
            // In MODE_VREPLACE state, we will backspace over the text to be
            // wrapped, so save a copy now to put on the next line.
-           saved_text = vim_strsave(ml_get_cursor());
+           saved_text = vim_strnsave(ml_get_cursor(), ml_get_cursor_len());
            curwin->w_cursor.col = orig_col;
            if (saved_text == NULL)
                break;  // Can't do it, out of memory
@@ -552,9 +552,7 @@ same_leader(
     char_u  *leader2_flags)
 {
     int            idx1 = 0, idx2 = 0;
-    char_u  *p;
     char_u  *line1;
-    char_u  *line2;
 
     if (leader1_len == 0)
        return (leader2_len == 0);
@@ -566,6 +564,8 @@ same_leader(
     // some text after it and the second line has the 'm' flag.
     if (leader1_flags != NULL)
     {
+       char_u  *p;
+
        for (p = leader1_flags; *p && *p != ':'; ++p)
        {
            if (*p == COM_FIRST)
@@ -589,9 +589,11 @@ same_leader(
 
     // Get current line and next line, compare the leaders.
     // The first line has to be saved, only one line can be locked at a time.
-    line1 = vim_strsave(ml_get(lnum));
+    line1 = vim_strnsave(ml_get(lnum), ml_get_len(lnum));
     if (line1 != NULL)
     {
+       char_u  *line2;
+
        for (idx1 = 0; VIM_ISWHITE(line1[idx1]); ++idx1)
            ;
        line2 = ml_get(lnum + 1);
@@ -665,11 +667,8 @@ auto_format(
     int                prev_line)      // may start in previous line
 {
     pos_T      pos;
-    colnr_T    len;
     char_u     *old;
-    char_u     *new, *pnew;
     int                wasatend;
-    int                cc;
 
     if (!has_format_option(FO_AUTO))
        return;
@@ -688,6 +687,8 @@ auto_format(
     wasatend = (pos.col == ml_get_curline_len());
     if (*old != NUL && !trailblank && wasatend)
     {
+       int cc;
+
        dec_cursor();
        cc = gchar_cursor();
        if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
@@ -740,11 +741,13 @@ auto_format(
     // formatted.
     if (!wasatend && has_format_option(FO_WHITE_PAR))
     {
-       new = ml_get_curline();
-       len = ml_get_curline_len();
+       char_u  *new = ml_get_curline();
+       colnr_T len = ml_get_curline_len();
        if (curwin->w_cursor.col == len)
        {
-           pnew = vim_strnsave(new, len + 2);
+           char_u *pnew = vim_strnsave(new, len + 2);
+           if (pnew == NULL)
+               return;
            pnew[len] = ' ';
            pnew[len + 1] = NUL;
            ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
index 4f7996a9915f9ec601c38b411320decfef16cb77..c4db34af8fec3c0e3e85c0edff1de5fa9ec4911d 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1346,
 /**/
     1345,
 /**/