pp.h: Use STATIC_ASSERT_EXPR on SETs#24587
Open
khwilliamson wants to merge 1 commit into
Open
Conversation
This makes sure that the argument whose pointer is being passed has the correct size to be settable by the called element.
tonycoz
reviewed
Jul 22, 2026
| #define mXPUSHu(u) STMT_START { EXTEND(sp,1); mPUSHu(u); } STMT_END | ||
|
|
||
| #define SETs(s) (*sp = s) | ||
| #define SETs(s) (STATIC_ASSERT_EXPR(sizeof(*sp) == sizeof(s)), *sp = s) |
Contributor
There was a problem hiding this comment.
When wouldn't this be caught by the compiler?
This could only be a problem when called from non-XS code (ie. no dSP explicit or implicit), since *SP is a SV*, s can only be a pointer (always the same size as SV* on anything we use) or an integer constant that evaluates to zero, which is valid but rare (same as NULL).
Non-SV pointers (which this change doesn't catch) give you:
<source>:11:5: warning: incompatible pointer types assigning to 'SV *' (aka 'struct sv *') from 'int *' [-Wincompatible-pointer-types]
11 | SETs(&x);
| ^ ~~
/opt/compiler-explorer/perl-5.42.2/lib/5.42.2/x86_64-linux-thread-multi/CORE/pp.h:583:23: note: expanded from macro 'SETs'
583 | #define SETs(s) (*sp = s)
| ^ ~
1
Non pointers/non-integer:
<source>:11:5: error: assigning to 'SV *' (aka 'struct sv *') from incompatible type 'double'
11 | SETs(0.0);
| ^ ~~~
/opt/compiler-explorer/perl-5.42.2/lib/5.42.2/x86_64-linux-thread-multi/CORE/pp.h:583:23: note: expanded from macro 'SETs'
583 | #define SETs(s) (*sp = s)
| ^ ~
clang complains about this valid but strange value (same as SETs(0)):
<source>:12:10: warning: expression which evaluates to zero treated as a null pointer constant of type 'SV *' (aka 'struct sv *') [-Wnon-literal-null-conversion]
12 | SETs('\0');
| ^~~~
/opt/compiler-explorer/perl-5.42.2/lib/5.42.2/x86_64-linux-thread-multi/CORE/pp.h:583:25: note: expanded from macro 'SETs'
583 | #define SETs(s) (*sp = s)
| ^
(clang++/g++ report an error, since '\0' is a char in C++ and an int in C)
Tested here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This makes sure that the argument whose pointer is being passed has the correct size to be settable by the called element.