Skip to content

account for null-terminator character#269

Merged
mikaelarguedas merged 2 commits intomasterfrom
snprintf_warning
May 8, 2018
Merged

account for null-terminator character#269
mikaelarguedas merged 2 commits intomasterfrom
snprintf_warning

Conversation

@mikaelarguedas
Copy link
Copy Markdown
Member

This fixes a gcc warning as the string to store can be up to 4 numeric characters + the null-terminator so the container should be of size 5.

related to ros2/ros2#481

@dirk-thomas
Copy link
Copy Markdown
Member

Based on the data type size_t why 5? If because of the currently used values I would argue that we should size the char array to for any value valid for the data type.

@mikaelarguedas
Copy link
Copy Markdown
Member Author

Because we know the value of size. I agree that it's fragile though.

I'm not aware of a macro returning the maximum length of a string representation of a type, (but if there's one I'm more than happy to use it!).
Otherwise, I can assume that for the foreseeable future we will have systems that have at most 64bits to represent size_t and use 21 for the array:

In [0]: len(str(2**64))
Out[0]: 20
# + null-terminator

Does that match your suggestion?

@dirk-thomas
Copy link
Copy Markdown
Member

That sounds better to me 👍

@mikaelarguedas mikaelarguedas merged commit 84c035d into master May 8, 2018
@mikaelarguedas mikaelarguedas deleted the snprintf_warning branch May 8, 2018 16:34
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