Skip to content

swprintf does not reset errno under Visual Studio 2013 #614

Closed
@mihaitodor

Description

I have encountered an issue with errno remaining set to ERANGE when using this library under Windows. It turns out that the following code:

#define _CRT_SECURE_NO_WARNINGS
#include <cstdlib>
#include <cstdio>

int main()
{
    int i = 10;
    wchar_t wrong[1]{};
    auto n1 = swprintf(wrong, 1, L"wrong %d", i);
    printf("%d %d\n", n1, errno);

    wchar_t right[100]{};
    auto n2 = swprintf(right, 100, L"fine %d", i);
    printf("%d %d\n", n2, errno);
}

when compiled with Visual Studio 2013 will output:

-1 34
7 34

and when compiled with Visual Studio 2017 will output:

-1 0
7 0

According to the documentation:

If execution is allowed to continue, these functions return -1 and set errno to EINVAL.

And, looking into the documentation for errno:

Always clear errno by calling _set_errno(0) immediately before a call that may set it, and check it immediately after the call.

Given this inconsistent behaviour, I am wondering if fmtlib should attempt to call _set_errno(0) after invoking such functions.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions