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

Fix improper null-byte termination handling #2875

Closed
wants to merge 1 commit into from

Conversation

nedbass
Copy link
Contributor

@nedbass nedbass commented Nov 7, 2014

Fix a few cases where null-byte termination of strings was done
unnecessarily or incorrectly.

  • The snprintf() function always produces a null-byte terminated string
    for non-negative return values, so it is not necessary to write out a
    null-byte as a separate step.
  • Also, it is unsafe to use the return value of snprintf() as an offset
    for placing a null-byte, because if the output was truncated the return
    value is the number of bytes that would have been written had enough
    space been available. Therefore the return value may index beyond the
    array boundaries.
  • Finally, snprintf() accounts for the null-byte when limiting its output
    size, so there is no need to pass it a size parameter that is one less
    than the buffer size.

Signed-off-by: Ned Bass bass6@llnl.gov

@behlendorf
Copy link
Contributor

Nice catch!

Fix a few cases where null-byte termination of strings was done
unnecessarily or incorrectly.

- The snprintf() function always produces a null-byte terminated string
  for non-negative return values, so it is not necessary to write out a
  null-byte as a separate step.

- Also, it is unsafe to use the return value of snprintf() as an offset
  for placing a null-byte, because if the output was truncated the return
  value is the number of bytes that _would_ have been written had enough
  space been available. Therefore the return value may index beyond the
  array boundaries.

- Finally, snprintf() accounts for the null-byte when limiting its output
  size, so there is no need to pass it a size parameter that is one less
  than the buffer size.

Signed-off-by: Ned Bass <bass6@llnl.gov>
@behlendorf
Copy link
Contributor

Merged as:

7b2d78a Fix improper null-byte termination handling

ryao pushed a commit to ryao/zfs that referenced this pull request Nov 29, 2014
Fix a few cases where null-byte termination of strings was done
unnecessarily or incorrectly.

- The snprintf() function always produces a null-byte terminated string
  for non-negative return values, so it is not necessary to write out a
  null-byte as a separate step.

- Also, it is unsafe to use the return value of snprintf() as an offset
  for placing a null-byte, because if the output was truncated the return
  value is the number of bytes that _would_ have been written had enough
  space been available. Therefore the return value may index beyond the
  array boundaries.

- Finally, snprintf() accounts for the null-byte when limiting its output
  size, so there is no need to pass it a size parameter that is one less
  than the buffer size.

Signed-off-by: Ned Bass <bass6@llnl.gov>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes openzfs#2875
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.

2 participants