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 issues mixing 'const'-ness of STRING_PTR_RO #6659

Merged
merged 2 commits into from
Dec 13, 2024
Merged

Conversation

MichaelChirico
Copy link
Member

Closes #6656

Thanks for looking into this @aitap! I think the PR here is the best option.

  • for chmatch.c, xd is not const because of this:
    SET_TRUELENGTH(xd[i],0); // w falls on ending 0 marker: dups used up; any more dups should return nomatch
  • for coalesce.c, I think just switching to the _RO accessors for the other SEXPTYPEs is correct -- valP is only ever read from (and its value coerced before writing to val).

@MichaelChirico MichaelChirico requested a review from aitap December 12, 2024 02:10
Copy link

codecov bot commented Dec 12, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.61%. Comparing base (a599557) to head (24cbd00).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #6659   +/-   ##
=======================================
  Coverage   98.61%   98.61%           
=======================================
  Files          79       79           
  Lines       14559    14559           
=======================================
  Hits        14358    14358           
  Misses        201      201           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

github-actions bot commented Dec 12, 2024

Comparison Plot

Generated via commit 24cbd00

Download link for the artifact containing the test results: ↓ atime-results.zip

Task Duration
R setup and installing dependencies 4 minutes and 42 seconds
Installing different package versions 7 minutes and 33 seconds
Running and plotting the test cases 2 minutes and 15 seconds

@aitap
Copy link
Contributor

aitap commented Dec 12, 2024

Counter-intuitively, a SEXP being const doesn't preclude modification of the underlying value. This is because SEXP is an alias for a pointer type: const SEXP * xd is equivalent to struct SEXPREC * const * xd, so while the pointer xd[i] is const, the contents, *(xd[i]), are not.

We're not allowed to *(xd[i]) because we're no longer told what struct SEXPREC is, but SET_TRUELENGTH getting a copy of a const SEXP pointer as the function argument and modifying the pointed-to buffer doesn't break any rules.

@MichaelChirico
Copy link
Member Author

Thanks for the explanation (which I'm still processing)!

I had noticed that the compiler doesn't care if we used const SEXP *xd, however I decided to take the current approach based on the "spirit" of const, namely it's telling the reader (as well as the compiler) 'this is "read-only"' (even though in practice the actual thing that is read-only is somewhat complex).

IMO understanding why const is technically allowed here requires too much reasoning about internal/implementation details of SEXP, and I am not seeing much downside to using (SEXP *) instead of making xd const.

@aitap
Copy link
Contributor

aitap commented Dec 12, 2024

That's reasonable. Besides, the memory is writeable anyway, so the cast will not introduce undefined behaviour.

A better explanation may be that the garbage collector cares about the pointed-to object remaining at the same address but not about its contents changing (we can't guarantee the latter at the C level).

@MichaelChirico MichaelChirico merged commit a9cd0bf into master Dec 13, 2024
11 checks passed
@MichaelChirico MichaelChirico deleted the const-warn branch December 13, 2024 02:44
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.

New compiler warnings about modifying 'const' object (STRING_PTR_RO)
2 participants