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

Fixed various type punning warnings by using memcpy() #172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

seanm
Copy link
Contributor

@seanm seanm commented Apr 1, 2023

2acc444 was buggy, and undone with cedc42d. This essentially restores 2acc444 without the buggy pointer arithmetic.

Modern compilers optimize memcpy() very well, this will likely produce the same code.

@seanm
Copy link
Contributor Author

seanm commented Apr 1, 2023

@afni-rickr @hjmjohnson this is a fixed version of PR #166 which was buggy and undone by PR #170.

Don't merge as-is, I added some asserts to verify the address calculation is correct. They are just there as a test and will be removed.

Alas, no test actually enters this code, as I confirmed with the assert(0).

@@ -212,6 +212,7 @@ static int g_debug = 1;
#include <limits.h>
#include <string.h>
#include <stdint.h>
#include <assert.h> // TEMP
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this //TEP be removed before committing?

char * offsetp = (char *)basep + field->offset;
offsetp = &(offsetp[fc * sizeof(sval)]);
assert(offsetp == &(((short *)((char *)basep + field->offset))[fc])); // TEMP
assert(0);
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this assert(0) be removed?

Copy link
Contributor

Choose a reason for hiding this comment

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

Are the other assert()s going to stay? If so, that means some compiler warns about the original statements but not the asserts?
If the asserts will be kept, they should probably have casts to (char *)before the '&'.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, going from a pointer to an array and back seems a little odd. What if 3900 were to read:
offsetp += fc*sizeof(sval); /* offset further by fc elements */

Or that could be merged into the previous line, though it is nice to spell out the pieces:

    /* from the struct base offset by the field location, access element #fc */
    char * offsetp = (char *)basep + field->offset + fc*sizeof(sval);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please see my comments way above, from 2023-04-01, repeated here:

"Don't merge as-is, I added some asserts to verify the address calculation is correct. They are just there as a test and will be removed.

Alas, no test actually enters this code, as I confirmed with the assert(0)."

It would be good if we first added some tests that exercise this code. Then we can confirm the code change is correct by not hitting those asserts.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay, that was on me to do. Note that I was planning to add only a single test, not for every type. This probably won't happen today...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A single test would be great! Once you have a chance to do it, I'll run this code and check that those asserts don't fire, then clean up this PR for merging.

2acc444 was buggy, and undone with cedc42d.  This essentially restores 2acc444 without the buggy pointer arithmetic.

Modern compilers optimize memcpy() very well, this will likely produce the same code.
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