Skip to content

Commit

Permalink
Guard against Windows' min/max macros (#5348)
Browse files Browse the repository at this point in the history
* Guard against Windows' min/max macros

* Add NOMINMAX and LEAN_AND_MEAN + clang-format

* clang-format

---------

Co-authored-by: Anton Kolesnyk <[email protected]>
  • Loading branch information
antkmsft and antkmsft authored Feb 13, 2024
1 parent 0f7d02b commit 9271d13
Show file tree
Hide file tree
Showing 60 changed files with 151 additions and 122 deletions.
2 changes: 1 addition & 1 deletion sdk/core/azure-core-amqp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ An AMQP Message Sender is responsible for sending messages to an AMQP server ove
senderOptions.Name = "sender-link";
senderOptions.MessageSource = "source";
senderOptions.SettleMode = Azure::Core::Amqp::_internal::SenderSettleMode::Unsettled;
senderOptions.MaxMessageSize = std::numeric_limits<uint16_t>::max();
senderOptions.MaxMessageSize = (std::numeric_limits<uint16_t>::max)();

Azure::Core::Amqp::_internal::MessageSender sender(
session, credentials->GetEntityPath(), senderOptions, nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ namespace Azure { namespace Core { namespace Amqp { namespace _internal {
* @remarks The maximum frame size must be at least 512 bytes. The default value is the maximum
* value for a uint32.
*/
uint32_t MaxFrameSize{std::numeric_limits<uint32_t>::max()};
uint32_t MaxFrameSize{(std::numeric_limits<uint32_t>::max)()};

/** @brief The maximum number of channels supported.
*
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/azure-core-amqp/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Demonstrates writing messages to the Azure Event Hubs service using the AMQP pro
senderOptions.Name = "sender-link";
senderOptions.MessageSource = "source";
senderOptions.SettleMode = Azure::Core::Amqp::_internal::SenderSettleMode::Unsettled;
senderOptions.MaxMessageSize = std::numeric_limits<uint16_t>::max();
senderOptions.MaxMessageSize = (std::numeric_limits<uint16_t>::max)();

Azure::Core::Amqp::_internal::MessageSender sender(
session, credentials->GetEntityPath(), senderOptions, nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ int main()
credentials->GetHostName(), credentials->GetPort(), credentials, connectionOptions);

Azure::Core::Amqp::_internal::SessionOptions sessionOptions;
sessionOptions.InitialIncomingWindowSize = std::numeric_limits<int32_t>::max();
sessionOptions.InitialOutgoingWindowSize = std::numeric_limits<uint16_t>::max();
sessionOptions.InitialIncomingWindowSize = (std::numeric_limits<int32_t>::max)();
sessionOptions.InitialOutgoingWindowSize = (std::numeric_limits<uint16_t>::max)();
Azure::Core::Amqp::_internal::Session session(connection);

// @begin_snippet: CreateSender
Azure::Core::Amqp::_internal::MessageSenderOptions senderOptions;
senderOptions.Name = "sender-link";
senderOptions.MessageSource = "source";
senderOptions.SettleMode = Azure::Core::Amqp::_internal::SenderSettleMode::Unsettled;
senderOptions.MaxMessageSize = std::numeric_limits<uint16_t>::max();
senderOptions.MaxMessageSize = (std::numeric_limits<uint16_t>::max)();

Azure::Core::Amqp::_internal::MessageSender sender(
session, credentials->GetEntityPath(), senderOptions, nullptr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ int main()

// Establish a session to the eventhub.
Azure::Core::Amqp::_internal::SessionOptions sessionOptions;
sessionOptions.InitialIncomingWindowSize = std::numeric_limits<int32_t>::max();
sessionOptions.InitialOutgoingWindowSize = std::numeric_limits<uint16_t>::max();
sessionOptions.InitialIncomingWindowSize = (std::numeric_limits<int32_t>::max)();
sessionOptions.InitialOutgoingWindowSize = (std::numeric_limits<uint16_t>::max)();
Azure::Core::Amqp::_internal::Session session(connection.CreateSession(sessionOptions));

auto eventHubProperties = GetEventHubProperties(session, eventhubsEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int main()
receiverOptions.Name = "receiver-link";
receiverOptions.MessageTarget = "ingress-rx";
receiverOptions.SettleMode = Azure::Core::Amqp::_internal::ReceiverSettleMode::First;
receiverOptions.MaxMessageSize = std::numeric_limits<uint16_t>::max();
receiverOptions.MaxMessageSize = (std::numeric_limits<uint16_t>::max)();

Azure::Core::Amqp::_internal::MessageReceiver receiver(session, hostUrl, receiverOptions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int main()
receiverOptions.Name = "receiver-link";
receiverOptions.MessageTarget = "ingress-rx";
receiverOptions.SettleMode = Azure::Core::Amqp::_internal::ReceiverSettleMode::First;
receiverOptions.MaxMessageSize = std::numeric_limits<uint16_t>::max();
receiverOptions.MaxMessageSize = (std::numeric_limits<uint16_t>::max)();
receiverOptions.EnableTrace = true;

Azure::Core::Amqp::_internal::MessageReceiver receiver(session.CreateMessageReceiver(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ int main()
credential->GetHostName(), credential, connectionOptions);

Azure::Core::Amqp::_internal::SessionOptions sessionOptions;
sessionOptions.InitialIncomingWindowSize = std::numeric_limits<int32_t>::max();
sessionOptions.InitialOutgoingWindowSize = std::numeric_limits<uint16_t>::max();
sessionOptions.InitialIncomingWindowSize = (std::numeric_limits<int32_t>::max)();
sessionOptions.InitialOutgoingWindowSize = (std::numeric_limits<uint16_t>::max)();

Azure::Core::Amqp::_internal::Session session(connection.CreateSession(sessionOptions));

Expand All @@ -44,7 +44,7 @@ int main()
senderOptions.Name = "sender-link";
senderOptions.MessageSource = "ingress";
senderOptions.SettleMode = Azure::Core::Amqp::_internal::SenderSettleMode::Settled;
senderOptions.MaxMessageSize = std::numeric_limits<uint16_t>::max();
senderOptions.MaxMessageSize = (std::numeric_limits<uint16_t>::max)();
Azure::Core::Amqp::_internal::MessageSender sender(
session.CreateMessageSender(entityPath, senderOptions, nullptr));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ int main()
receiverOptions.Name = "receiver-link";
receiverOptions.MessageTarget = "ingress-rx";
receiverOptions.SettleMode = Azure::Core::Amqp::_internal::ReceiverSettleMode::First;
receiverOptions.MaxMessageSize = std::numeric_limits<uint16_t>::max();
receiverOptions.MaxMessageSize = (std::numeric_limits<uint16_t>::max)();
receiverOptions.EnableTrace = true;

Azure::Core::Amqp::_internal::MessageReceiver receiver(session.CreateMessageReceiver(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ int main()
Azure::Core::Amqp::_internal::Connection connection(eventhubsHost, credential, connectionOptions);

Azure::Core::Amqp::_internal::SessionOptions sessionOptions;
sessionOptions.InitialIncomingWindowSize = std::numeric_limits<int32_t>::max();
sessionOptions.InitialOutgoingWindowSize = std::numeric_limits<uint16_t>::max();
sessionOptions.InitialIncomingWindowSize = (std::numeric_limits<int32_t>::max)();
sessionOptions.InitialOutgoingWindowSize = (std::numeric_limits<uint16_t>::max)();

Azure::Core::Amqp::_internal::Session session(connection.CreateSession(sessionOptions));

Expand All @@ -52,7 +52,7 @@ int main()
message.SetBody(Azure::Core::Amqp::Models::AmqpValue{"Hello"});

Azure::Core::Amqp::_internal::MessageSenderOptions senderOptions;
senderOptions.MaxMessageSize = std::numeric_limits<uint16_t>::max();
senderOptions.MaxMessageSize = (std::numeric_limits<uint16_t>::max)();
senderOptions.MessageSource = "ingress";
senderOptions.Name = "sender-link";
senderOptions.SettleMode = Azure::Core::Amqp::_internal::SenderSettleMode::Settled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ int main()
credentials->GetHostName(), connectionOptions);

Azure::Core::Amqp::_internal::SessionOptions sessionOptions;
sessionOptions.InitialIncomingWindowSize = std::numeric_limits<int32_t>::max();
sessionOptions.InitialOutgoingWindowSize = std::numeric_limits<uint16_t>::max();
sessionOptions.InitialIncomingWindowSize = (std::numeric_limits<int32_t>::max)();
sessionOptions.InitialOutgoingWindowSize = (std::numeric_limits<uint16_t>::max)();

Azure::Core::Amqp::_internal::Session session(connection, credentials, sessionOptions);

Expand All @@ -36,7 +36,7 @@ int main()
senderOptions.Name = "sender-link";
senderOptions.MessageSource = "ingress";
senderOptions.SettleMode = Azure::Core::Amqp::_internal::SenderSettleMode::Unsettled;
senderOptions.MaxMessageSize = std::numeric_limits<uint16_t>::max();
senderOptions.MaxMessageSize = (std::numeric_limits<uint16_t>::max)();
Azure::Core::Amqp::_internal::MessageSender sender(
session, credentials->GetEntityPath(), senderOptions, nullptr);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ int main()
Azure::Core::Amqp::_internal::Connection connection("localhost", nullptr, connectionOptions);

Azure::Core::Amqp::_internal::SessionOptions sessionOptions;
sessionOptions.InitialIncomingWindowSize = std::numeric_limits<int32_t>::max();
sessionOptions.InitialOutgoingWindowSize = std::numeric_limits<uint16_t>::max();
sessionOptions.InitialIncomingWindowSize = (std::numeric_limits<int32_t>::max)();
sessionOptions.InitialOutgoingWindowSize = (std::numeric_limits<uint16_t>::max)();

Azure::Core::Amqp::_internal::Session session = connection.CreateSession(sessionOptions);

Azure::Core::Amqp::_internal::MessageSenderOptions senderOptions;
senderOptions.Name = "sender-link";
senderOptions.MessageSource = "ingress";
senderOptions.MaxMessageSize = std::numeric_limits<uint16_t>::max();
senderOptions.MaxMessageSize = (std::numeric_limits<uint16_t>::max)();
Azure::Core::Amqp::_internal::MessageSender sender{
session.CreateMessageSender("localhost/ingress", senderOptions, nullptr)};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ int main()
Azure::Core::Amqp::_internal::Connection connection("localhost", nullptr, connectionOptions);

Azure::Core::Amqp::_internal::SessionOptions sessionOptions;
sessionOptions.InitialIncomingWindowSize = std::numeric_limits<int32_t>::max();
sessionOptions.InitialOutgoingWindowSize = std::numeric_limits<uint16_t>::max();
sessionOptions.InitialIncomingWindowSize = (std::numeric_limits<int32_t>::max)();
sessionOptions.InitialOutgoingWindowSize = (std::numeric_limits<uint16_t>::max)();

Azure::Core::Amqp::_internal::Session session{connection.CreateSession(sessionOptions)};

Azure::Core::Amqp::_internal::MessageSenderOptions senderOptions;
senderOptions.Name = "sender-link";
senderOptions.MessageSource = "ingress";
senderOptions.MaxMessageSize = std::numeric_limits<uint16_t>::max();
senderOptions.MaxMessageSize = (std::numeric_limits<uint16_t>::max)();
Azure::Core::Amqp::_internal::MessageSender sender{
session.CreateMessageSender("localhost/ingress", senderOptions, nullptr)};

Expand Down
2 changes: 1 addition & 1 deletion sdk/core/azure-core-amqp/src/amqp/message_receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ namespace Azure { namespace Core { namespace Amqp { namespace _detail {
}
else
{
m_link->SetMaxMessageSize(std::numeric_limits<uint64_t>::max());
m_link->SetMaxMessageSize((std::numeric_limits<uint64_t>::max)());
}
if (m_options.MaxLinkCredit != 0)
{
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/azure-core-amqp/src/amqp/message_sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ namespace Azure { namespace Core { namespace Amqp { namespace _detail {
}
else
{
m_link->SetMaxMessageSize(std::numeric_limits<uint64_t>::max());
m_link->SetMaxMessageSize((std::numeric_limits<uint64_t>::max)());
}
if (m_options.MaxLinkCredits != 0)
{
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/azure-core-amqp/test/ut/amqp_value_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,7 @@ template <typename T> T GenerateRandomValue()
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<T> dis(
std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
(std::numeric_limits<T>::min)(), (std::numeric_limits<T>::max)());
return dis(gen);
}

Expand Down
1 change: 1 addition & 0 deletions sdk/core/azure-core-amqp/test/ut/azure_core_amqp_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#if defined(AZ_PLATFORM_WINDOWS)
#if defined(_DEBUG) && defined(_MSC_VER)
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <windows.h>
// MSVC CRT error callback.
// This function is called when a CRT error is detected.
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/azure-core-amqp/test/ut/session_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace Azure { namespace Core { namespace Amqp { namespace Tests {

// Verify defaults are something "reasonable".
EXPECT_EQ(1, session.GetIncomingWindow());
EXPECT_EQ(std::numeric_limits<uint32_t>::max(), session.GetHandleMax());
EXPECT_EQ((std::numeric_limits<uint32_t>::max)(), session.GetHandleMax());
EXPECT_EQ(1, session.GetOutgoingWindow());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ namespace Azure { namespace Core { namespace Test {
{
Core::Credentials::AccessToken accessToken;
accessToken.Token = "magicToken";
accessToken.ExpiresOn = DateTime::max();
accessToken.ExpiresOn = (DateTime::max)();

if (context.IsCancelled() || tokenRequestContext.Scopes.size() == 0)
{
accessToken.ExpiresOn = DateTime::min();
accessToken.ExpiresOn = (DateTime::min)();
}

return accessToken;
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/azure-core/src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Azure::DateTime Azure::Core::Context::GetDeadline() const
{
// Contexts form a tree. Here, we walk from a node all the way back to the root in order to find
// the earliest deadline value.
auto result = DateTime::max();
auto result = (DateTime::max)();
for (auto ptr = m_contextSharedState; ptr; ptr = ptr->Parent)
{
auto deadline = ContextSharedState::FromDateTimeRepresentation(ptr->Deadline);
Expand Down
3 changes: 3 additions & 0 deletions sdk/core/azure-core/src/cryptography/md5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include "azure/core/platform.hpp"

#if defined(AZ_PLATFORM_WINDOWS)
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX

// Windows needs to go before bcrypt
#include <windows.h>

Expand Down
3 changes: 3 additions & 0 deletions sdk/core/azure-core/src/cryptography/sha_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <azure/core/platform.hpp>

#if defined(AZ_PLATFORM_WINDOWS)
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX

// Windows needs to go before bcrypt
#include <windows.h>

Expand Down
14 changes: 7 additions & 7 deletions sdk/core/azure-core/src/datetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ DateTime GetSystemClockEpoch()
DateTime GetMaxDateTime()
{
auto const systemClockMax = std::chrono::duration_cast<DateTime::clock::duration>(
std::chrono::system_clock::time_point::max().time_since_epoch())
(std::chrono::system_clock::time_point::max)().time_since_epoch())
.count();

auto const systemClockEpoch = GetSystemClockEpoch().time_since_epoch().count();

constexpr auto repMax = std::numeric_limits<DateTime::clock::duration::rep>::max();
constexpr auto repMax = (std::numeric_limits<DateTime::clock::duration::rep>::max)();

return DateTime(DateTime::time_point(
DateTime::duration(systemClockMax + std::min(systemClockEpoch, (repMax - systemClockMax)))));
return DateTime(DateTime::time_point(DateTime::duration(
systemClockMax + (std::min)(systemClockEpoch, (repMax - systemClockMax)))));
}

template <typename T>
Expand Down Expand Up @@ -315,7 +315,7 @@ T ParseNumber(
{
if (*cursor + minLength <= strLen)
{
auto const MaxChars = std::min(static_cast<int>(strLen - *cursor), maxLength);
auto const MaxChars = (std::min)(static_cast<int>(strLen - *cursor), maxLength);
int64_t value = 0;
auto i = 0;
for (; i < MaxChars; ++i)
Expand All @@ -335,7 +335,7 @@ T ParseNumber(
break;
}

if (value >= 0 && value <= std::numeric_limits<T>::max())
if (value >= 0 && value <= (std::numeric_limits<T>::max)())
{
*cursor += i;
return static_cast<T>(value);
Expand Down Expand Up @@ -422,7 +422,7 @@ DateTime::DateTime(

DateTime::operator std::chrono::system_clock::time_point() const
{
static DateTime SystemClockMin(std::chrono::system_clock::time_point::min());
static DateTime SystemClockMin((std::chrono::system_clock::time_point::min)());
static DateTime SystemClockMax(GetMaxDateTime());

auto outOfRange = 0;
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/azure-core/src/http/curl/curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ int pollSocketUntilEventOrTimeout(
// Before doing any work, check to make sure that the context hasn't already been cancelled.
context.ThrowIfCancelled();
int pollTimeoutMs = static_cast<int>(
std::min(
(std::min)(
pollInterval, std::chrono::duration_cast<std::chrono::milliseconds>(deadline - now))
.count());
#if defined(AZ_PLATFORM_POSIX)
Expand Down
6 changes: 3 additions & 3 deletions sdk/core/azure-core/src/http/retry_policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ std::chrono::milliseconds CalculateExponentialDelay(
= std::numeric_limits<int32_t>::digits - (std::numeric_limits<int32_t>::is_signed ? 1 : 0);

// Scale exponentially: 1 x RetryDelay on 1st attempt, 2x on 2nd, 4x on 3rd, 8x on 4th ... all the
// way up to std::numeric_limits<int32_t>::max() * RetryDelay.
// way up to (std::numeric_limits<int32_t>::max()) * RetryDelay.
auto exponentialRetryAfter = retryOptions.RetryDelay
* (((attempt - 1) <= beforeLastBit) ? (1 << (attempt - 1))
: std::numeric_limits<int32_t>::max());
: (std::numeric_limits<int32_t>::max()));

// Multiply exponentialRetryAfter by jitterFactor
exponentialRetryAfter = std::chrono::milliseconds(static_cast<std::chrono::milliseconds::rep>(
(std::chrono::duration<double, std::chrono::milliseconds::period>(exponentialRetryAfter)
* jitterFactor)
.count()));

return std::min(exponentialRetryAfter, retryOptions.MaxRetryDelay);
return (std::min)(exponentialRetryAfter, retryOptions.MaxRetryDelay);
}

bool WasLastAttempt(RetryOptions const& retryOptions, int32_t attempt)
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/azure-core/src/http/url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Url::Url(std::string const& url)
// stoi will throw out_of_range when `int` is overflow, but we need to throw if uint16 is
// overflow
{
constexpr auto const MaxPortNumberSupported = std::numeric_limits<uint16_t>::max();
constexpr auto const MaxPortNumberSupported = (std::numeric_limits<uint16_t>::max)();
if (portNumber > MaxPortNumberSupported)
{
throw std::out_of_range(
Expand Down
7 changes: 7 additions & 0 deletions sdk/core/azure-core/src/http/winhttp/win_http_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
#include "azure/core/http/win_http_transport.hpp"
#include "azure/core/url.hpp"

#if !defined(WIN32_LEAN_AND_MEAN)
#define WIN32_LEAN_AND_MEAN
#endif
#if !defined(NOMINMAX)
#define NOMINMAX
#endif

#include <Windows.h>

#include <memory>
Expand Down
7 changes: 7 additions & 0 deletions sdk/core/azure-core/src/http/winhttp/win_http_transport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
#include "win_http_request.hpp"
#endif

#if !defined(WIN32_LEAN_AND_MEAN)
#define WIN32_LEAN_AND_MEAN
#endif
#if !defined(NOMINMAX)
#define NOMINMAX
#endif

#include <Windows.h>

#include <algorithm>
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/azure-core/src/io/body_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ std::vector<uint8_t> BodyStream::ReadToEnd(Context const& context)
size_t MemoryBodyStream::OnRead(uint8_t* buffer, size_t count, Context const& context)
{
(void)context;
size_t copy_length = std::min(count, this->m_length - this->m_offset);
size_t copy_length = (std::min)(count, this->m_length - this->m_offset);
// Copy what's left or just the count
std::memcpy(buffer, this->m_data + m_offset, static_cast<size_t>(copy_length));
// move position
Expand Down
Loading

0 comments on commit 9271d13

Please sign in to comment.