Skip to content

Don't force MBEDTLS_PLATFORM_SNPRINTF_ALT on Windows in check_config.h #3453

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

Conversation

gilles-peskine-arm
Copy link
Contributor

check_config.h forces MBEDTLS_PLATFORM_SNPRINTF_ALT and MBEDTLS_PLATFORM_VSNPRINTF_ALT to be defined on Windows. The reason for this is that the native (v)snprintf on older Windows runtimes (up to Visual Studio 2015: _MSVC_VER <= 1900) does not correctly zero-terminate the buffer. This is documented in platform.h and tested in selftest.c. To work around this bug, the library defines wrappers mbedtls_platform_win32_snprintfandmbedtls_platform_win32_vsnprintfinplatform.cand uses them as the value ofmbedtls_snprintfandmbedtls_vsnprintf`.

Even if MBEDTLS_PLATFORM_{V,}SNPRINTF_ALT is not defined, the macro mbedtls_{v,}snprintf expands to the corresponding wrapper function on platforms where it is necessary. Therefore modifying the configuration to set the xxxSNPRINTF_ALT symbols is not actually necessary. Don't do it.

With this change, check_config.h is truly a configuration check, as the name implies, and does not modify the configuration.

Here's a CI run with an additional patch that removes the wrapper altogether, to validate that the wrapper is getting used in this pull request: https://jenkins-internal.mbed.com/job/mbedtls-release-new/740/

  • VS 2015 and VS 2017 builds pass.
  • The mingw tests fail as expected because the test suite contains a test for snprintf as well.
  • I expected the selftest on VS 2013 to fail, but that actually doesn't build because VS 2013 (at least the way we set it up) doesn't have enough C99 to declare snprintf (only vsnprintf). Never mind, the failure of snprintf on mingw shows that we do have a test that would catch the absence of the wrapper if it was necessary.

I don't see a need to backport this. The only reason I want to make this change is as a precursor to changing the way check_config.h is included, as done in #1999. That would be too disruptive for LTS branches.

check_config.h forces MBEDTLS_PLATFORM_SNPRINTF_ALT and
MBEDTLS_PLATFORM_VSNPRINTF_ALT to be defined on Windows. The reason
for this is that the native (v)snprintf on older Windows runtimes (up
to Visual Studio 2015: _MSVC_VER <= 1900) does not correctly
zero-terminate the buffer. This is documented in platform.h and tested
in selftest.c. To work around this bug, the library defines wrappers
mbedtls_platform_win32_snprintf and mbedtls_platform_win32_vsnprintf
in platform.c and uses them as the value of mbedtls_snprintf and
mbedtls_vsnprintf.

Even if MBEDTLS_PLATFORM_{V,}SNPRINTF_ALT is not defined,
the macro mbedtls_{v,}snprintf expands to the corresponding wrapper
function on platforms where it is necessary. Therefore modifying the
configuration to set the xxxSNPRINTF_ALT symbols is not actually
necessary. Don't do it.

With this change, check_config.h is truly a configuration _check_, as
the name implies, and does not modify the configuration.

Move the requirement for MBEDTLS_PLATFORM_C on Windows platforms to
platform.h, and limit it to platforms where the snprintf wrapper is
necessary, since this is the only reason why the platform module is
necessary.

Signed-off-by: Gilles Peskine <[email protected]>
@gilles-peskine-arm gilles-peskine-arm added enhancement needs-review Every commit must be reviewed by at least two team members, component-platform Portability layer and build scripts labels Jun 24, 2020
@gilles-peskine-arm gilles-peskine-arm requested a review from mpg June 24, 2020 13:28
@gilles-peskine-arm gilles-peskine-arm self-assigned this Jun 24, 2020
@gilles-peskine-arm gilles-peskine-arm added needs-work needs-ci Needs to pass CI tests and removed needs-review Every commit must be reviewed by at least two team members, labels Jun 24, 2020
@gilles-peskine-arm
Copy link
Contributor Author

The CI jobs Windows-2013 and Windows-mingw pass but win32-mingw, win32-msvc12_64 and win32_msvc12_32 complain about a broken snprintf. Back to the drawing board.

@gilles-peskine-arm gilles-peskine-arm removed the request for review from mpg June 24, 2020 14:48
@daverodgman daverodgman added the needs-reviewer This PR needs someone to pick it up for review label Oct 2, 2020
@gilles-peskine-arm gilles-peskine-arm removed needs-ci Needs to pass CI tests needs-reviewer This PR needs someone to pick it up for review labels Oct 6, 2020
@daverodgman daverodgman added good-first-issue Good for newcomers needs-adoption stalled PR that someone should pick up and complete labels Jul 6, 2021
@gilles-peskine-arm
Copy link
Contributor Author

I am closing this pull request because it isn't working, I don't have much expertise on the topic (Windows) and I have no time to investigate further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component-platform Portability layer and build scripts enhancement good-first-issue Good for newcomers needs-adoption stalled PR that someone should pick up and complete needs-work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants