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

Uninitialized scalar variable with CID 147694-93-92 #5252

Merged
merged 1 commit into from
Oct 13, 2016

Conversation

heary-cao
Copy link
Contributor

issues:
fix coverity defects
coverity scan CID:147694, Type:Uninitialized scalar variable
coverity scan CID:147693, Type:Uninitialized scalar variable
coverity scan CID:147692, Type:Uninitialized scalar variable

Signed-off-by: cao.xuewen [email protected]

@mention-bot
Copy link

@heary-cao, thanks for your PR! By analyzing the history of the files in this pull request, we identified @behlendorf, @legend-hua and @ironMann to be potential reviewers.

@@ -64,7 +64,7 @@ main(int argc, char **argv)
offset_t llseek_ret = 0;
int write_ret = 0;
int err = 0;
char mybuf[5];
char mybuf[5] = {0};
Copy link
Contributor

@behlendorf behlendorf Oct 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this byte gets appended to a file I'd suggest initializing it with a pattern like "abcd\0". This will make it easier to spot check with a text editor if we ever need too. It's also a little weird this array is bigger than it needs to be but let's just leave that as is. It's no doing any harm.

@@ -67,7 +67,7 @@ int
main(int argc, char **argv)
{
int fd;
char buf[BUFSIZ];
char buf[BUFSIZ] = {0};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using BUFSIZ here probably isn't a great idea, even if that is what the existing code does. How about we just make this 1024 and initialize it with memset() with a fixed character pattern. This should be fine since the intent in to overlap the IO with the 8192 mmaps region.

        char buf[1024];

        memset(buf, 'a', sizeof (buf));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@behlendorf thank you for suggest.
please review again.

@@ -89,10 +90,12 @@ main(int argc, char *argv[])
}

buf = (char *)malloc(filesize);
memset(buf, 0x0, filesize);
Copy link
Contributor

@behlendorf behlendorf Oct 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally I'd suggest using calloc() here since you'll get an optimized version which is already zero filled. But instead I think writing a known pattern in better in this case. How about memset(buf, 'a', filesize).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@behlendorf thank you for suggest.
please review again.

@heary-cao heary-cao force-pushed the Uninitialized branch 2 times, most recently from 7d3cbe2 to 05432de Compare October 11, 2016 03:27
err = errno;
goto out;
free(testfile);
return errno;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't quite right. errno may be changed by the call the free(). In fact, errno may be changed by perror and it should be saved before it's called, see perror(3). Also there's a cstyle issue regarding return, it should be return (errno). This first block should be changed to:

        err = errno;
        perror(...);
        free(testfile);
        return (err);

And the subsequent goto blocks should be:

        err = errno;
        perror(...);
        goto out;

@@ -88,11 +89,13 @@ main(int argc, char *argv[])
return (1);
}

buf = (char *)malloc(filesize);
buf = (char *)calloc(1, filesize);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There needs to be a NULL check here since calloc may fail.

issues:
fix coverity defects
coverity scan CID:147694, Type:Uninitialized scalar variable
coverity scan CID:147693, Type:Uninitialized scalar variable
coverity scan CID:147692, Type:Uninitialized scalar variable

Signed-off-by: cao.xuewen <[email protected]>
@behlendorf behlendorf merged commit a85a905 into openzfs:master Oct 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants