Skip to content

Commit 82a7ee4

Browse files
authored
[Mono] Fix C4018 round I (#70417)
* First round of change of fixing C4018 * Address part of review feedback * Change the type of idx to unsigned for `effective_table_slow` * Add idx range check after the type was changed * Address review feedback for `class-init.c` * Change the return type of `*table_num_rows*` to `guint32`. Deal with the consequence of return type change of `table_info_get_rows`. Correct the type of a few local variables which store the return of `mono_metadata_token_index`. * Update return type * Address review feedbacks of metadata.c * Fix native crash * Make counter private to for-loop * Address review feedbacks * Address review feedbacks
1 parent e242424 commit 82a7ee4

17 files changed

+133
-138
lines changed

src/mono/mono/component/debugger-state-machine.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ mono_debugger_state (JsonWriter *writer)
298298
mono_json_writer_object_key(writer, "breakpoints");
299299
mono_json_writer_array_begin (writer);
300300

301-
for (int i=0; i < breakpoint_copy->len; i++) {
301+
for (guint i=0; i < breakpoint_copy->len; i++) {
302302
MonoBreakpoint *bp = (MonoBreakpoint *) g_ptr_array_index (breakpoint_copy, i);
303303

304304
mono_json_writer_indent (writer);

src/mono/mono/component/hot_reload-stub.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static void
3939
hot_reload_stub_cleanup_on_close (MonoImage *image);
4040

4141
static void
42-
hot_reload_stub_effective_table_slow (const MonoTableInfo **t, int idx);
42+
hot_reload_stub_effective_table_slow (const MonoTableInfo **t, uint32_t idx);
4343

4444
static void
4545
hot_reload_stub_close_except_pools_all (MonoImage *base_image);
@@ -62,7 +62,7 @@ hot_reload_stub_get_updated_method_ppdb (MonoImage *base_image, uint32_t idx);
6262
static gboolean
6363
hot_reload_stub_has_modified_rows (const MonoTableInfo *table);
6464

65-
static int
65+
static guint32
6666
hot_reload_stub_table_num_rows_slow (MonoImage *image, int table_index);
6767

6868
static uint32_t
@@ -187,7 +187,7 @@ hot_reload_stub_cleanup_on_close (MonoImage *image)
187187
}
188188

189189
void
190-
hot_reload_stub_effective_table_slow (const MonoTableInfo **t, int idx)
190+
hot_reload_stub_effective_table_slow (const MonoTableInfo **t, uint32_t idx)
191191
{
192192
g_assert_not_reached ();
193193
}
@@ -238,7 +238,7 @@ hot_reload_stub_has_modified_rows (const MonoTableInfo *table)
238238
return FALSE;
239239
}
240240

241-
static int
241+
static guint32
242242
hot_reload_stub_table_num_rows_slow (MonoImage *image, int table_index)
243243
{
244244
g_assert_not_reached (); /* should always take the fast path */

src/mono/mono/component/hot_reload.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static void
6464
hot_reload_cleanup_on_close (MonoImage *image);
6565

6666
static void
67-
hot_reload_effective_table_slow (const MonoTableInfo **t, int idx);
67+
hot_reload_effective_table_slow (const MonoTableInfo **t, uint32_t idx);
6868

6969
static void
7070
hot_reload_apply_changes (int origin, MonoImage *base_image, gconstpointer dmeta, uint32_t dmeta_len, gconstpointer dil, uint32_t dil_len, gconstpointer dpdb_bytes_orig, uint32_t dpdb_length, MonoError *error);
@@ -93,7 +93,7 @@ hot_reload_get_updated_method_ppdb (MonoImage *base_image, uint32_t idx);
9393
static gboolean
9494
hot_reload_has_modified_rows (const MonoTableInfo *table);
9595

96-
static int
96+
static guint32
9797
hot_reload_table_num_rows_slow (MonoImage *image, int table_index);
9898

9999
static GSList*
@@ -1057,7 +1057,7 @@ effective_table_mutant (MonoImage *base, BaselineInfo *info, int tbl_index, cons
10571057
}
10581058

10591059
void
1060-
hot_reload_effective_table_slow (const MonoTableInfo **t, int idx)
1060+
hot_reload_effective_table_slow (const MonoTableInfo **t, uint32_t idx G_GNUC_UNUSED)
10611061
{
10621062
/* FIXME: don't let any thread other than the updater thread see values from a delta image
10631063
* with a generation past update_published
@@ -2694,7 +2694,7 @@ hot_reload_has_modified_rows (const MonoTableInfo *table)
26942694
return info->any_modified_rows[tbl_index];
26952695
}
26962696

2697-
static int
2697+
static guint32
26982698
hot_reload_table_num_rows_slow (MonoImage *base, int table_index)
26992699
{
27002700
BaselineInfo *base_info = baseline_info_lookup (base);

src/mono/mono/component/hot_reload.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ typedef struct _MonoComponentHotReload {
2323
uint32_t (*thread_expose_published) (void);
2424
uint32_t (*get_thread_generation) (void);
2525
void (*cleanup_on_close) (MonoImage *image);
26-
void (*effective_table_slow) (const MonoTableInfo **t, int idx);
26+
void (*effective_table_slow) (const MonoTableInfo **t, uint32_t idx);
2727
void (*apply_changes) (int origin, MonoImage *base_image, gconstpointer dmeta, uint32_t dmeta_len, gconstpointer dil, uint32_t dil_len, gconstpointer dpdb_bytes_orig, uint32_t dpdb_length, MonoError *error);
2828
void (*image_close_except_pools_all) (MonoImage *base_image);
2929
void (*image_close_all) (MonoImage *base_image);
@@ -32,7 +32,7 @@ typedef struct _MonoComponentHotReload {
3232
gboolean (*delta_heap_lookup) (MonoImage *base_image, MetadataHeapGetterFunc get_heap, uint32_t orig_index, MonoImage **image_out, uint32_t *index_out);
3333
gpointer (*get_updated_method_ppdb) (MonoImage *base_image, uint32_t idx);
3434
gboolean (*has_modified_rows) (const MonoTableInfo *table);
35-
gboolean (*table_num_rows_slow) (MonoImage *base_image, int table_index);
35+
uint32_t (*table_num_rows_slow) (MonoImage *base_image, int table_index);
3636
uint32_t (*method_parent) (MonoImage *base_image, uint32_t method_index);
3737
void* (*metadata_linear_search) (MonoImage *base_image, MonoTableInfo *base_table, const void *key, BinarySearchComparer comparer);
3838
uint32_t (*field_parent) (MonoImage *base_image, uint32_t method_index);

src/mono/mono/eglib/garray.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ g_array_set_size (GArray *array, gint length)
229229
if (length == priv->capacity)
230230
return; // nothing to be done
231231

232-
if (length > priv->capacity) {
232+
if (GINT_TO_UINT(length) > priv->capacity) {
233233
// grow the array
234234
ensure_capacity (priv, length);
235235
}

src/mono/mono/eglib/gfile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ g_file_set_contents (const gchar *filename, const gchar *contents, gssize length
133133
if (length < 0)
134134
length = strlen (contents);
135135

136-
if (fwrite (contents, 1, length, fp) < length) {
136+
if (fwrite (contents, 1, length, fp) < GSSIZE_TO_SIZE(length)) {
137137
g_set_error (err, G_FILE_ERROR, g_file_error_from_errno (ferror (fp)), "%s", g_strerror (ferror (fp)));
138138
g_unlink (path);
139139
g_free (path);

src/mono/mono/eglib/glib.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,9 @@ __CAST_STYPE_TO_UTYPE(gssize, guint32, UINT32_MAX)
14401440
__CAST_STYPE_TO_STYPE(gssize, gint, INT_MIN, INT_MAX)
14411441
__CAST_STYPE_TO_UTYPE(gssize, guint, UINT_MAX)
14421442

1443+
__CAST_STYPE_TO_UTYPE(gssize, gsize, SIZE_MAX)
1444+
__CAST_UTYPE_TO_STYPE(gsize, gssize, PTRDIFF_MIN, PTRDIFF_MAX)
1445+
14431446
__CAST_STYPE_TO_STYPE(gdouble, gint64, INT64_MIN, INT64_MAX)
14441447
__CAST_STYPE_TO_UTYPE(gdouble, guint64, UINT64_MAX)
14451448
__CAST_STYPE_TO_STYPE(gdouble, gint32, INT32_MIN, INT32_MAX)
@@ -1487,6 +1490,7 @@ __CAST_STYPE_TO_UTYPE(gint32, guint8, UINT8_MAX)
14871490

14881491
__CAST_STYPE_TO_UTYPE(gint32, guint, UINT_MAX)
14891492

1493+
__CAST_UTYPE_TO_UTYPE(guint32, guint, UINT_MAX)
14901494
__CAST_UTYPE_TO_STYPE(guint32, gint32, INT32_MIN, INT32_MAX)
14911495
__CAST_UTYPE_TO_STYPE(guint32, gint16, INT16_MIN, INT16_MAX)
14921496
__CAST_UTYPE_TO_UTYPE(guint32, guint16, UINT16_MAX)
@@ -1617,6 +1621,9 @@ __CAST_UTYPE_TO_STYPE(gunichar, gchar, CHAR_MIN, CHAR_MAX)
16171621
#define GSSIZE_TO_INT(v) G_CAST_TYPE_TO_TYPE(gssize, gint, v)
16181622
#define GSSIZE_TO_UINT(v) G_CAST_TYPE_TO_TYPE(gssize, guint, v)
16191623

1624+
#define GSSIZE_TO_SIZE(v) G_CAST_TYPE_TO_TYPE(gssize, gsize, v)
1625+
#define GSIZE_TO_SSIZE(v) G_CAST_TYPE_TO_TYPE(gsize, gssize, v)
1626+
16201627
#define GDOUBLE_TO_INT64(v) G_CAST_TYPE_TO_TYPE(gdouble, gint64, v)
16211628
#define GDOUBLE_TO_UINT64(v) G_CAST_TYPE_TO_TYPE(gdouble, guint64, v)
16221629
#define GDOUBLE_TO_INT32(v) G_CAST_TYPE_TO_TYPE(gdouble, gint32, v)
@@ -1670,6 +1677,7 @@ __CAST_UTYPE_TO_STYPE(gunichar, gchar, CHAR_MIN, CHAR_MAX)
16701677

16711678
#define GINT32_TO_UINT(v) G_CAST_TYPE_TO_TYPE(gint32, guint, v)
16721679

1680+
#define GUINT32_TO_UINT(v) G_CAST_TYPE_TO_TYPE(guint32, guint, v)
16731681
#define GUINT32_TO_INT32(v) G_CAST_TYPE_TO_TYPE(guint32, gint32, v)
16741682
#define GUINT32_TO_INT16(v) G_CAST_TYPE_TO_TYPE(guint32, gint16, v)
16751683
#define GUINT32_TO_UINT16(v) G_CAST_TYPE_TO_TYPE(guint32, guint16, v)

src/mono/mono/eglib/gptrarray.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ gboolean
226226
g_ptr_array_find (GPtrArray *array, gconstpointer needle, guint *index)
227227
{
228228
g_assert (array);
229-
for (int i = 0; i < array->len; i++) {
229+
for (guint i = 0; i < array->len; i++) {
230230
if (array->pdata [i] == needle) {
231231
if (index)
232232
*index = i;

src/mono/mono/eventpipe/ep-rt-mono.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,7 +1469,7 @@ eventpipe_fire_method_events (
14691469
il_offsets = (uint32_t*)events_data->buffer;
14701470
native_offsets = il_offsets + offset_entries;
14711471

1472-
for (int32_t offset_count = 0; offset_count < offset_entries; ++offset_count) {
1472+
for (uint32_t offset_count = 0; offset_count < offset_entries; ++offset_count) {
14731473
il_offsets [offset_count] = debug_info->line_numbers [offset_count].il_offset;
14741474
native_offsets [offset_count] = debug_info->line_numbers [offset_count].native_offset;
14751475
}
@@ -3106,7 +3106,7 @@ ep_rt_mono_fire_bulk_type_event (BulkTypeEventLogger *type_logger)
31063106

31073107
char *ptr = (char *)type_logger->bulk_type_event_buffer;
31083108

3109-
for (int type_value_index = 0; type_value_index < type_logger->bulk_type_value_count; type_value_index++) {
3109+
for (uint32_t type_value_index = 0; type_value_index < type_logger->bulk_type_value_count; type_value_index++) {
31103110
BulkTypeValue *target = &type_logger->bulk_type_values [type_value_index];
31113111

31123112
values_element_size += write_event_buffer_int64 (target->fixed_sized_data.type_id, ptr, &ptr);
@@ -3120,7 +3120,7 @@ ep_rt_mono_fire_bulk_type_event (BulkTypeEventLogger *type_logger)
31203120

31213121
values_element_size += write_event_buffer_int32 (target->type_parameters_count, ptr, &ptr);
31223122

3123-
for (int i = 0; i < target->type_parameters_count; i++) {
3123+
for (uint32_t i = 0; i < target->type_parameters_count; i++) {
31243124
uint64_t type_parameter = get_typeid_for_type (target->mono_type_parameters [i]);
31253125
values_element_size += write_event_buffer_int64 ((int64_t)type_parameter, ptr, &ptr);
31263126
}
@@ -3413,7 +3413,7 @@ ep_rt_mono_send_method_details_event (MonoMethod *method)
34133413
method_inst_parameter_types_count = method_inst->type_argc;
34143414

34153415
uint64_t *method_inst_parameters_type_ids = mono_mempool_alloc0 (type_logger->mem_pool, method_inst_parameter_types_count * sizeof (uint64_t));
3416-
for (int i = 0; i < method_inst_parameter_types_count; i++) {
3416+
for (uint32_t i = 0; i < method_inst_parameter_types_count; i++) {
34173417
method_inst_parameters_type_ids [i] = get_typeid_for_type (method_inst->type_argv [i]);
34183418

34193419
ep_rt_mono_log_type_and_parameters_if_necessary (type_logger, method_inst->type_argv [i]);
@@ -3524,7 +3524,7 @@ ep_rt_mono_write_event_method_il_to_native_map (
35243524
}
35253525
if (il_offsets) {
35263526
native_offsets = il_offsets + offset_entries;
3527-
for (int32_t offset_count = 0; offset_count < offset_entries; ++offset_count) {
3527+
for (uint32_t offset_count = 0; offset_count < offset_entries; ++offset_count) {
35283528
il_offsets [offset_count] = debug_info->line_numbers [offset_count].il_offset;
35293529
native_offsets [offset_count] = debug_info->line_numbers [offset_count].native_offset;
35303530
}

src/mono/mono/eventpipe/ep-rt-mono.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1460,7 +1460,7 @@ ep_rt_temp_path_get (
14601460

14611461
const ep_char8_t *path = g_get_tmp_dir ();
14621462
int32_t result = snprintf (buffer, buffer_len, "%s", path);
1463-
if (result <= 0 || result > buffer_len)
1463+
if (result <= 0 || GINT32_TO_UINT32(result) > buffer_len)
14641464
ep_raise_error ();
14651465

14661466
if (buffer [result - 1] != G_DIR_SEPARATOR) {

src/mono/mono/metadata/class-init.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ mono_class_create_from_typedef (MonoImage *image, guint32 type_token, MonoError
436436
MonoClass *klass, *parent = NULL;
437437
guint32 cols [MONO_TYPEDEF_SIZE];
438438
guint32 cols_next [MONO_TYPEDEF_SIZE];
439-
guint tidx = mono_metadata_token_index (type_token);
439+
guint32 tidx = mono_metadata_token_index (type_token);
440440
MonoGenericContext *context = NULL;
441441
const char *name, *nspace;
442442
guint icount = 0;
@@ -2020,7 +2020,8 @@ mono_class_layout_fields (MonoClass *klass, int base_instance_size, int packing_
20202020
int i;
20212021
const int top = mono_class_get_field_count (klass);
20222022
guint32 layout = mono_class_get_flags (klass) & TYPE_ATTRIBUTE_LAYOUT_MASK;
2023-
guint32 pass, passes, real_size;
2023+
guint32 pass, passes;
2024+
gint32 real_size;
20242025
gboolean gc_aware_layout = FALSE;
20252026
gboolean has_static_fields = FALSE;
20262027
gboolean has_references = FALSE;
@@ -2270,8 +2271,7 @@ mono_class_layout_fields (MonoClass *klass, int base_instance_size, int packing_
22702271
case TYPE_ATTRIBUTE_EXPLICIT_LAYOUT: {
22712272
real_size = 0;
22722273
for (i = 0; i < top; i++) {
2273-
gint32 align;
2274-
guint32 size;
2274+
gint32 align, size;
22752275
MonoType *ftype;
22762276

22772277
field = &klass->fields [i];
@@ -2815,7 +2815,7 @@ mono_get_unique_iid (MonoClass *klass)
28152815

28162816
iid = mono_bitset_find_first_unset (global_interface_bitset, -1);
28172817
if (iid < 0) {
2818-
int old_size = mono_bitset_size (global_interface_bitset);
2818+
guint32 old_size = mono_bitset_size (global_interface_bitset);
28192819
MonoBitSet *new_set = mono_bitset_clone (global_interface_bitset, old_size * 2);
28202820
mono_bitset_free (global_interface_bitset);
28212821
global_interface_bitset = new_set;
@@ -2825,7 +2825,7 @@ mono_get_unique_iid (MonoClass *klass)
28252825
/* set the bit also in the per-image set */
28262826
if (!mono_class_is_ginst (klass)) {
28272827
if (klass->image->interface_bitset) {
2828-
if (iid >= mono_bitset_size (klass->image->interface_bitset)) {
2828+
if (GINT_TO_UINT32(iid) >= mono_bitset_size (klass->image->interface_bitset)) {
28292829
MonoBitSet *new_set = mono_bitset_clone (klass->image->interface_bitset, iid + 1);
28302830
mono_bitset_free (klass->image->interface_bitset);
28312831
klass->image->interface_bitset = new_set;
@@ -3567,12 +3567,11 @@ mono_class_setup_methods (MonoClass *klass)
35673567
void
35683568
mono_class_setup_properties (MonoClass *klass)
35693569
{
3570-
guint startm, endm, i, j;
3570+
guint startm, endm;
35713571
guint32 cols [MONO_PROPERTY_SIZE];
35723572
MonoTableInfo *msemt = &klass->image->tables [MONO_TABLE_METHODSEMANTICS];
35733573
MonoProperty *properties;
3574-
guint32 last;
3575-
int first, count;
3574+
guint32 first, last, count;
35763575
MonoClassPropertyInfo *info;
35773576

35783577
info = mono_class_get_property_info (klass);
@@ -3590,7 +3589,7 @@ mono_class_setup_properties (MonoClass *klass)
35903589
MonoClassPropertyInfo *ginfo = mono_class_get_property_info (gklass);
35913590
properties = mono_class_new0 (klass, MonoProperty, ginfo->count + 1);
35923591

3593-
for (i = 0; i < ginfo->count; i++) {
3592+
for (guint32 i = 0; i < ginfo->count; i++) {
35943593
ERROR_DECL (error);
35953594
MonoProperty *prop = &properties [i];
35963595

@@ -3611,6 +3610,7 @@ mono_class_setup_properties (MonoClass *klass)
36113610
count = ginfo->count;
36123611
} else {
36133612
first = mono_metadata_properties_from_typedef (klass->image, mono_metadata_token_index (klass->type_token) - 1, &last);
3613+
g_assert ((last - first) >= 0);
36143614
count = last - first;
36153615

36163616
if (count) {
@@ -3620,15 +3620,15 @@ mono_class_setup_properties (MonoClass *klass)
36203620
}
36213621

36223622
properties = (MonoProperty *)mono_class_alloc0 (klass, sizeof (MonoProperty) * count);
3623-
for (i = first; i < last; ++i) {
3623+
for (guint32 i = first; i < last; ++i) {
36243624
mono_metadata_decode_table_row (klass->image, MONO_TABLE_PROPERTY, i, cols, MONO_PROPERTY_SIZE);
36253625
properties [i - first].parent = klass;
36263626
properties [i - first].attrs = cols [MONO_PROPERTY_FLAGS];
36273627
properties [i - first].name = mono_metadata_string_heap (klass->image, cols [MONO_PROPERTY_NAME]);
36283628

36293629
startm = mono_metadata_methods_from_property (klass->image, i, &endm);
36303630
int first_idx = mono_class_get_first_method_idx (klass);
3631-
for (j = startm; j < endm; ++j) {
3631+
for (guint j = startm; j < endm; ++j) {
36323632
MonoMethod *method;
36333633

36343634
mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);
@@ -3690,11 +3690,10 @@ inflate_method_listz (MonoMethod **methods, MonoClass *klass, MonoGenericContext
36903690
void
36913691
mono_class_setup_events (MonoClass *klass)
36923692
{
3693-
int first, count;
3694-
guint startm, endm, i, j;
3693+
guint32 first, last, count;
3694+
guint startm, endm;
36953695
guint32 cols [MONO_EVENT_SIZE];
36963696
MonoTableInfo *msemt = &klass->image->tables [MONO_TABLE_METHODSEMANTICS];
3697-
guint32 last;
36983697
MonoEvent *events;
36993698

37003699
MonoClassEventInfo *info = mono_class_get_event_info (klass);
@@ -3718,7 +3717,7 @@ mono_class_setup_events (MonoClass *klass)
37183717
if (count)
37193718
context = mono_class_get_context (klass);
37203719

3721-
for (i = 0; i < count; i++) {
3720+
for (guint32 i = 0; i < count; i++) {
37223721
ERROR_DECL (error);
37233722
MonoEvent *event = &events [i];
37243723
MonoEvent *gevent = &ginfo->events [i];
@@ -3739,6 +3738,7 @@ mono_class_setup_events (MonoClass *klass)
37393738
}
37403739
} else {
37413740
first = mono_metadata_events_from_typedef (klass->image, mono_metadata_token_index (klass->type_token) - 1, &last);
3741+
g_assert ((last - first) >= 0);
37423742
count = last - first;
37433743

37443744
if (count) {
@@ -3749,7 +3749,7 @@ mono_class_setup_events (MonoClass *klass)
37493749
}
37503750

37513751
events = (MonoEvent *)mono_class_alloc0 (klass, sizeof (MonoEvent) * count);
3752-
for (i = first; i < last; ++i) {
3752+
for (guint32 i = first; i < last; ++i) {
37533753
MonoEvent *event = &events [i - first];
37543754

37553755
mono_metadata_decode_table_row (klass->image, MONO_TABLE_EVENT, i, cols, MONO_EVENT_SIZE);
@@ -3759,7 +3759,7 @@ mono_class_setup_events (MonoClass *klass)
37593759

37603760
startm = mono_metadata_methods_from_event (klass->image, i, &endm);
37613761
int first_idx = mono_class_get_first_method_idx (klass);
3762-
for (j = startm; j < endm; ++j) {
3762+
for (guint j = startm; j < endm; ++j) {
37633763
MonoMethod *method;
37643764

37653765
mono_metadata_decode_row (msemt, j, cols, MONO_METHOD_SEMA_SIZE);

0 commit comments

Comments
 (0)