Skip to content

Comments

Correct buffer_bytes > INT_MAX on BSD/amd64.#712

Merged
discordianfish merged 6 commits intoprometheus:masterfrom
derekmarcotte:dm-bufspace-long
Oct 25, 2017
Merged

Correct buffer_bytes > INT_MAX on BSD/amd64.#712
discordianfish merged 6 commits intoprometheus:masterfrom
derekmarcotte:dm-bufspace-long

Conversation

@derekmarcotte
Copy link
Contributor

The sysctl vfs.bufspace returns either an int or a long, depending on
the value. Large values of vfs.bufspace will result in error messages
like:

couldn't get meminfo: cannot allocate memory

This will detect the returned data type, and cast appropriately.

This is an updated fix to the one provided in #710.

I suspect that there'll be more re-factoring here, if other uses show up.

The sysctl vfs.bufspace returns either an int or a long, depending on
the value.  Large values of vfs.bufspace will result in error messages
like:

  couldn't get meminfo: cannot allocate memory

This will detect the returned data type, and cast appropriately.
)
}

if len(raw) < (C.sizeof_long) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd feel a little better if we checked explicitly to ensure it matches the size of an integer, and then threw an error otherwise.

Copy link
Contributor Author

@derekmarcotte derekmarcotte Oct 24, 2017

Choose a reason for hiding this comment

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

Thanks, I feel better about this too. I've had short in the back of my head the whole time. And maybe this thing becomes bsdSysctlTypeSignedInteger, if it's needed.

// Not sure this is valid for all CLongs. It is at
// least for vfs.bufspace:
// https://github.com/freebsd/freebsd/blob/releng/10.3/sys/kern/vfs_bio.c#L338
tmpf64 = float64(*(*C.int)(unsafe.Pointer(&raw[0])))
Copy link
Contributor

Choose a reason for hiding this comment

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

As it stands now, this will be overwritten by line 134.

// least for vfs.bufspace:
// https://github.com/freebsd/freebsd/blob/releng/10.3/sys/kern/vfs_bio.c#L338
tmpf64 = float64(*(*C.int)(unsafe.Pointer(&raw[0])))
break
Copy link
Contributor

Choose a reason for hiding this comment

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

Unneeded.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The break at 131 is what stops 134 for overwriting - at least that's my intention.

Copy link
Contributor

Choose a reason for hiding this comment

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

Apologies, may have misread the diff.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Still valid. ;)

Copy link
Contributor Author

@derekmarcotte derekmarcotte left a comment

Choose a reason for hiding this comment

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

What about something like this? Seems a little easier to figure out.

Copy link
Contributor

@mdlayher mdlayher left a comment

Choose a reason for hiding this comment

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

LGTM after the error message fix. Also left a Go nit about using an early return instead of nested if blocks.

return 0, fmt.Errorf(
"length of bytes received from sysctl (%d) does not match expected bytes (%d)",
len(raw),
C.sizeof_long,
Copy link
Contributor

Choose a reason for hiding this comment

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

C.sizeof_int?

return 0, err
}

if len(raw) != (C.sizeof_long) {
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO invert this and move the return from line 152 up to reduce a level of indentation.

Copy link
Contributor

@mdlayher mdlayher left a comment

Choose a reason for hiding this comment

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

LGTM! Sorry for the brief back-and-forth.

Good cleanup work too. Will leave for someone else more familiar with BSD for +2.

@derekmarcotte
Copy link
Contributor Author

Not at all. Happy for the feedback!

@discordianfish
Copy link
Member

Nice, thanks! LGTM

@discordianfish discordianfish merged commit 0eecaa9 into prometheus:master Oct 25, 2017
@derekmarcotte derekmarcotte deleted the dm-bufspace-long branch November 2, 2017 10:03
SuperQ pushed a commit that referenced this pull request Nov 7, 2017
* Correct buffer_bytes > INT_MAX on BSD/amd64.

The sysctl vfs.bufspace returns either an int or a long, depending on
the value.  Large values of vfs.bufspace will result in error messages
like:

  couldn't get meminfo: cannot allocate memory

This will detect the returned data type, and cast appropriately.

* Added explicit length checks per feedback.

* Flatten Value() to make it easier to read.

* Simplify per feedback.

* Fix style.

* Doc updates.
@SuperQ SuperQ mentioned this pull request Nov 7, 2017
SuperQ added a commit that referenced this pull request Nov 8, 2017
* netstat: return nothing when /proc/net/snmp6 not found

* Fix off by one in Linux interrupts collector (#721)

* Fix off by one in Linux interrupts collector

* Fix off by one in CPU column handler.
* Add test.

* Enable interrupts in end-to-end test.

* Add and use sysReadFile in hwmon collector (#728)

* xfs: expose correct fields, fix metric names

* Correct buffer_bytes > INT_MAX on BSD/amd64. (#712)

* Correct buffer_bytes > INT_MAX on BSD/amd64.

The sysctl vfs.bufspace returns either an int or a long, depending on
the value.  Large values of vfs.bufspace will result in error messages
like:

  couldn't get meminfo: cannot allocate memory

This will detect the returned data type, and cast appropriately.

* Added explicit length checks per feedback.

* Flatten Value() to make it easier to read.

* Simplify per feedback.

* Fix style.

* Doc updates.

* Release v0.15.1

* [BUGFIX] netstat: return nothing when /proc/net/snmp6 not found #718
* [BUGFIX] Fix off by one in Linux interrupts collector #721
* [BUGFIX] Add and use sysReadFile in hwmon collector #728
oblitorum pushed a commit to shatteredsilicon/node_exporter that referenced this pull request Apr 9, 2024
* Correct buffer_bytes > INT_MAX on BSD/amd64.

The sysctl vfs.bufspace returns either an int or a long, depending on
the value.  Large values of vfs.bufspace will result in error messages
like:

  couldn't get meminfo: cannot allocate memory

This will detect the returned data type, and cast appropriately.

* Added explicit length checks per feedback.

* Flatten Value() to make it easier to read.

* Simplify per feedback.

* Fix style.

* Doc updates.
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