Segmentation fault when logging to clipboard #112

Closed
opened 2015-01-13 22:07:35 +00:00 by xythobuz · 10 comments
xythobuz commented 2015-01-13 22:07:35 +00:00 (Migrated from github.com)

I'm trying to log my Console output (a bunch of TextUnformatted) to the clipboard. Unfortunately, I get a segmentation fault in vsnprintf at imgui.cpp:587 (referring to imgui v1.20).

I've replaced my console output with "Test". It still segfaults.

Stepping up some frames in the debugger, I can see the faulting call starts out as TextUnformatted, going to RenderText at imgui.cpp:3294, then LogText at imgui.cpp:1912, to LogText on imgui.cpp:1858, to appendv at imgui.cpp:1815, to ImFormatStringV at imgui.cpp:1194.

Unfortunately I can't find the source of the problem.

I'm trying to log my Console output (a bunch of TextUnformatted) to the clipboard. Unfortunately, I get a segmentation fault in vsnprintf at imgui.cpp:587 (referring to imgui v1.20). I've replaced my console output with "Test". It still segfaults. Stepping up some frames in the debugger, I can see the faulting call starts out as TextUnformatted, going to RenderText at imgui.cpp:3294, then LogText at imgui.cpp:1912, to LogText on imgui.cpp:1858, to appendv at imgui.cpp:1815, to ImFormatStringV at imgui.cpp:1194. Unfortunately I can't find the source of the problem.
ocornut commented 2015-01-13 22:29:43 +00:00 (Migrated from github.com)

Could you provide a repro or at least paste in the code you are using?
I did a few tests and couldn't reproduce the issue.

Could you provide a repro or at least paste in the code you are using? I did a few tests and couldn't reproduce the issue.
xythobuz commented 2015-01-13 22:33:19 +00:00 (Migrated from github.com)

Of course, the complete Console code can be found here. Logging is enabled in Line 105, the TextUnformatted is in Line 128, and logging ends on Line 132. It works with LogToFile and LogToTTY, but not LogToClipboard.

Of course, the complete Console code [can be found here](https://github.com/xythobuz/OpenRaider/blob/loader/src/Console.cpp#L105). Logging is enabled in Line 105, the TextUnformatted is in Line 128, and logging ends on Line 132. It works with LogToFile and LogToTTY, but not LogToClipboard.
ocornut commented 2015-01-13 22:51:46 +00:00 (Migrated from github.com)

Hmm, well assuming all your "entry.text.c_str()" values are non-null and the string zero-terminated the user code sounds right. I can't find an issue with appendv() but maybe vsnprintf() doesn't behave as I expected on some systems with a NULL parameter and 0 size as input (I am measuring it to measure the output buffer requirement). Which system/compiler are you using?

In appendv() could you retrieve the local variables from a debugger and dump them here?
len, write_off, Buf.size, Buf.capacity, and fmt.

Hmm, well assuming all your "entry.text.c_str()" values are non-null and the string zero-terminated the user code sounds right. I can't find an issue with appendv() but maybe vsnprintf() doesn't behave as I expected on some systems with a NULL parameter and 0 size as input (I am measuring it to measure the output buffer requirement). Which system/compiler are you using? In appendv() could you retrieve the local variables from a debugger and dump them here? len, write_off, Buf.size, Buf.capacity, and fmt.
xythobuz commented 2015-01-13 23:02:00 +00:00 (Migrated from github.com)

I'm on Mac OS X 10.10.1, my compiler identifies as Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn).

I've just added two checks, so I can confirm all the strings are neither NULL nor zero length.

And here are the dumped values:

(const char *) fmt = 0x00000001001613ff "\n%*s%.*s"
(int) len = 5
(size_t) write_off = 1

(lldb) expr -- Buf.size()
(size_t) $0 = 6

(lldb) expr -- Buf.capacity()
(size_t) $1 = 8

To be honest, I would not be surprised if this is some strange bug in vsnprintf on Macs... :)

