From ff126f1eb7f95417b50cb3d45768d41de00526e1 Mon Sep 17 00:00:00 2001 From: Ahson Khan Date: Tue, 18 May 2021 13:49:53 -0700 Subject: [PATCH] Explore removing use of `int` within header files to align with guidelines. (#2276) Part of https://github.com/Azure/azure-sdk-for-cpp/issues/2226 Trying to focus mainly on headers, depending on the conclusion, we'd want to make cpp file changes as well, but that can be done later. Assuming this is the guideline we want: https://azure.github.io/azure-sdk/cpp_implementation.html#integer-sizes ![image](https://user-images.githubusercontent.com/6527137/118341184-00d52580-b4d3-11eb-8e96-937cb5899c92.png) Some places that we still use `int` is for things like unix file desriptor or where APIs like BIO_read from OpenSSL returns an int. Are those exceptions OK? https://github.com/Azure/azure-sdk-for-cpp/blob/0b64ab3845d6850bc1666337f21640899e732ef5/sdk/core/azure-core/inc/azure/core/io/body_stream.hpp#L166 https://github.com/Azure/azure-sdk-for-cpp/blob/308e5363adaa00ace2f06ff0f1a31747a0a28062/sdk/core/azure-core/src/base64.cpp#L87 --- sdk/core/azure-core/inc/azure/core/uuid.hpp | 7 ++++--- .../azure-core/src/http/curl/curl_connection_private.hpp | 8 ++++---- sdk/core/azure-core/src/private/curl_connection.hpp | 8 ++++---- sdk/core/azure-core/src/private/package_version.hpp | 8 +++++--- sdk/core/azure-core/test/ut/operation_test.hpp | 2 +- .../azure-identity/src/private/package_version.hpp | 8 +++++--- .../src/private/package_version.hpp | 8 +++++--- .../src/private/package_version.hpp | 8 +++++--- 8 files changed, 33 insertions(+), 24 deletions(-) diff --git a/sdk/core/azure-core/inc/azure/core/uuid.hpp b/sdk/core/azure-core/inc/azure/core/uuid.hpp index 50e1ec9f4..8d198e25b 100644 --- a/sdk/core/azure-core/inc/azure/core/uuid.hpp +++ b/sdk/core/azure-core/inc/azure/core/uuid.hpp @@ -28,7 +28,7 @@ namespace Azure { namespace Core { class Uuid final { private: - static constexpr int UuidSize = 16; + static constexpr size_t UuidSize = 16; uint8_t m_uuid[UuidSize]; // The UUID reserved variants. @@ -86,13 +86,14 @@ namespace Azure { namespace Core { #if defined(AZ_PLATFORM_WINDOWS) std::random_device rd; - for (int i = 0; i < UuidSize; i += 4) + for (size_t i = 0; i < UuidSize; i += 4) { const uint32_t x = rd(); std::memcpy(uuid + i, &x, 4); } #elif defined(AZ_PLATFORM_POSIX) - int ret = RAND_bytes(uuid, UuidSize); + // This static cast is safe since we know Uuid size, which is a const, will always fit an int. + int ret = RAND_bytes(uuid, static_cast(UuidSize)); if (ret <= 0) { abort(); diff --git a/sdk/core/azure-core/src/http/curl/curl_connection_private.hpp b/sdk/core/azure-core/src/http/curl/curl_connection_private.hpp index 04a33e0f6..485add350 100644 --- a/sdk/core/azure-core/src/http/curl/curl_connection_private.hpp +++ b/sdk/core/azure-core/src/http/curl/curl_connection_private.hpp @@ -25,14 +25,14 @@ namespace Azure { namespace Core { namespace Http { // Run time error template constexpr static const char* DefaultFailedToGetNewConnectionTemplate = "Fail to get a new connection for: "; - constexpr static int DefaultMaxOpenNewConnectionIntentsAllowed = 10; + constexpr static int32_t DefaultMaxOpenNewConnectionIntentsAllowed = 10; // After 3 connections are received from the pool and failed to send a request, the next // connections would ask the pool to be clean and spawn new connection. - constexpr static int RequestPoolResetAfterConnectionFailed = 3; + constexpr static int32_t RequestPoolResetAfterConnectionFailed = 3; // 90 sec -> cleaner wait time before next clean routine - constexpr static int DefaultCleanerIntervalMilliseconds = 1000 * 90; + constexpr static int32_t DefaultCleanerIntervalMilliseconds = 1000 * 90; // 60 sec -> expired connection is when it waits for 60 sec or more and it's not re-used - constexpr static int DefaultConnectionExpiredMilliseconds = 1000 * 60; + constexpr static int32_t DefaultConnectionExpiredMilliseconds = 1000 * 60; // Define the maximun allowed connections per host-index in the pool. If this number is reached // for the host-index, next connections trying to be added to the pool will be ignored. constexpr static size_t MaxConnectionsPerIndex = 1024; diff --git a/sdk/core/azure-core/src/private/curl_connection.hpp b/sdk/core/azure-core/src/private/curl_connection.hpp index 04a33e0f6..485add350 100644 --- a/sdk/core/azure-core/src/private/curl_connection.hpp +++ b/sdk/core/azure-core/src/private/curl_connection.hpp @@ -25,14 +25,14 @@ namespace Azure { namespace Core { namespace Http { // Run time error template constexpr static const char* DefaultFailedToGetNewConnectionTemplate = "Fail to get a new connection for: "; - constexpr static int DefaultMaxOpenNewConnectionIntentsAllowed = 10; + constexpr static int32_t DefaultMaxOpenNewConnectionIntentsAllowed = 10; // After 3 connections are received from the pool and failed to send a request, the next // connections would ask the pool to be clean and spawn new connection. - constexpr static int RequestPoolResetAfterConnectionFailed = 3; + constexpr static int32_t RequestPoolResetAfterConnectionFailed = 3; // 90 sec -> cleaner wait time before next clean routine - constexpr static int DefaultCleanerIntervalMilliseconds = 1000 * 90; + constexpr static int32_t DefaultCleanerIntervalMilliseconds = 1000 * 90; // 60 sec -> expired connection is when it waits for 60 sec or more and it's not re-used - constexpr static int DefaultConnectionExpiredMilliseconds = 1000 * 60; + constexpr static int32_t DefaultConnectionExpiredMilliseconds = 1000 * 60; // Define the maximun allowed connections per host-index in the pool. If this number is reached // for the host-index, next connections trying to be added to the pool will be ignored. constexpr static size_t MaxConnectionsPerIndex = 1024; diff --git a/sdk/core/azure-core/src/private/package_version.hpp b/sdk/core/azure-core/src/private/package_version.hpp index 3f846eb1c..f056f8d62 100644 --- a/sdk/core/azure-core/src/private/package_version.hpp +++ b/sdk/core/azure-core/src/private/package_version.hpp @@ -8,6 +8,8 @@ #pragma once +#include + #define AZURE_CORE_VERSION_MAJOR 1 #define AZURE_CORE_VERSION_MINOR 0 #define AZURE_CORE_VERSION_PATCH 0 @@ -24,13 +26,13 @@ namespace Azure { namespace Core { namespace _detail { class PackageVersion final { public: /// Major numeric identifier. - static constexpr int Major = AZURE_CORE_VERSION_MAJOR; + static constexpr int32_t Major = AZURE_CORE_VERSION_MAJOR; /// Minor numeric identifier. - static constexpr int Minor = AZURE_CORE_VERSION_MINOR; + static constexpr int32_t Minor = AZURE_CORE_VERSION_MINOR; /// Patch numeric identifier. - static constexpr int Patch = AZURE_CORE_VERSION_PATCH; + static constexpr int32_t Patch = AZURE_CORE_VERSION_PATCH; /// Indicates whether the SDK is in a pre-release state. static constexpr bool IsPreRelease = sizeof(AZURE_CORE_VERSION_PRERELEASE) != sizeof(""); diff --git a/sdk/core/azure-core/test/ut/operation_test.hpp b/sdk/core/azure-core/test/ut/operation_test.hpp index 802218558..b585eeb79 100644 --- a/sdk/core/azure-core/test/ut/operation_test.hpp +++ b/sdk/core/azure-core/test/ut/operation_test.hpp @@ -21,7 +21,7 @@ namespace Azure { namespace Core { namespace Test { private: std::string m_operationToken; std::string m_value; - int m_count = 0; + int32_t m_count = 0; private: std::unique_ptr PollInternal(Context const&) override diff --git a/sdk/identity/azure-identity/src/private/package_version.hpp b/sdk/identity/azure-identity/src/private/package_version.hpp index 15a40f54d..f0d3447b1 100644 --- a/sdk/identity/azure-identity/src/private/package_version.hpp +++ b/sdk/identity/azure-identity/src/private/package_version.hpp @@ -8,6 +8,8 @@ #pragma once +#include + #define AZURE_IDENTITY_VERSION_MAJOR 1 #define AZURE_IDENTITY_VERSION_MINOR 0 #define AZURE_IDENTITY_VERSION_PATCH 0 @@ -24,13 +26,13 @@ namespace Azure { namespace Identity { namespace _detail { class PackageVersion final { public: /// Major numeric identifier. - static constexpr int Major = AZURE_IDENTITY_VERSION_MAJOR; + static constexpr int32_t Major = AZURE_IDENTITY_VERSION_MAJOR; /// Minor numeric identifier. - static constexpr int Minor = AZURE_IDENTITY_VERSION_MINOR; + static constexpr int32_t Minor = AZURE_IDENTITY_VERSION_MINOR; /// Patch numeric identifier. - static constexpr int Patch = AZURE_IDENTITY_VERSION_PATCH; + static constexpr int32_t Patch = AZURE_IDENTITY_VERSION_PATCH; /// Indicates whether the SDK is in a pre-release state. static constexpr bool IsPreRelease = sizeof(AZURE_IDENTITY_VERSION_PRERELEASE) != sizeof(""); diff --git a/sdk/keyvault/azure-security-keyvault-common/src/private/package_version.hpp b/sdk/keyvault/azure-security-keyvault-common/src/private/package_version.hpp index ecead5256..5ab12ca06 100644 --- a/sdk/keyvault/azure-security-keyvault-common/src/private/package_version.hpp +++ b/sdk/keyvault/azure-security-keyvault-common/src/private/package_version.hpp @@ -8,6 +8,8 @@ #pragma once +#include + #define AZURE_SECURITY_KEYVAULT_COMMON_VERSION_MAJOR 4 #define AZURE_SECURITY_KEYVAULT_COMMON_VERSION_MINOR 0 #define AZURE_SECURITY_KEYVAULT_COMMON_VERSION_PATCH 0 @@ -25,13 +27,13 @@ namespace Azure { namespace Security { namespace KeyVault { namespace Common { n class PackageVersion final { public: /// Major numeric identifier. - static constexpr int Major = AZURE_SECURITY_KEYVAULT_COMMON_VERSION_MAJOR; + static constexpr int32_t Major = AZURE_SECURITY_KEYVAULT_COMMON_VERSION_MAJOR; /// Minor numeric identifier. - static constexpr int Minor = AZURE_SECURITY_KEYVAULT_COMMON_VERSION_MINOR; + static constexpr int32_t Minor = AZURE_SECURITY_KEYVAULT_COMMON_VERSION_MINOR; /// Patch numeric identifier. - static constexpr int Patch = AZURE_SECURITY_KEYVAULT_COMMON_VERSION_PATCH; + static constexpr int32_t Patch = AZURE_SECURITY_KEYVAULT_COMMON_VERSION_PATCH; /// Indicates whether the SDK is in a pre-release state. static constexpr bool IsPreRelease diff --git a/sdk/keyvault/azure-security-keyvault-keys/src/private/package_version.hpp b/sdk/keyvault/azure-security-keyvault-keys/src/private/package_version.hpp index 20660221e..7dca16762 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/src/private/package_version.hpp +++ b/sdk/keyvault/azure-security-keyvault-keys/src/private/package_version.hpp @@ -8,6 +8,8 @@ #pragma once +#include + #define AZURE_SECURITY_KEYVAULT_KEYS_VERSION_MAJOR 4 #define AZURE_SECURITY_KEYVAULT_KEYS_VERSION_MINOR 0 #define AZURE_SECURITY_KEYVAULT_KEYS_VERSION_PATCH 0 @@ -25,13 +27,13 @@ namespace Azure { namespace Security { namespace KeyVault { namespace Keys { nam class PackageVersion final { public: /// Major numeric identifier. - static constexpr int Major = AZURE_SECURITY_KEYVAULT_KEYS_VERSION_MAJOR; + static constexpr int32_t Major = AZURE_SECURITY_KEYVAULT_KEYS_VERSION_MAJOR; /// Minor numeric identifier. - static constexpr int Minor = AZURE_SECURITY_KEYVAULT_KEYS_VERSION_MINOR; + static constexpr int32_t Minor = AZURE_SECURITY_KEYVAULT_KEYS_VERSION_MINOR; /// Patch numeric identifier. - static constexpr int Patch = AZURE_SECURITY_KEYVAULT_KEYS_VERSION_PATCH; + static constexpr int32_t Patch = AZURE_SECURITY_KEYVAULT_KEYS_VERSION_PATCH; /// Indicates whether the SDK is in a pre-release state. static constexpr bool IsPreRelease