[vim/vim] Fix that setvbuf causes assertion error (PR #12467)

25 views
Skip to first unread message

K.Takata

unread,
May 30, 2023, 2:00:35 AM5/30/23
to vim/vim, Subscribed

When Vim is compiled with debug mode on MSVC, the following command causes an assertion error:

> vimd -e -s -c q
Debug Assertion Failed!

Program: C:\work\vim-msvc\src\vim32d.dll
File: minkernel\crts\ucrt\src\appcrt\stdio\setvbuf.cpp
Line: 55

Expression: 2 <= buffer_size_in_bytes && buffer_size_in_bytes <= INT_MAX

This is because MSVC's setvbuf() doesn't accept 0 as the 4th parameter. https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setvbuf?view=msvc-170

Additionally, it seems that passing NULL to the 2nd parameter is no-op in some C libraries.


You can view, comment on, or merge this pull request online at:

  https://github.com/vim/vim/pull/12467

Commit Summary

  • dea07cf Fix that setvbuf causes assertion error

File Changes

(1 file)

Patch Links:


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/12467@github.com>

Christ van Willegen

unread,
May 30, 2023, 2:03:44 AM5/30/23
to vim...@googlegroups.com, reply+ACY5DGF4OF6UKF4KF4...@reply.github.com
Hi, 

Op di 30 mei 2023 08:00 schreef K.Takata <vim-dev...@256bit.org>:

When Vim is compiled with debug mode on MSVC, the following command causes an assertion error:


This leaks memory, though, because of the malloc() call...

Christ van Willegen

vim-dev ML

unread,
May 30, 2023, 2:03:57 AM5/30/23
to vim/vim, vim-dev ML, Your activity

Hi,

Op di 30 mei 2023 08:00 schreef K.Takata ***@***.***>:


> When Vim is compiled with debug mode on MSVC, the following command causes
> an assertion error:
>

This leaks memory, though, because of the malloc() call...

Christ van Willegen

>


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/12467/c1567800419@github.com>

codecov[bot]

unread,
May 30, 2023, 2:10:55 AM5/30/23
to vim/vim, vim-dev ML, Comment

Codecov Report

Merging #12467 (dea07cf) into master (c9fbd25) will increase coverage by 0.65%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master   #12467      +/-   ##
==========================================
+ Coverage   82.06%   82.72%   +0.65%     
==========================================
  Files         160      150      -10     
  Lines      193588   180400   -13188     
  Branches    43472    40548    -2924     
==========================================
- Hits       158877   149242    -9635     
+ Misses      21853    18200    -3653     
- Partials    12858    12958     +100     
Flag Coverage Δ
huge-clang-none 82.72% <100.00%> (+<0.01%) ⬆️
linux 82.72% <100.00%> (+<0.01%) ⬆️
mingw-x64-HUGE ?
mingw-x86-HUGE ?
windows ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/main.c 84.72% <100.00%> (+0.75%) ⬆️

... and 143 files with indirect coverage changes


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.Message ID: <vim/vim/pull/12467/c1567809190@github.com>

Bram Moolenaar

unread,
May 30, 2023, 5:50:50 PM5/30/23
to vim/vim, vim-dev ML, Comment

Making the buffer a global variable should avoid a warning for leaked memory.


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/pull/12467/c1569154162@github.com>

K.Takata

unread,
May 30, 2023, 11:06:22 PM5/30/23
to vim/vim, vim-dev ML, Push

@k-takata pushed 1 commit.

  • 03fb2d2 Free the buffer when EXITFREE is defined


View it on GitHub or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/12467/push/13815402473@github.com>

K.Takata

unread,
May 30, 2023, 11:15:40 PM5/30/23
to vim/vim, vim-dev ML, Comment

Updated to free the buffer when EXITFREE is defined.


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/pull/12467/c1569435575@github.com>

K.Takata

unread,
May 30, 2023, 11:27:32 PM5/30/23
to vim/vim, vim-dev ML, Push

@k-takata pushed 1 commit.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/12467/push/13815530720@github.com>

K.Takata

unread,
May 31, 2023, 12:03:45 AM5/31/23
to vim/vim, vim-dev ML, Push

@k-takata pushed 1 commit.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/12467/push/13815742194@github.com>

Christian Brabandt

unread,
May 31, 2023, 3:28:23 AM5/31/23
to vim/vim, vim-dev ML, Comment

@chrisbra commented on this pull request.


In src/main.c:

> @@ -436,6 +442,19 @@ main
 #endif // NO_VIM_MAIN
 #endif // PROTO
 
+#if defined(EXITFREE) || defined(PROTO)
+    void
+free_vbuf(void)
+{
+    if (s_vbuf != NULL)
+    {
+	setvbuf(stdout, NULL, _IONBF, 0);

setvbuf(stdout, NULL, _IONBF, 0);

Is this call required? Because now you are introducing the same problem that this PR is fixing? Shouldn't size be larger than 1?


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/pull/12467/review/1452394673@github.com>

K.Takata

unread,
May 31, 2023, 4:09:15 AM5/31/23
to vim/vim, vim-dev ML, Comment

@k-takata commented on this pull request.


In src/main.c:

> @@ -436,6 +442,19 @@ main
 #endif // NO_VIM_MAIN
 #endif // PROTO
 
+#if defined(EXITFREE) || defined(PROTO)
+    void
+free_vbuf(void)
+{
+    if (s_vbuf != NULL)
+    {
+	setvbuf(stdout, NULL, _IONBF, 0);

Before freeing s_vbuf we should stop using it in setvbuf().
So I use _IONBF (not _IOLBF) here to stop buffering.


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/pull/12467/review/1452466591@github.com>

Christian Brabandt

unread,
May 31, 2023, 4:13:36 AM5/31/23
to vim/vim, vim-dev ML, Comment

@chrisbra commented on this pull request.


In src/main.c:

> @@ -436,6 +442,19 @@ main
 #endif // NO_VIM_MAIN
 #endif // PROTO
 
+#if defined(EXITFREE) || defined(PROTO)
+    void
+free_vbuf(void)
+{
+    if (s_vbuf != NULL)
+    {
+	setvbuf(stdout, NULL, _IONBF, 0);

okay, but what about the size then? I think 0 is not valid according to your link.


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/pull/12467/review/1452474887@github.com>

K.Takata

unread,
May 31, 2023, 4:24:27 AM5/31/23
to vim/vim, vim-dev ML, Comment

@k-takata commented on this pull request.


In src/main.c:

> @@ -436,6 +442,19 @@ main
 #endif // NO_VIM_MAIN
 #endif // PROTO
 
+#if defined(EXITFREE) || defined(PROTO)
+    void
+free_vbuf(void)
+{
+    if (s_vbuf != NULL)
+    {
+	setvbuf(stdout, NULL, _IONBF, 0);

The assertion error relates only to _IOFBF and _IOLBF, not to _IONBF.

_IONBF No buffer is used, regardless of buffer or size.


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/pull/12467/review/1452495049@github.com>

Christian Brabandt

unread,
May 31, 2023, 4:28:08 AM5/31/23
to vim/vim, vim-dev ML, Comment

@chrisbra commented on this pull request.


In src/main.c:

> @@ -436,6 +442,19 @@ main
 #endif // NO_VIM_MAIN
 #endif // PROTO
 
+#if defined(EXITFREE) || defined(PROTO)
+    void
+free_vbuf(void)
+{
+    if (s_vbuf != NULL)
+    {
+	setvbuf(stdout, NULL, _IONBF, 0);

ah, thanks. Makes sense


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/pull/12467/review/1452502885@github.com>

K.Takata

unread,
May 31, 2023, 4:28:50 AM5/31/23
to vim/vim, vim-dev ML, Comment

@k-takata commented on this pull request.


In src/main.c:

> @@ -436,6 +442,19 @@ main
 #endif // NO_VIM_MAIN
 #endif // PROTO
 
+#if defined(EXITFREE) || defined(PROTO)
+    void
+free_vbuf(void)
+{
+    if (s_vbuf != NULL)
+    {
+	setvbuf(stdout, NULL, _IONBF, 0);

If you have installed MSVC, you should be able to find the source code of setvbuf() here:
C:\Program Files (x86)\Windows Kits\10\Source\<BuildNumber>\ucrt\stdio\setvbuf.cpp


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/pull/12467/review/1452504164@github.com>

Bram Moolenaar

unread,
May 31, 2023, 7:48:21 AM5/31/23
to vim/vim, vim-dev ML, Comment

Closed #12467 via 3c240f6.


Reply to this email directly, view it on GitHub.

You are receiving this because you commented.Message ID: <vim/vim/pull/12467/issue_event/9389277170@github.com>

Reply all
Reply to author
Forward
0 new messages