Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: add CURL_STRICMP() macro, use _stricmp() on Windows #15788

Closed
wants to merge 6 commits into from

Conversation

vszakats
Copy link
Member

@vszakats vszakats commented Dec 20, 2024

Add CURL_STRICMP() macro that works on all platforms depending on
which lib C function is available.

Make sure to always use _stricmp() on Windows, which is the
non-deprecated, official API for this on this platform. Before this
patch it used a MinGW-specific call, or a deprecated compatibility
wrapper with MSVC.

Drop stricmp variant detections on Windows with autotools.

https://learn.microsoft.com/cpp/c-runtime-library/reference/stricmp-wcsicmp-mbsicmp-stricmp-l-wcsicmp-l-mbsicmp-l

Ref: #15652

src/tool_util.c Outdated
@@ -181,7 +181,9 @@ int struplocompare(const char *p1, const char *p2)
return p2 ? -1 : 0;
if(!p2)
return 1;
#ifdef HAVE_STRCASECMP
#if defined(_WIN32)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems clearer to readers to use #ifdef HAVE_STRICMP here. Then we don't need to know that this is Windows specific when reading the code.

Copy link
Member

@bagder bagder Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a policy, we check for features in ifdefs rather than OSes etc so that when something else at some point starts to use this feature it will just automatically work.

Copy link
Member Author

@vszakats vszakats Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Windows stricmp is a deprecated function. Depending on
toolchain, at the moment one of it's many synonyms are detected
and used, sometimes wrongly. _stricmp is the Windows-specific
name that's always guarenteed to exist.

We already do _WIN32 checks at a lot of similar places. I'm not
sure I understand the issue for this specific case.

Should I detect _stricmp with all the dance at build-level, just to
have HAVE__STRICMP here? That would at best wasting configure
time and adding complexity, unless there ever comes up a non-Windows
platform which uses _stricmp. (Apple was one of these where it
turned out to be an elaborate upstream bug causing mis-detections:
8b76a8a #15526
93e6e4b #15559)

Copy link
Member

@bagder bagder Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally prefer us doing ifdef [feature] even if that feature is a unique feature for a single operating system, rather than ifdef [operatingsystem].

I know we do win32 checks in many places, but that is generally the wrong approach and we should strive towards avoiding them when we can easily do so.

Copy link
Member Author

@vszakats vszakats Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or perhaps add a global CURL_STRICMP() that maps to the
correct implementation at a single place? And use that in
struplocompare() and other, future, places?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have to "detect" the feature any more than we detect other windows-specific functions. We can just say "if windows", set this define. Like we do for many other features.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or perhaps add a global CURL_STRICMP() that maps to the correct implementation at a single place?

Sounds like a good idea

Copy link
Member Author

@vszakats vszakats Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally I agree, feature-check is peferred to OS-check.

But, I'm not sure this helps this particular case, where
feature is exclusive to operatingsystem.

And the feature is one of several detected alternate options, so
checking order becomes significant without the code telling this
story. Plus the added busywork up the chain.

It seems clearer and simpler to shortcut _WIN32 in this case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The macro way works for me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this helps this particular case, where feature is exclusive to operatingsystem.

The things is that when done correctly, we don't know that it is unique to a single OS and we don't have to care. When we want to build curl on the new OS ABC tomorrow that has this function, having done the code correctly based on features then makes it an easier ride.

@vszakats vszakats changed the title windows: use _stricmp() src: add CURL_STRICMP() macro, use _stricmp() on Windows Dec 21, 2024
@vszakats vszakats closed this in 6dacd2f Dec 23, 2024
@vszakats vszakats deleted the w-_stricmp branch December 23, 2024 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants