Skip to content

Commit aa668a5

Browse files
authored
[native] Switch C++ standard to C++23 (#9325)
Whenever `size_t` or `ssize_t` types are used and an integer literal is involved, use the `uz` or `z` literal suffix. This is to make the intent explicit as well as to avoid hidden type conversions. Despite being more verbose, this makes the code more type-safe.
1 parent 46f9145 commit aa668a5

33 files changed

+181
-185
lines changed

src/native/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ ensure_variable_set(XA_LIB_TOP_DIR)
3333
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${OUTPUT_PATH}/${ANDROID_RID}" CACHE PATH "" FORCE)
3434
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${OUTPUT_PATH}/${ANDROID_RID}" CACHE PATH "" FORCE)
3535

36-
set(CMAKE_CXX_STANDARD 20)
36+
set(CMAKE_CXX_STANDARD 23)
3737
set(CMAKE_CXX_STANDARD_REQUIRED ON)
3838
set(CMAKE_CXX_EXTENSIONS OFF)
3939

src/native/monodroid/archive-dso-stub-config.hh.in

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
namespace xamarin::android {
66
struct ArchiveDSOStubConfig
77
{
8-
static inline constexpr size_t PayloadSectionAlignment = @ARCHIVE_DSO_STUB_PAYLOAD_SECTION_ALIGNMENT@;
9-
static inline constexpr size_t SectionHeaderEntrySize = @SECTION_HEADER_ENTRY_SIZE@;
10-
static inline constexpr size_t SectionHeaderEntryCount = @SECTION_HEADER_ENTRY_COUNT@;
11-
static inline constexpr uint32_t PayloadSectionOffset = @PAYLOAD_SECTION_OFFSET@;
8+
static inline constexpr size_t PayloadSectionAlignment = @ARCHIVE_DSO_STUB_PAYLOAD_SECTION_ALIGNMENT@uz;
9+
static inline constexpr size_t SectionHeaderEntrySize = @SECTION_HEADER_ENTRY_SIZE@uz;
10+
static inline constexpr size_t SectionHeaderEntryCount = @SECTION_HEADER_ENTRY_COUNT@uz;
11+
static inline constexpr uint32_t PayloadSectionOffset = @PAYLOAD_SECTION_OFFSET@uz;
1212

1313
// We know that payload section is the last one in the binary, this is an index into
1414
// the section header table.
15-
static inline constexpr size_t PayloadSectionIndex = SectionHeaderEntryCount - 1;
15+
static inline constexpr size_t PayloadSectionIndex = SectionHeaderEntryCount - 1uz;
1616
};
1717
}

src/native/monodroid/embedded-assemblies-zip.cc

+27-29
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
using namespace xamarin::android::internal;
1616

17-
using read_count_type = size_t;
18-
1917
force_inline bool
2018
EmbeddedAssemblies::zip_load_entry_common (size_t entry_index, std::vector<uint8_t> const& buf, dynamic_local_string<SENSIBLE_PATH_MAX> &entry_name, ZipEntryLoadState &state) noexcept
2119
{
@@ -157,7 +155,7 @@ EmbeddedAssemblies::zip_load_individual_assembly_entries (std::vector<uint8_t> c
157155
// However, clang-tidy can't know that the value is owned by Mono and we must not free it, thus the suppression.
158156
//
159157
// NOLINTNEXTLINE(clang-analyzer-unix.Malloc)
160-
for (size_t i = 0; i < num_entries; i++) {
158+
for (size_t i = 0uz; i < num_entries; i++) {
161159
bool interesting_entry = zip_load_entry_common (i, buf, entry_name, state);
162160
if (!interesting_entry) {
163161
continue;
@@ -260,7 +258,7 @@ EmbeddedAssemblies::zip_load_assembly_store_entries (std::vector<uint8_t> const&
260258
bool assembly_store_found = false;
261259

262260
log_debug (LOG_ASSEMBLY, "Looking for assembly stores in APK ('%s)", assembly_store_file_path.data ());
263-
for (size_t i = 0; i < num_entries; i++) {
261+
for (size_t i = 0uz; i < num_entries; i++) {
264262
if (all_required_zip_entries_found ()) {
265263
need_to_scan_more_apks = false;
266264
break;
@@ -341,17 +339,17 @@ EmbeddedAssemblies::zip_load_entries (int fd, const char *apk_name, [[maybe_unus
341339
.file_name = apk_name,
342340
.prefix = prefix,
343341
.prefix_len = prefix_len,
344-
.buf_offset = 0,
345-
.compression_method = 0,
346-
.local_header_offset = 0,
347-
.data_offset = 0,
348-
.file_size = 0,
342+
.buf_offset = 0uz,
343+
.compression_method = 0u,
344+
.local_header_offset = 0u,
345+
.data_offset = 0u,
346+
.file_size = 0u,
349347
.bundled_assemblies_slow_path = false,
350-
.max_assembly_name_size = 0,
351-
.max_assembly_file_name_size = 0,
348+
.max_assembly_name_size = 0u,
349+
.max_assembly_file_name_size = 0u,
352350
};
353351

354-
ssize_t nread = read (fd, buf.data (), static_cast<read_count_type>(buf.size ()));
352+
ssize_t nread = read (fd, buf.data (), buf.size ());
355353
if (static_cast<size_t>(nread) != cd_size) {
356354
Helpers::abort_application (
357355
LOG_ASSEMBLY,
@@ -424,14 +422,14 @@ EmbeddedAssemblies::zip_read_cd_info (int fd, uint32_t& cd_offset, uint32_t& cd_
424422
}
425423

426424
std::array<uint8_t, ZIP_EOCD_LEN> eocd;
427-
ssize_t nread = ::read (fd, eocd.data (), static_cast<read_count_type>(eocd.size ()));
425+
ssize_t nread = ::read (fd, eocd.data (), eocd.size ());
428426
if (nread < 0 || nread != eocd.size ()) {
429427
log_error (LOG_ASSEMBLY, "Failed to read EOCD from the APK: %s (nread: %d; errno: %d)", std::strerror (errno), nread, errno);
430428
return false;
431429
}
432430

433-
size_t index = 0; // signature
434-
std::array<uint8_t, 4> signature;
431+
size_t index = 0uz; // signature
432+
std::array<uint8_t, 4uz> signature;
435433

436434
if (!zip_read_field (eocd, index, signature)) {
437435
log_error (LOG_ASSEMBLY, "Failed to read EOCD signature");
@@ -443,7 +441,7 @@ EmbeddedAssemblies::zip_read_cd_info (int fd, uint32_t& cd_offset, uint32_t& cd_
443441
}
444442

445443
// Most probably a ZIP with comment
446-
constexpr size_t alloc_size = 65535 + ZIP_EOCD_LEN; // 64k is the biggest comment size allowed
444+
constexpr size_t alloc_size = 65535uz + ZIP_EOCD_LEN; // 64k is the biggest comment size allowed
447445
ret = ::lseek (fd, static_cast<off_t>(-alloc_size), SEEK_END);
448446
if (ret < 0) {
449447
log_error (LOG_ASSEMBLY, "Unable to seek into the file to find ECOD before APK comment: %s (ret: %d; errno: %d)", std::strerror (errno), ret, errno);
@@ -452,7 +450,7 @@ EmbeddedAssemblies::zip_read_cd_info (int fd, uint32_t& cd_offset, uint32_t& cd_
452450

453451
std::vector<uint8_t> buf (alloc_size);
454452

455-
nread = ::read (fd, buf.data (), static_cast<read_count_type>(buf.size ()));
453+
nread = ::read (fd, buf.data (), buf.size ());
456454

457455
if (nread < 0 || static_cast<size_t>(nread) != alloc_size) {
458456
log_error (LOG_ASSEMBLY, "Failed to read EOCD and comment from the APK: %s (nread: %d; errno: %d)", std::strerror (errno), nread, errno);
@@ -462,7 +460,7 @@ EmbeddedAssemblies::zip_read_cd_info (int fd, uint32_t& cd_offset, uint32_t& cd_
462460
// We scan from the end to save time
463461
bool found = false;
464462
const uint8_t* data = buf.data ();
465-
for (ssize_t i = static_cast<ssize_t>(alloc_size - (ZIP_EOCD_LEN + 2)); i >= 0; i--) {
463+
for (ssize_t i = static_cast<ssize_t>(alloc_size - (ZIP_EOCD_LEN + 2)); i >= 0z; i--) {
466464
if (memcmp (data + i, ZIP_EOCD_MAGIC.data (), sizeof(ZIP_EOCD_MAGIC)) != 0)
467465
continue;
468466

@@ -482,8 +480,8 @@ EmbeddedAssemblies::zip_read_cd_info (int fd, uint32_t& cd_offset, uint32_t& cd_
482480
bool
483481
EmbeddedAssemblies::zip_adjust_data_offset (int fd, ZipEntryLoadState &state)
484482
{
485-
static constexpr size_t LH_FILE_NAME_LENGTH_OFFSET = 26;
486-
static constexpr size_t LH_EXTRA_LENGTH_OFFSET = 28;
483+
static constexpr size_t LH_FILE_NAME_LENGTH_OFFSET = 26uz;
484+
static constexpr size_t LH_EXTRA_LENGTH_OFFSET = 28uz;
487485

488486
off_t result = ::lseek (fd, static_cast<off_t>(state.local_header_offset), SEEK_SET);
489487
if (result < 0) {
@@ -534,9 +532,9 @@ template<size_t BufSize>
534532
bool
535533
EmbeddedAssemblies::zip_extract_cd_info (std::array<uint8_t, BufSize> const& buf, uint32_t& cd_offset, uint32_t& cd_size, uint16_t& cd_entries)
536534
{
537-
constexpr size_t EOCD_TOTAL_ENTRIES_OFFSET = 10;
538-
constexpr size_t EOCD_CD_SIZE_OFFSET = 12;
539-
constexpr size_t EOCD_CD_START_OFFSET = 16;
535+
constexpr size_t EOCD_TOTAL_ENTRIES_OFFSET = 10uz;
536+
constexpr size_t EOCD_CD_SIZE_OFFSET = 12uz;
537+
constexpr size_t EOCD_CD_START_OFFSET = 16uz;
540538

541539
static_assert (BufSize >= ZIP_EOCD_LEN, "Buffer too short for EOCD");
542540

@@ -627,12 +625,12 @@ EmbeddedAssemblies::zip_read_field (T const& buf, size_t index, size_t count, dy
627625
bool
628626
EmbeddedAssemblies::zip_read_entry_info (std::vector<uint8_t> const& buf, dynamic_local_string<SENSIBLE_PATH_MAX>& file_name, ZipEntryLoadState &state)
629627
{
630-
constexpr size_t CD_COMPRESSION_METHOD_OFFSET = 10;
631-
constexpr size_t CD_UNCOMPRESSED_SIZE_OFFSET = 24;
632-
constexpr size_t CD_FILENAME_LENGTH_OFFSET = 28;
633-
constexpr size_t CD_EXTRA_LENGTH_OFFSET = 30;
634-
constexpr size_t CD_LOCAL_HEADER_POS_OFFSET = 42;
635-
constexpr size_t CD_COMMENT_LENGTH_OFFSET = 32;
628+
constexpr size_t CD_COMPRESSION_METHOD_OFFSET = 10uz;
629+
constexpr size_t CD_UNCOMPRESSED_SIZE_OFFSET = 24uz;
630+
constexpr size_t CD_FILENAME_LENGTH_OFFSET = 28uz;
631+
constexpr size_t CD_EXTRA_LENGTH_OFFSET = 30uz;
632+
constexpr size_t CD_LOCAL_HEADER_POS_OFFSET = 42uz;
633+
constexpr size_t CD_COMMENT_LENGTH_OFFSET = 32uz;
636634

637635
size_t index = state.buf_offset;
638636
zip_ensure_valid_params (buf, index, ZIP_CENTRAL_LEN);

src/native/monodroid/embedded-assemblies.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ EmbeddedAssemblies::map_runtime_file (XamarinAndroidBundledAssembly& file) noexc
238238
if (Util::should_log (LOG_ASSEMBLY) && map_info.area != nullptr) [[unlikely]] {
239239
const char *p = (const char*) file.data;
240240

241-
std::array<char, 9> header;
242-
for (size_t j = 0; j < header.size () - 1; ++j)
241+
std::array<char, 9uz> header;
242+
for (size_t j = 0uz; j < header.size () - 1uz; ++j)
243243
header[j] = isprint (p [j]) ? p [j] : '.';
244244
header [header.size () - 1] = '\0';
245245

@@ -349,7 +349,7 @@ EmbeddedAssemblies::individual_assemblies_open_from_bundles (dynamic_local_strin
349349

350350
MonoAssembly *a = nullptr;
351351

352-
for (size_t i = 0; i < application_config.number_of_assemblies_in_apk; i++) {
352+
for (size_t i = 0uz; i < application_config.number_of_assemblies_in_apk; i++) {
353353
a = load_bundled_assembly (bundled_assemblies [i], name, abi_name, loader_data, ref_only);
354354
if (a != nullptr) {
355355
return a;
@@ -575,7 +575,7 @@ EmbeddedAssemblies::binary_search (const Key *key, const Entry *base, size_t nme
575575
force_inline const TypeMapModuleEntry*
576576
EmbeddedAssemblies::binary_search (uint32_t key, const TypeMapModuleEntry *arr, uint32_t n) noexcept
577577
{
578-
ssize_t left = -1;
578+
ssize_t left = -1z;
579579
ssize_t right = static_cast<ssize_t>(n);
580580
ssize_t middle;
581581

@@ -1010,7 +1010,7 @@ EmbeddedAssemblies::typemap_load_index (TypeMapIndexHeader &header, size_t file_
10101010
}
10111011

10121012
uint8_t *p = data.get ();
1013-
for (size_t i = 0; i < type_map_count; i++) {
1013+
for (size_t i = 0uz; i < type_map_count; i++) {
10141014
type_maps[i].assembly_name = reinterpret_cast<char*>(p);
10151015
p += entry_size;
10161016
}
@@ -1043,7 +1043,7 @@ EmbeddedAssemblies::typemap_load_index (int dir_fd, const char *dir_path, const
10431043
bool
10441044
EmbeddedAssemblies::typemap_load_file (BinaryTypeMapHeader &header, const char *dir_path, const char *file_path, int file_fd, TypeMap &module)
10451045
{
1046-
size_t alloc_size = Helpers::add_with_overflow_check<size_t> (header.assembly_name_length, 1);
1046+
size_t alloc_size = Helpers::add_with_overflow_check<size_t> (header.assembly_name_length, 1uz);
10471047
module.assembly_name = new char[alloc_size];
10481048

10491049
ssize_t nread = do_read (file_fd, module.assembly_name, header.assembly_name_length);
@@ -1086,7 +1086,7 @@ EmbeddedAssemblies::typemap_load_file (BinaryTypeMapHeader &header, const char *
10861086
TypeMapEntry *cur;
10871087

10881088
constexpr uint32_t INVALID_TYPE_INDEX = std::numeric_limits<uint32_t>::max ();
1089-
for (size_t i = 0; i < module.entry_count; i++) {
1089+
for (size_t i = 0uz; i < module.entry_count; i++) {
10901090
cur = const_cast<TypeMapEntry*> (&module.java_to_managed[i]);
10911091
cur->from = reinterpret_cast<char*>(java_pos);
10921092

@@ -1291,7 +1291,7 @@ EmbeddedAssemblies::register_from_filesystem (const char *lib_dir_path,bool look
12911291
)
12921292
);
12931293

1294-
size_t assembly_count = 0;
1294+
size_t assembly_count = 0uz;
12951295
do {
12961296
errno = 0;
12971297
dirent *cur = readdir (lib_dir);

src/native/monodroid/embedded-assemblies.hh

+7-7
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ namespace xamarin::android::internal {
341341
}
342342

343343
template<typename Key, typename Entry, int (*compare)(const Key*, const Entry*), bool use_extra_size = false>
344-
static const Entry* binary_search (const Key *key, const Entry *base, size_t nmemb, size_t extra_size = 0) noexcept;
344+
static const Entry* binary_search (const Key *key, const Entry *base, size_t nmemb, size_t extra_size = 0uz) noexcept;
345345

346346
#if defined (DEBUG)
347347
static int compare_type_name (const char *type_name, const TypeMapEntry *entry) noexcept;
@@ -390,7 +390,7 @@ namespace xamarin::android::internal {
390390
}
391391

392392
template<bool IsSatelliteAssembly>
393-
static void unmangle_name (dynamic_local_string<SENSIBLE_PATH_MAX> &name, size_t start_idx = 0) noexcept
393+
static void unmangle_name (dynamic_local_string<SENSIBLE_PATH_MAX> &name, size_t start_idx = 0uz) noexcept
394394
{
395395
constexpr size_t mangled_data_size = get_mangled_data_size<IsSatelliteAssembly> ();
396396
if (name.length () <= mangled_data_size) {
@@ -423,8 +423,8 @@ namespace xamarin::android::internal {
423423

424424
bool register_debug_symbols;
425425
bool have_and_want_debug_symbols;
426-
size_t bundled_assembly_index = 0;
427-
size_t number_of_found_assemblies = 0;
426+
size_t bundled_assembly_index = 0uz;
427+
size_t number_of_found_assemblies = 0uz;
428428

429429
#if defined (DEBUG)
430430
TypeMappingInfo *java_to_managed_maps;
@@ -436,10 +436,10 @@ namespace xamarin::android::internal {
436436

437437
md_mmap_info runtime_config_blob_mmap{};
438438
void *runtime_config_data = nullptr;
439-
size_t runtime_config_data_size = 0;
439+
size_t runtime_config_data_size = 0uz;
440440
bool runtime_config_blob_found = false;
441-
uint32_t number_of_mapped_assembly_stores = 0;
442-
uint32_t number_of_zip_dso_entries = 0;
441+
uint32_t number_of_mapped_assembly_stores = 0u;
442+
uint32_t number_of_zip_dso_entries = 0u;
443443
bool need_to_scan_more_apks = true;
444444

445445
AssemblyStoreIndexEntry *assembly_store_hashes;

src/native/monodroid/jni-remapping.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ JniRemapping::lookup_replacement_type (const char *jniSimpleReference) noexcept
2828
}
2929

3030
size_t ref_len = strlen (jniSimpleReference);
31-
for (size_t i = 0; i < application_config.jni_remapping_replacement_type_count; i++) {
31+
for (size_t i = 0uz; i < application_config.jni_remapping_replacement_type_count; i++) {
3232
JniRemappingTypeReplacementEntry const& entry = jni_remapping_type_replacements[i];
3333

3434
if (equal (entry.name, jniSimpleReference, ref_len)) {
@@ -51,7 +51,7 @@ JniRemapping::lookup_replacement_method_info (const char *jniSourceType, const c
5151
size_t source_type_len = strlen (jniSourceType);
5252

5353
const JniRemappingIndexTypeEntry *type = nullptr;
54-
for (size_t i = 0; i < application_config.jni_remapping_replacement_method_index_entry_count; i++) {
54+
for (size_t i = 0uz; i < application_config.jni_remapping_replacement_method_index_entry_count; i++) {
5555
JniRemappingIndexTypeEntry const& entry = jni_remapping_method_replacement_index[i];
5656

5757
if (!equal (entry.name, jniSourceType, source_type_len)) {
@@ -67,9 +67,9 @@ JniRemapping::lookup_replacement_method_info (const char *jniSourceType, const c
6767
}
6868

6969
size_t method_name_len = strlen (jniMethodName);
70-
size_t signature_len = jniMethodSignature == nullptr ? 0 : strlen (jniMethodSignature);
70+
size_t signature_len = jniMethodSignature == nullptr ? 0uz : strlen (jniMethodSignature);
7171

72-
for (size_t i = 0; i < type->method_count; i++) {
72+
for (size_t i = 0uz; i < type->method_count; i++) {
7373
JniRemappingIndexMethodEntry const& entry = type->methods[i];
7474

7575
if (!equal (entry.name, jniMethodName, method_name_len)) {
@@ -89,7 +89,7 @@ JniRemapping::lookup_replacement_method_info (const char *jniSourceType, const c
8989
sig_end--;
9090
}
9191

92-
if (equal (entry.signature, jniMethodSignature, static_cast<size_t>(sig_end - jniMethodSignature) + 1)) {
92+
if (equal (entry.signature, jniMethodSignature, static_cast<size_t>(sig_end - jniMethodSignature) + 1uz)) {
9393
return &type->methods[i].replacement;
9494
}
9595
}

src/native/monodroid/mono-image-loader.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ namespace xamarin::android::internal {
102102
force_inline static ssize_t find_index (hash_t hash) noexcept
103103
{
104104
ssize_t idx = Search::binary_search (hash, assembly_image_cache_hashes, number_of_cache_index_entries);
105-
return idx >= 0 ? static_cast<ssize_t>(assembly_image_cache_indices[idx]) : -1;
105+
return idx >= 0 ? static_cast<ssize_t>(assembly_image_cache_indices[idx]) : -1z;
106106

107107
}
108108
#endif // def USE_CACHE

src/native/monodroid/monodroid-glue-internal.hh

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ namespace xamarin::android::internal
108108

109109
private:
110110
static constexpr std::string_view base_apk_name { "/base.apk" };
111-
static constexpr size_t SMALL_STRING_PARSE_BUFFER_LEN = 50;
111+
static constexpr size_t SMALL_STRING_PARSE_BUFFER_LEN = 50uz;
112112
static constexpr bool is_running_on_desktop = false;
113113

114114
public:

0 commit comments

Comments
 (0)