Skip to content

account for null-terminator character#275

Merged
mikaelarguedas merged 6 commits intomasterfrom
snprintf_warning
Jun 7, 2018
Merged

account for null-terminator character#275
mikaelarguedas merged 6 commits intomasterfrom
snprintf_warning

Conversation

@mikaelarguedas
Copy link
Copy Markdown
Member

@mikaelarguedas mikaelarguedas added the in progress Actively being worked on (Kanban column) label May 8, 2018
@mikaelarguedas mikaelarguedas added in review Waiting for review (Kanban column) and removed in progress Actively being worked on (Kanban column) labels May 8, 2018
@dirk-thomas
Copy link
Copy Markdown
Member

dirk-thomas commented May 8, 2018

The doc for snprintf says:

n - Maximum number of bytes to be used in the buffer.
The generated string has a length of at most n-1, leaving space for the additional terminating null character.

So I don't think this patch is correct.

@mikaelarguedas
Copy link
Copy Markdown
Member Author

If you have a suggestion on how to properly handle, I'll happily merge it into this branch.
I was looking for the minimal change to get this to compile without warning. I believe that this PR achieves that goal without making the behavior more wrong than before

@dirk-thomas
Copy link
Copy Markdown
Member

Since we do want n characters to be written (and not n-1) the patch is changing (and in my opinion breaking) the behavior.

If we do rely on truncating the data then the code should explicitly check the return value (in which case I would expect the warning to disappear).

@mikaelarguedas mikaelarguedas added in progress Actively being worked on (Kanban column) and removed in review Waiting for review (Kanban column) labels May 10, 2018
@mikaelarguedas
Copy link
Copy Markdown
Member Author

Thanks @dirk-thomas for the explanation. I modified the patch to actually write all characters provided by the user / test and not n - 1

@mikaelarguedas
Copy link
Copy Markdown
Member Author

Without this patch: Build Status (temporary job)

GNU C Compiler Warnings: 3 warnings.
3 new warnings


test_interfaces.cpp:285, GNU C Compiler 4 (gcc), Priority: Normal
‘__builtin___snprintf_chk’ output truncated before the last format character [-Wformat-truncation=]

test_interfaces.cpp:337, GNU C Compiler 4 (gcc), Priority: Normal
‘__builtin___snprintf_chk’ output truncated before the last format character [-Wformat-truncation=]

test_interfaces.cpp:579, GNU C Compiler 4 (gcc), Priority: Normal
‘__builtin___snprintf_chk’ output truncated before the last format character [-Wformat-truncation=]

With this patch: Build Status (temporary job)

GNU C Compiler Warnings: 0 warnings.
3 fixed warnings

@mikaelarguedas mikaelarguedas added in review Waiting for review (Kanban column) and removed in progress Actively being worked on (Kanban column) labels Jun 7, 2018
@mikaelarguedas mikaelarguedas changed the title account for null-terminator when padding account for null-terminator character Jun 7, 2018
@mikaelarguedas
Copy link
Copy Markdown
Member Author

And CI on our regular jobs/platforms to make sure this doesnt break anything:

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@mikaelarguedas mikaelarguedas self-assigned this Jun 7, 2018
size_t step_length = (maxlength - minlength) / size;
char * tmpstr = reinterpret_cast<char *>(malloc(maxlength));
std::snprintf(tmpstr, minlength, "%*d", minlength, min);
char * tmpstr = reinterpret_cast<char *>(malloc(maxlength + 1));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unrelated to the patch: this doesn't need a reinterpret_cast - a static_cast would be sufficient and "better" here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

👍 fee3265

@mikaelarguedas mikaelarguedas merged commit 92ffc94 into master Jun 7, 2018
@mikaelarguedas mikaelarguedas deleted the snprintf_warning branch June 7, 2018 19:35
@wjwwood wjwwood removed the in review Waiting for review (Kanban column) label Jun 7, 2018
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