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

MSVC warnings: narrowing conversion, signed/unsigned mismatch #446

Closed
nickhutchinson opened this issue Dec 14, 2020 · 1 comment
Closed

Comments

@nickhutchinson
Copy link
Contributor

Description

The following in doctest.cpp can cause MSVC to raise C4245 (signed/unsigned mismatch) and C4838 (narrowing conversion). This is because EXCEPTION_ILLEGAL_INSTRUCTION and friends may be defined as either a DWORD (unsigned long) or NTSTATUS (signed long) depending on whether ntstatus.h is included.

Simplest solution would be to static_cast to DWORD.

    struct SignalDefs
    {
        DWORD id;
        const char* name;
    };
    // ...
    SignalDefs signalDefs[] = {
            {EXCEPTION_ILLEGAL_INSTRUCTION, "SIGILL - Illegal instruction signal"},
            {EXCEPTION_STACK_OVERFLOW, "SIGSEGV - Stack overflow"},
            {EXCEPTION_ACCESS_VIOLATION, "SIGSEGV - Segmentation violation signal"},
            {EXCEPTION_INT_DIVIDE_BY_ZERO, "Divide by zero error"},
    };
// winnt.h (included by windows.h)
#define STATUS_ILLEGAL_INSTRUCTION ((DWORD)0xC000001DL)

// ntstatus.h
#define STATUS_ILLEGAL_INSTRUCTION ((NTSTATUS)0xC000001DL)  

// minwinbase.h (included by windows.h)
#define EXCEPTION_ILLEGAL_INSTRUCTION STATUS_ILLEGAL_INSTRUCTION

Steps to reproduce

#define WIN32_NO_STATUS // suppress macro redefinition warnings
#include <windows.h>
#undef WIN32_NO_STATUS
#include <ntstatus.h>

#include <doctest.h>

Extra information

  • doctest version: v2.4.1
  • Operating System: Windows
  • Compiler+version: MSVC 19.28, Windows SDK 10.0.19041.0
nickhutchinson added a commit to nickhutchinson/doctest that referenced this issue Dec 14, 2020
`EXCEPTION_ILLEGAL_INSTRUCTION` and friends may be either typed as `DWORD`
(`unsigned long`) or NTSTATUS (long) depending on whether `ntstatus`.h is
included.

Add explicit casts to `DWORD` to silence MSVC warnings about narrowing
conversions or signed/unsigned mismatches.

Fixes doctest#446.
nickhutchinson added a commit to nickhutchinson/doctest that referenced this issue Dec 14, 2020
`EXCEPTION_ILLEGAL_INSTRUCTION` and friends may be either typed as `DWORD`
(`unsigned long`) or NTSTATUS (long) depending on whether `ntstatus`.h is
included.

Add explicit casts to `DWORD` to silence MSVC warnings about narrowing
conversions or signed/unsigned mismatches.

Fixes doctest#446.
nickhutchinson added a commit to nickhutchinson/doctest that referenced this issue Dec 14, 2020
`EXCEPTION_ILLEGAL_INSTRUCTION` and friends may be either typed as `DWORD`
(`unsigned long`) or `NTSTATUS` (`long`) depending on whether `ntstatus.h` is
included.

Add explicit casts to `DWORD` to silence MSVC warnings about narrowing
conversions or signed/unsigned mismatches.

Fixes doctest#446.
onqtam pushed a commit that referenced this issue Dec 15, 2020
`EXCEPTION_ILLEGAL_INSTRUCTION` and friends may be either typed as `DWORD`
(`unsigned long`) or `NTSTATUS` (`long`) depending on whether `ntstatus.h` is
included.

Add explicit casts to `DWORD` to silence MSVC warnings about narrowing
conversions or signed/unsigned mismatches.

Fixes #446.
@onqtam
Copy link
Member

onqtam commented Dec 15, 2020

Thanks for reporting! I just merged the PR - closing this now. Will release an official version..."soon"!

@onqtam onqtam closed this as completed Dec 15, 2020
onqtam pushed a commit that referenced this issue Dec 15, 2020
`EXCEPTION_ILLEGAL_INSTRUCTION` and friends may be either typed as `DWORD`
(`unsigned long`) or `NTSTATUS` (`long`) depending on whether `ntstatus.h` is
included.

Add explicit casts to `DWORD` to silence MSVC warnings about narrowing
conversions or signed/unsigned mismatches.

Fixes #446.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants