Skip to content

NULL compares equal to 0 #4526

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

Merged
merged 1 commit into from
Apr 13, 2019

Conversation

tautschnig
Copy link
Collaborator

When comparing constants, we just compared their string representations.
This fails when one side of the (in)equality was NULL while the other
side was 0, which is wrong when config.ansi_c.NULL_is_zero is set. Thus
actually test both sides for being zero in this configuration.

  • Each commit message has a non-empty body, explaining why the change was made.
  • n/a Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • n/a The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • n/a My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • n/a White-space or formatting changes outside the feature-related changed lines are in commits of their own.

Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

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

✔️
Passed Diffblue compatibility checks (cbmc commit: 15bf786).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/108192432

@kroening
Copy link
Member

If config.ansi_c.NULL_is_zero is false, can we really conclude that a non-zero pointer is NOT equal to NULL?

@kroening
Copy link
Member

(Very academic question; I am yet to spot a system in which NULL isn't a zero bit pattern).

@tautschnig
Copy link
Collaborator Author

See http://c-faq.com/null/machexamp.html for the discussion of NULL-is-not-zero. But, yes, this remains academic.

@tautschnig
Copy link
Collaborator Author

If config.ansi_c.NULL_is_zero is false, can we really conclude that a non-zero pointer is NOT equal to NULL?

Hmm, indeed, we should probably just refrain from simplifying in that case. Let me fix this.

@tautschnig tautschnig force-pushed the fix-pointer-comparison branch from 15bf786 to 7562a99 Compare April 13, 2019 09:42
@tautschnig
Copy link
Collaborator Author

If config.ansi_c.NULL_is_zero is false, can we really conclude that a non-zero pointer is NOT equal to NULL?

Hmm, indeed, we should probably just refrain from simplifying in that case. Let me fix this.

Done.

@tautschnig
Copy link
Collaborator Author

As a side note: a still-current scenario is that the address 0 is a valid place to store data, which is true in about any freestanding setting. There are probably several places where we don't currently account for this in CBMC...

Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

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

✔️
Passed Diffblue compatibility checks (cbmc commit: 7562a99).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/108214328

Copy link
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

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

Add a test exercising the !null_is_zero path?

int main()
{
union {
unsigned long long i;
Copy link
Contributor

Choose a reason for hiding this comment

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

AFAIK unsigned long is the type guaranteed to have same size as a pointer

Copy link
Collaborator Author

@tautschnig tautschnig Apr 13, 2019

Choose a reason for hiding this comment

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

Not on Windows/LLP64... (and also it would be ok for the integer type to be larger than the pointer type in this union, but not the other way around)

Copy link
Contributor

Choose a reason for hiding this comment

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

Gah, so it isn't, Though perhaps intptr_t is better?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

@tautschnig tautschnig force-pushed the fix-pointer-comparison branch from 7562a99 to 0201b0a Compare April 13, 2019 14:20
@tautschnig
Copy link
Collaborator Author

Add a test exercising the !null_is_zero path?

Test added!

When comparing constants, we just compared their string representations.
This fails when one side of the (in)equality was NULL while the other
side was 0, which is wrong when config.ansi_c.NULL_is_zero is set. Thus
actually test both sides for being zero in this configuration.
@tautschnig tautschnig force-pushed the fix-pointer-comparison branch from 0201b0a to 8f68057 Compare April 13, 2019 14:37
@tautschnig tautschnig merged commit c7fa8e7 into diffblue:develop Apr 13, 2019
@tautschnig tautschnig deleted the fix-pointer-comparison branch April 13, 2019 15:24
Copy link
Contributor

@allredj allredj left a comment

Choose a reason for hiding this comment

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

✔️
Passed Diffblue compatibility checks (cbmc commit: 8f68057).
Build URL: https://travis-ci.com/diffblue/test-gen/builds/108223887

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants