LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-24-2019, 04:53 PM   #1
GazL
LQ Guru
 
Registered: May 2008
Posts: 5,156
Blog Entries: 18

Rep: Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849
segfault after return from main()?


Hey guys, little help please...

Prompted by babbab's post about wide input I knocked up a small test, with unexpected results...

Code:
$ cat testing.c 
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main()
{
    wchar_t ws[20] = { L'\0' };
    
    setlocale(LC_ALL, "");
    
    fgetws(ws, 9, stdin);

    return EXIT_SUCCESS;
}
$ cc -Wall -O0 -g testing.c -o testing
$ gdb testing
Reading symbols from testing...
(gdb) break main
Breakpoint 1 at 0x40112a: file testing.c, line 9.
(gdb) run
Starting program: /home/test/tmp/testing/testing 

Breakpoint 1, main () at testing.c:9
9           wchar_t ws[20] = { L'\0' };
(gdb) s
11          setlocale(LC_ALL, "");
(gdb) s
13          fgetws(ws, 9, stdin);
(gdb) s
1234567890123456
15          return EXIT_SUCCESS;
(gdb) info local
ws = L"12345678", '\000' <repeats 11 times>
(gdb) s
16      }
(gdb) s
0x00007ffff7ddcb5b in __libc_start_main () from /lib64/libc.so.6
(gdb) s
Single stepping until exit from function __libc_start_main,
which has no line number information.
(gdb) 
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7e3d625 in _IO_wfile_sync () from /lib64/libc.so.6
(gdb) bt
#0  0x00007ffff7e3d625 in _IO_wfile_sync () from /lib64/libc.so.6
#1  0x00007ffff7e45421 in _IO_default_setbuf () from /lib64/libc.so.6
#2  0x00007ffff7e41769 in __GI__IO_file_setbuf () from /lib64/libc.so.6
#3  0x00007ffff7e45dc9 in _IO_cleanup () from /lib64/libc.so.6
#4  0x00007ffff7dfc39a in __run_exit_handlers () from /lib64/libc.so.6
#5  0x00007ffff7dfc3da in exit () from /lib64/libc.so.6
#6  0x00007ffff7ddcb62 in __libc_start_main () from /lib64/libc.so.6
#7  0x000000000040107a in _start () at ../sysdeps/x86_64/start.S:120
As far as I can see, though I'm entering extra information, the buffer is not overflowing as the second parameter of fgetws() prevents that and I'm not even entering a full buffers worth anyway.

This is slackware64-current, gcc-9.2.0, glibc-2.29. LANG=en_GB.utf8.

Am I doing something dumb here, or have I stumbled upon a breakage?
 
Old 08-24-2019, 05:01 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,010
Blog Entries: 13

Rep: Reputation: 3451Reputation: 3451Reputation: 3451Reputation: 3451Reputation: 3451Reputation: 3451Reputation: 3451Reputation: 3451Reputation: 3451Reputation: 3451Reputation: 3451
fgetws() needs input from a stream.

Code:
wchar_t* fgetws (wchar_t* ws, int num, FILE* stream);
How are you passing information from stdin to the function?

Last edited by rtmistler; 08-24-2019 at 05:06 PM.
 
Old 08-24-2019, 05:03 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 8,010
Blog Entries: 13

Rep: Reputation: 3451Reputation: 3451Reputation: 3451Reputation: 3451Reputation: 3451Reputation: 3451Reputation: 3451Reputation: 3451Reputation: 3451Reputation: 3451Reputation: 3451
I also have no idea what the setlocale() call is doing for you. I'd comment that line out.

I see the other thread, but I don't get the relevance of that function here.

Last edited by rtmistler; 08-24-2019 at 05:05 PM.
 
Old 08-24-2019, 06:22 PM   #4
GazL
LQ Guru
 
Registered: May 2008
Posts: 5,156

Original Poster
Blog Entries: 18

Rep: Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849Reputation: 2849
The program was originally longer, I just cut out the irrelevant stuff to isolate the issue. Input was passed on stdin from the terminal.

Removing setlocale() does avoid the segfault, so having locale enabled is clearly a contributing factor, but that's not a solution, it's just avoiding the problem.

Seems like you get a segfault if you leave anything buffered on the stream when you exit. I suspect it's a library bug that only happens when you setlocale().

This works around it:
Code:
    fgetws(ws, 9, stdin);
    if ( (rc = wcsrchr( ws, L'\n')) == NULL )
        while ( fgetwc(stdin) != L'\n') ; /* discard to eol */
    else
        *rc = L'\0';
a fclose(stdin); before the return also seems to workaround the bug.

Not that I'd normally use fgetws() anyway.

Update: found a 3 year old bug report that sounds like the same issue:
https://sourceware.org/bugzilla/show_bug.cgi?id=20938

Last edited by GazL; 08-24-2019 at 07:18 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: int main() vs void main() LXer Syndicated Linux News 0 01-09-2009 02:11 PM
Exception in thread "main" java.lang.ClassFormatError: onmyown.Main (unrecognized cla zimboney Fedora 4 07-20-2007 01:00 PM
./main to main DDD26 Linux - Newbie 3 07-25-2005 03:09 PM
(2) Main Menu's? Isn't it, "there can be only one?" for the term "Main"? t3gah LQ Suggestions & Feedback 1 04-09-2005 09:30 AM
Gnome - main gnome menu segfault xconspirisist Linux - Software 0 03-08-2004 02:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration