Skip to content

Commit 4bdd592

Browse files
authored
[runtime] Adopt safer native compiler flags: Wshadow (#24088)
Example warning: ``` macios/runtime/trampolines-invoke.m:212:15: error: declaration shadows a local variable [-Werror,-Wshadow] 212 | const char *type = xamarin_skip_encoding_flags (argType); | ^ ``` References: * https://developer.apple.com/documentation/xcode/enabling-enhanced-security-for-your-app * https://releases.llvm.org/8.0.0/tools/clang/docs/ReleaseNotes.html#major-new-features (for -ftrivial-auto-var-init=zero) Contributes towards #23023.
1 parent bcf30d3 commit 4bdd592

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

mk/rules.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ CFLAGS=\
4242
-Wcast-function-type-mismatch \
4343
-Wsemicolon-before-method-body \
4444
-Wsign-compare \
45+
-Wshadow \
4546
-g \
4647
-I.
4748
SWIFTFLAGS=-g -emit-library

runtime/monovm-bridge.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@
267267
bool
268268
xamarin_is_class_nsstring (MonoClass *cls)
269269
{
270-
MonoClass *nsstring_class = xamarin_get_nsstring_class ();
271270
if (nsstring_class == NULL)
272271
return false;
273272

runtime/runtime.m

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,9 +2176,11 @@ -(struct NSObjectData*) xamarinGetNSObjectData;
21762176
MarshalManagedExceptionMode mode;
21772177
GCHandle exception_gchandle = INVALID_GCHANDLE;
21782178

2179-
GCHandle handle = xamarin_gchandle_new (exception, false);
2180-
mode = xamarin_on_marshal_managed_exception (handle, &exception_gchandle);
2181-
xamarin_gchandle_free (handle);
2179+
{
2180+
GCHandle handle = xamarin_gchandle_new (exception, false);
2181+
mode = xamarin_on_marshal_managed_exception (handle, &exception_gchandle);
2182+
xamarin_gchandle_free (handle);
2183+
}
21822184

21832185
if (exception_gchandle != INVALID_GCHANDLE) {
21842186
PRINT (PRODUCT ": Got an exception while executing the MarshalManagedException event (this exception will be ignored):");
@@ -2197,7 +2199,7 @@ -(struct NSObjectData*) xamarinGetNSObjectData;
21972199
switch (mode) {
21982200
#if !defined (CORECLR_RUNTIME) // CoreCLR won't unwind through native frames, so we'll have to abort (in the default case statement)
21992201
case MarshalManagedExceptionModeDisable:
2200-
case MarshalManagedExceptionModeUnwindNativeCode:
2202+
case MarshalManagedExceptionModeUnwindNativeCode: {
22012203
//
22022204
// We want to maintain the original stack trace of the exception, but unfortunately
22032205
// calling mono_raise_exception directly with the original exception will overwrite
@@ -2210,7 +2212,7 @@ -(struct NSObjectData*) xamarinGetNSObjectData;
22102212
// to throw an exception that contains the original stack trace.
22112213
//
22122214

2213-
handle = xamarin_gchandle_new (exception, false);
2215+
GCHandle handle = xamarin_gchandle_new (exception, false);
22142216
xamarin_rethrow_managed_exception (handle, &exception_gchandle);
22152217
xamarin_gchandle_free (handle);
22162218

@@ -2231,8 +2233,9 @@ -(struct NSObjectData*) xamarinGetNSObjectData;
22312233
xamarin_handling_unhandled_exceptions = 0;
22322234

22332235
mono_raise_exception ((MonoException *) exception);
2234-
#endif
22352236
break;
2237+
}
2238+
#endif
22362239
case MarshalManagedExceptionModeThrowObjectiveCException: {
22372240
GCHandle handle = xamarin_gchandle_new (exception, false);
22382241
NSException *ns_exc = xamarin_unwrap_ns_exception (handle, &exception_gchandle);
@@ -2295,13 +2298,14 @@ -(struct NSObjectData*) xamarinGetNSObjectData;
22952298
case MarshalManagedExceptionModeUnwindNativeCode:
22962299
#endif
22972300
case MarshalManagedExceptionModeAbort:
2298-
default:
2299-
handle = xamarin_gchandle_new (exception, false);
2301+
default: {
2302+
GCHandle handle = xamarin_gchandle_new (exception, false);
23002303
const char *msg = [xamarin_print_all_exceptions (handle) UTF8String];
23012304
xamarin_gchandle_free (handle);
23022305
xamarin_assertion_message ("Aborting due to trying to marshal managed exception:\n%s\n", msg);
23032306
break;
23042307
}
2308+
}
23052309
}
23062310

23072311
void

runtime/trampolines-invoke.m

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@
7979
}
8080

8181
void
82-
xamarin_invoke_trampoline (enum TrampolineType type, id self, SEL sel, iterator_func iterator, marshal_return_value_func marshal_return_value, void *context)
82+
xamarin_invoke_trampoline (enum TrampolineType trampoline_type, id self, SEL sel, iterator_func iterator, marshal_return_value_func marshal_return_value, void *context)
8383
{
8484
MonoObject *exception = NULL;
8585
MonoObject **exception_ptr = xamarin_is_managed_exception_marshaling_disabled () ? NULL : &exception;
8686
GCHandle exception_gchandle = INVALID_GCHANDLE;
87-
bool is_static = (type & Tramp_Static) == Tramp_Static;
88-
bool is_ctor = type == Tramp_Ctor;
87+
bool is_static = (trampoline_type & Tramp_Static) == Tramp_Static;
88+
bool is_ctor = trampoline_type == Tramp_Ctor;
8989
const char *ret_type = NULL;
9090
MonoType *sig_ret_type = NULL;
9191

@@ -148,8 +148,6 @@
148148
void *iter = NULL;
149149
gboolean needs_writeback = FALSE; // determines if there are any ref/out parameters.
150150
MonoType *p;
151-
int ofs;
152-
unsigned long i;
153151
unsigned long mofs = 0;
154152

155153
unsigned long desc_arg_count = num_arg + 2; /* 1 for the return value + 1 if this is a category instance method */
@@ -207,11 +205,11 @@
207205
if (exception_gchandle != INVALID_GCHANDLE)
208206
goto exception_handling;
209207

210-
for (i = 0, ofs = 0; i < num_arg; i++) {
208+
for (unsigned long i = 0, ofs = 0; i < num_arg; i++) {
211209
const char *argType = [sig getArgumentTypeAtIndex: (i+2)];
212210
const char *type = xamarin_skip_encoding_flags (argType);
213211
unsigned long size = xamarin_objc_type_size (type);
214-
int frameofs = ofs;
212+
unsigned long frameofs = ofs;
215213
p = mono_signature_get_params (msig, &iter);
216214
ADD_TO_MONOOBJECT_RELEASE_LIST (p);
217215
LOGZ (" argument %i's type: %s (argType: %s)\n", (int) i + 1, type, argType);
@@ -633,7 +631,7 @@
633631
iterator (IteratorStart, context, NULL, 0, NULL, &exception_gchandle); // start
634632
if (exception_gchandle != INVALID_GCHANDLE)
635633
goto exception_handling;
636-
for (i = 0; i < num_arg; i++) {
634+
for (unsigned long i = 0; i < num_arg; i++) {
637635
const char *type = [sig getArgumentTypeAtIndex: (i+2)];
638636

639637
type = xamarin_skip_encoding_flags (type);

0 commit comments

Comments
 (0)