]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.2015: blob2string() stopped after an empty line v9.1.2015
authorFoxe Chen <chen.foxe@gmail.com>
Tue, 23 Dec 2025 20:37:19 +0000 (20:37 +0000)
committerChristian Brabandt <cb@256bit.org>
Tue, 23 Dec 2025 20:37:19 +0000 (20:37 +0000)
Problem:  blob2string() stopped after an empty line
Solution: Specifically check for empty content (Foxe Chen)

closes: #19001

Signed-off-by: Foxe Chen <chen.foxe@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/strings.c
src/testdir/test_blob.vim
src/version.c

index 228cff242cc60d9c446ad09a44f6a8f4a2df99b4..b548783022fbf1dfaf90d3f77f1df60765179389 100644 (file)
@@ -1234,6 +1234,8 @@ blob_from_string(char_u *str, blob_T *blob)
  * Return a string created from the bytes in blob starting at "start_idx".
  * A NL character in the blob indicates end of string.
  * A NUL character in the blob is translated to a NL.
+ * If a newline is followed by another newline (empty line), then an empty
+ * allocated string is returned and "start_idx" is moved forward by one byte.
  * On return, "start_idx" points to next byte to process in blob.
  */
     static char_u *
@@ -1265,6 +1267,8 @@ string_from_blob(blob_T *blob, long *start_idx)
 
     if (str_ga.ga_data != NULL)
        ret_str = vim_strnsave(str_ga.ga_data, str_ga.ga_len);
+    else
+       ret_str = vim_strsave((char_u *)"");
     *start_idx = idx;
 
     ga_clear(&str_ga);
index a70cdcf61d48dc454280964ace3eccc68085404f..1ce227d5cf01e933f98ae6152030952834c52a7c 100644 (file)
@@ -885,4 +885,17 @@ func Test_blob_byte_set_invalid_value()
   call v9.CheckSourceLegacyAndVim9Failure(lines, 'E1239: Invalid value for blob:')
 endfunc
 
+" Test when converting a blob to a string, and there is an empty line (newline
+" followed directly by another newline).
+func Test_blob2str_empty_line()
+  let stuff =<< trim END
+  Hello
+
+  World!
+  END
+
+  let b = str2blob(stuff)
+  call assert_equal(['Hello', '', 'World!'], blob2str(b))
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab
index 77c2fd181b76b1cd14c6aea3c8e644807077745e..75714149c3174b7388b8c7348b60a83ea78a4f17 100644 (file)
@@ -734,6 +734,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2015,
 /**/
     2014,
 /**/