-
Notifications
You must be signed in to change notification settings - Fork 33
Fix: Limit scope of macro redefinitions in _ctype.h #366
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -35,11 +35,11 @@ | |||||||||||||||||||||||||||
#define _STDDEF_H_ | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
#include <sys/cdefs.h> | ||||||||||||||||||||||||||||
#include <sys/_null.h> | ||||||||||||||||||||||||||||
#include <sys/_types.h> | ||||||||||||||||||||||||||||
#include <sys/types.h> | ||||||||||||||||||||||||||||
#include <runetype.h> /* For __rune_t */ | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
#ifndef _PTRDIFF_T_DECLARED | ||||||||||||||||||||||||||||
typedef __ptrdiff_t ptrdiff_t; | ||||||||||||||||||||||||||||
typedef __PTRDIFF_TYPE__ ptrdiff_t; | ||||||||||||||||||||||||||||
#define _PTRDIFF_T_DECLARED | ||||||||||||||||||||||||||||
#endif | ||||||||||||||||||||||||||||
Comment on lines
41
to
44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Using PTRDIFF_TYPE is compiler-specific Add a guard or fallback for compilers that do not support PTRDIFF_TYPE, such as using __ptrdiff_t as an alternative.
Suggested change
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
@@ -51,13 +51,13 @@ typedef __rune_t rune_t; | |||||||||||||||||||||||||||
#endif | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
#ifndef _SIZE_T_DECLARED | ||||||||||||||||||||||||||||
typedef __size_t size_t; | ||||||||||||||||||||||||||||
typedef __SIZE_TYPE__ size_t; | ||||||||||||||||||||||||||||
#define _SIZE_T_DECLARED | ||||||||||||||||||||||||||||
#endif | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
#ifndef __cplusplus | ||||||||||||||||||||||||||||
#ifndef _WCHAR_T_DECLARED | ||||||||||||||||||||||||||||
typedef ___wchar_t wchar_t; | ||||||||||||||||||||||||||||
typedef __WCHAR_TYPE__ wchar_t; | ||||||||||||||||||||||||||||
#define _WCHAR_T_DECLARED | ||||||||||||||||||||||||||||
#endif | ||||||||||||||||||||||||||||
#endif | ||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include <stdio.h> | ||
|
||
// Simulate the relevant parts of _ctype.h | ||
// Test Case 1: _EXTERNALIZE_CTYPE_INLINES_ is defined | ||
|
||
#define _EXTERNALIZE_CTYPE_INLINES_ | ||
|
||
#ifdef _EXTERNALIZE_CTYPE_INLINES_ | ||
#define static_test_A | ||
#define __inline_test_A | ||
// This simulates a function definition that would use the macros | ||
static_test_A __inline_test_A int externalized_func_A() { return 1; } | ||
#undef static_test_A | ||
#undef __inline_test_A | ||
#endif | ||
|
||
// After the block, static and __inline should be normal keywords | ||
static int my_static_func_A() { | ||
return 10; | ||
} | ||
|
||
static __inline int my_inline_func_A() { | ||
return 20; | ||
} | ||
|
||
// Test Case 2: _EXTERNALIZE_CTYPE_INLINES_ is NOT defined | ||
// (to ensure our original static __inline functions in the header are not affected | ||
// by any hypothetical future changes that might accidentally undef them too broadly) | ||
|
||
#undef _EXTERNALIZE_CTYPE_INLINES_ // Ensure it's not defined for this part | ||
|
||
// These would be the normal static __inline functions in _ctype.h | ||
// We are just testing if the keywords 'static' and '__inline' work as expected. | ||
static __inline int normal_inline_func_B() { | ||
return 2; | ||
} | ||
|
||
int main() { | ||
int rA1 = externalized_func_A(); // Should compile to `int externalized_func_A()` | ||
int rA2 = my_static_func_A(); | ||
int rA3 = my_inline_func_A(); // Standard inline or regular function | ||
int rB1 = normal_inline_func_B(); | ||
|
||
if (rA1 != 1) { | ||
printf("Test Failed: externalized_func_A wrong value.\n"); | ||
return 1; | ||
} | ||
if (rA2 != 10) { | ||
printf("Test Failed: my_static_func_A wrong value.\n"); | ||
return 1; | ||
} | ||
// rA3 (my_inline_func_A) and rB1 (normal_inline_func_B) are harder to test for "inlineness" | ||
// without inspecting assembly, but compilation success is key. | ||
Comment on lines
+52
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (testing): Assert return values for Please add assertions for |
||
|
||
printf("Macro Verification Test Passed!\n"); | ||
return 0; // Success | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include <ctype.h> // This should include _ctype.h | ||
#include <stdio.h> | ||
|
||
// Test user-defined static function | ||
static int my_static_func() { | ||
return 10; | ||
} | ||
|
||
// Test user-defined inline function | ||
// Using __inline as that's what _ctype.h uses internally mostly | ||
__inline int my_inline_func() { | ||
return 20; | ||
} | ||
|
||
// Ensure __inline is still usable if the compiler supports it, | ||
// or at least doesn't conflict. | ||
// Some compilers might need 'static __inline' for non-exported inline functions. | ||
static __inline int my_static_inline_func() { | ||
return 30; | ||
} | ||
|
||
int main() { | ||
int result = 0; | ||
result += my_static_func(); | ||
result += my_inline_func(); // May need to be static __inline for some compilers if not optimized out | ||
result += my_static_inline_func(); | ||
|
||
// Call a function from ctype.h to ensure it's still working | ||
if (isalpha('a')) { | ||
result += 1; | ||
} else { | ||
result -= 100; // Should not happen | ||
} | ||
|
||
if (isspace(' ')) { | ||
result += 1; | ||
} else { | ||
result -= 100; // Should not happen | ||
} | ||
|
||
// Expected: 10 (my_static_func) + 20 (my_inline_func) + 30 (my_static_inline_func) + 1 (isalpha) + 1 (isspace) = 62 | ||
printf("Result: %d\n", result); | ||
if (result == 62) { | ||
printf("Verification Test Passed (normal build)!\n"); | ||
return 0; // Success | ||
} else { | ||
printf("Verification Test Failed (normal build)! Expected 62, Got %d\n", result); | ||
return 1; // Failure | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (bug_risk): Guard feature macro with defined() check
Use '#if defined(__BSD_VISIBLE) && __BSD_VISIBLE' to avoid preprocessor errors if __BSD_VISIBLE is not defined.