Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

segfault when given logfile name is already taken by a directory #853

Closed
honggyukim opened this issue Sep 1, 2019 · 6 comments · Fixed by #861
Closed

segfault when given logfile name is already taken by a directory #853

honggyukim opened this issue Sep 1, 2019 · 6 comments · Fixed by #861

Comments

@honggyukim
Copy link
Collaborator

honggyukim commented Sep 1, 2019

Here is an example recording a simple program.

$ gcc -pg tests/s-abc.c -o t-abc

$ uftrace record t-abc

$ uftrace replay
# DURATION     TID     FUNCTION
   1.700 us [ 16855] | __monstartup();
   1.300 us [ 16855] | __cxa_atexit();
            [ 16855] | main() {
            [ 16855] |   a() {
            [ 16855] |     b() {
            [ 16855] |       c() {
   1.400 us [ 16855] |         getpid();
   3.600 us [ 16855] |       } /* c */
   5.600 us [ 16855] |     } /* b */
   7.300 us [ 16855] |   } /* a */
   9.200 us [ 16855] | } /* main */

The above example works fine and the debug message can be stored with --logfile option as follows:

$ uftrace record -v --logfile=dir t-abc

However, it gets crashed if the given file name is already used by another directory as follows:

$ mkdir dir

$ uftrace record -v --logfile=dir t-abc
Segmentation fault (core dumped)

It needs to check if the file is already exists or not.

@honggyukim
Copy link
Collaborator Author

The crash point is in uftrace.c file as follows:

1042int main(int argc, char *argv[])
1043│ {
        ...
1080if (dbg_domain_set && !debug)
1081debug = 1;
10821083if (opts.logfile) {
1084logfp = fopen(opts.logfile, "a");
1085if (logfp == NULL)
1086├───────────────────────> pr_err("cannot open log file");
10871088setvbuf(logfp, NULL, _IOLBF, 1024);
1089│         }
1090else if (debug) {
1091/* ensure normal output is not mixed by debug message */
1092setvbuf(outfp, NULL, _IOLBF, 1024);
1093│         }
        ...

@honggyukim
Copy link
Collaborator Author

This is a simple bug so anyone can fix this if interested.

@rls1004
Copy link
Contributor

rls1004 commented Sep 7, 2019

Can i take this issue?

@honggyukim
Copy link
Collaborator Author

Sure, please take it. I would like to provide more info regarding this.

Please refer to the following discussion.

I think we better have some protection code for this.

@rls1004
Copy link
Contributor

rls1004 commented Sep 8, 2019

The problem seems to be caused by writing an error message to logfd when logfd is NULL.
Before calling pr_err(), I want to set logfd to stderr.
Is this okay?

@honggyukim
Copy link
Collaborator Author

I'm okay with it. Thanks!

rls1004 added a commit to rls1004/uftrace that referenced this issue Sep 9, 2019
If logfd is NULL, set logfd to stderr before calling pr_err().

Fixed: namhyung#853
rls1004 added a commit to rls1004/uftrace that referenced this issue Sep 9, 2019
…irectory

This patch is to fix segfault in --logfile.
If logfd is NULL, set logfd to stderr before calling pr_err().

Fixed: namhyung#853
rls1004 added a commit to rls1004/uftrace that referenced this issue Sep 9, 2019
This patch is to fix segfault in --logfile.
If logfd is NULL, set logfd to stderr before calling pr_err().

Fixed: namhyung#853

Signed-off-by: MinJeong Kim <[email protected]>
namhyung pushed a commit that referenced this issue Sep 9, 2019
This patch is to fix segfault in --logfile.
If logfd is NULL, set logfd to stderr before calling pr_err().

Fixed: #853

Signed-off-by: MinJeong Kim <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants