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

Partitions() on macOS may return uninitialized elements #1390

Closed
brianryner8 opened this issue Dec 7, 2022 · 3 comments · Fixed by #1392
Closed

Partitions() on macOS may return uninitialized elements #1390

brianryner8 opened this issue Dec 7, 2022 · 3 comments · Fixed by #1392

Comments

@brianryner8
Copy link
Contributor

Describe the bug
When calling disk.Partitions() on macOS (observed only on 10.14, so far), there may be uninitialized elements returned at the end of the PartitionStat array.

This happens because we don't truncate the fs array to the number of items returned by the second call to unix.Getfsstat(). This count may be less than the count returned by the first call to getfsstat.

To Reproduce

partitions, err := disk.Partitions(false)
// iterate over partitions
// last partition in the list has Device, Mountpoint, and Fstype as all unprintable characters ���

Expected behavior
All elements in partitions contain valid data.

Environment (please complete the following information):

  • [X ] Mac OS: [paste the result of sw_vers and uname -a
    ProductName: Mac OS X
    ProductVersion: 10.14.6
    BuildVersion: 18G103

Darwin (hostname redacted) 18.7.0 Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2~2/RELEASE_X86_64 x86_64

Additional context

@shirou
Copy link
Owner

shirou commented Dec 17, 2022

I can not reproduce by using

ProductName: macOS
ProductVersion: 11.7
BuildVersion: 20G817
Kernel Version: 20.6.0 x86_64

and also FreeBSD 12.3. Is this problem happening only with macOSX 10.14? Or have you checked with other versions as well ?

@brianryner8
Copy link
Contributor Author

In our project's continuous build, we saw this issue only on 10.14 (I believe that is the oldest version we tested). It did not occur on newer releases.

Referring to the manpage for getfsstat() ( https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/getfsstat.2.html ), it's says that "Upon successful completion, the number of statfs structures is returned."

I suspect that newer macOS releases changed the implementation so the current behavior happens to work, but the code should be truncating the items to the returned count (as I did in #1392 ) so that it's not depending on undefined behavior.

Thanks,

@shirou
Copy link
Owner

shirou commented Dec 18, 2022

Hmm. We don't have any reports from others, but I would like to merge at least this change seems to have no negative impact. Could you add some comments like this?

// Only 10.14, actual count may be less than from the first call.
// https://github.com/shirou/gopsutil/issues/1390
fs = fs[:count] 

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 a pull request may close this issue.

2 participants