I'm on Mac OS X 10.10.1, my compiler identifies as `Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)`. I've just added two checks, so I can confirm all the strings are neither NULL nor zero length. And here are the dumped values: ``` (const char *) fmt = 0x00000001001613ff "\n%*s%.*s" (int) len = 5 (size_t) write_off = 1 (lldb) expr -- Buf.size() (size_t) $0 = 6 (lldb) expr -- Buf.capacity() (size_t) $1 = 8 ``` To be honest, I would not be surprised if this is some strange bug in vsnprintf on Macs... :)
ocornut commented 2015-01-13 23:08:31 +00:00 (Migrated from github.com)

What are the parameter passed to
ImGui::LogText(STR_NEWLINE "%_s%._s", tree_depth*4, "", char_count, text_remaining);
tree_depth, char_count, text_remaining ?
Thanks again!

What are the parameter passed to ImGui::LogText(STR_NEWLINE "%_s%._s", tree_depth*4, "", char_count, text_remaining); tree_depth, char_count, text_remaining ? Thanks again!
xythobuz commented 2015-01-13 23:16:11 +00:00 (Migrated from github.com)

The length changed - I think I was still printing only "Test" in the first dump, now it's the regular console text. So here it is:

(int) len = 46
(size_t) write_off = 1

(lldb) expr -- Buf.size()
(size_t) $0 = 47

(lldb) expr -- Buf.capacity()
(size_t) $1 = 47

And the arguments to LogText:

(const int) tree_depth = 0
(const int) char_count = 45
(const char *) text_remaining = 0x00000001005193e0 "Initializing OpenRaider-0.1.3-09b15dd (Debug)"

And no problem. I really hope it does not turn out to be a problem in my code, so I just wasted your time... 😄

The length changed - I think I was still printing only "Test" in the first dump, now it's the regular console text. So here it is: ``` (int) len = 46 (size_t) write_off = 1 (lldb) expr -- Buf.size() (size_t) $0 = 47 (lldb) expr -- Buf.capacity() (size_t) $1 = 47 ``` And the arguments to LogText: ``` (const int) tree_depth = 0 (const int) char_count = 45 (const char *) text_remaining = 0x00000001005193e0 "Initializing OpenRaider-0.1.3-09b15dd (Debug)" ``` And no problem. I really hope it does not turn out to be a problem in my code, so I just wasted your time... :smile:
ocornut commented 2015-01-13 23:36:29 +00:00 (Migrated from github.com)

E-mailing you :)

E-mailing you :)
ocornut commented 2015-01-13 23:41:17 +00:00 (Migrated from github.com)
Probably that http://www.bailopan.net/blog/?p=30
xythobuz commented 2015-01-13 23:49:02 +00:00 (Migrated from github.com)

That's it! Quick platform specific fix from ocornut for anyone else affected:

 void ImGuiTextBuffer::appendv(const char* fmt, va_list args)
 {
+    va_list args_copy;
+    va_copy(args_copy, args);

...

-    ImFormatStringV(&Buf[write_off] - 1, (size_t)len+1, fmt, args);
+    ImFormatStringV(&Buf[write_off] - 1, (size_t)len+1, fmt, args_copy);
 }
That's it! Quick platform specific fix from ocornut for anyone else affected: ``` void ImGuiTextBuffer::appendv(const char* fmt, va_list args) { + va_list args_copy; + va_copy(args_copy, args); ... - ImFormatStringV(&Buf[write_off] - 1, (size_t)len+1, fmt, args); + ImFormatStringV(&Buf[write_off] - 1, (size_t)len+1, fmt, args_copy); } ```
ocornut commented 2015-01-14 13:05:38 +00:00 (Migrated from github.com)

Unfortunately va_copy doesn't work with Visual Studio pre 2013 and god-knows-what compiler.
I am assuming that va_copy is macro to define it as a simple assignment if macro doesn't exist.
That will break if on some system va_copy exists and isn't a macro and isn't a simple assignment. Most code on the internet seems to assume that va_copy is always a macro if it exists.

Unfortunately va_copy doesn't work with Visual Studio pre 2013 and god-knows-what compiler. I am assuming that va_copy is macro to define it as a simple assignment if macro doesn't exist. That will break if on some system va_copy exists and isn't a macro and isn't a simple assignment. Most code on the internet seems to assume that va_copy is always a macro if it exists.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#112
No description provided.