GetDateFormatEx returns a non-zero value but the buffer returned by it is faulty

gupta, shivangi 20 Reputation points
2024-03-14T11:19:20.97+00:00

This is the sample program in which "GetDateFormatEx" returns an invalid string. It is failing in customer's setup.


#include <windows.h>
#include <iostream>
uint64_t
shimTime(void)
{
    FILETIME ts;
    GetSystemTimeAsFileTime(&ts);
    ULONGLONG systime = ((ULARGE_INTEGER*)&ts)->QuadPart;
    return systime;
}
int main() {
    static const size_t sz = 64;
    TCHAR currTimeStr[sz];
    TCHAR currDateStr[sz];
    FILETIME ts;
    int ret = -1;
    // Get current-time in pretty-format
    GetSystemTimeAsFileTime(&ts);
    uint64_t currTime = shimTime();
    SYSTEMTIME  stime, lstime;
    if (currTime == 0) {
        printf("currTime is 0\n");
        swprintf_s(currTimeStr, sz, L"0");
        swprintf_s(currDateStr, sz, L"0");
    }
    else {
        if (!FileTimeToSystemTime((FILETIME*)&currTime, &stime)) {
            printf("Unable to get FileTimeToSystemTime, err=%u\n", GetLastError());
        }
        if (!SystemTimeToTzSpecificLocalTime(NULL, &stime, &lstime)) {
            printf("Unable to get SystemTimeToTzSpecificLocalTime, err=%u\n", GetLastError());
        }
        ret = GetTimeFormatEx(LOCALE_NAME_USER_DEFAULT, 0, &lstime, NULL, currTimeStr, _countof(currTimeStr));
        printf("GetTimeFormatEx ret = %d\n", ret);
        if (!ret) {
            printf("GetTimeFormatEx failed, err=%u\n", GetLastError());
        }
        ret = GetDateFormatEx(LOCALE_NAME_USER_DEFAULT, 0, &lstime, L"ddd',' MMM dd',' yyyy", currDateStr, _countof(currDateStr), NULL);
        printf("GetDateFormatEx ret = %d\n", ret);
        if (!ret) {
            printf("GetDateFormatEx failed, err=%u\n", GetLastError());
        }
    }
    currTimeStr[sz - 1] = '\0';
    currDateStr[sz - 1] = '\0';
    // Print time and date
    printf("Current time is: %S\n", currTimeStr);
    printf("Current date is: %S\n", currDateStr);
    char cp[1024];
    int cursize = sprintf_s(&cp[0], 1023, "ts=%S %S", currTimeStr, currDateStr);
    if (cursize < 0 || cursize >= 1023) {
        printf("Error occurred during sprintf_s, cursize: %u\n", cursize);
    }
    printf("%s\n", cp);
    return 0;
}

All I can see from log is cursize from sprint_f returned is 429496729 and the last print returns "ts=06:21:20 " None of the functions have failed. All are returning non zero values. currDateStr seems to be faulty but GetDateFormatEx has returned a non-zero value.

Details of the system

C:>systeminfo

 

Host Name:                 MIGNT006

OS Name:                   Microsoft Windows Server 2019 Standard

OS Version:                10.0.17763 N/A Build 17763

OS Manufacturer:           Microsoft Corporation

OS Configuration:          Member Server

OS Build Type:             Multiprocessor Free

Registered Owner:          Flex

Registered Organization:   Flex

Product ID:                00429-70000-00000-AA322

Original Install Date:     01/30/2021, 17:11:12

System Boot Time:          01/19/2024, 14:52:23

System Manufacturer:       HP

System Model:              ProLiant DL360 G7

System Type:               x64-based PC

Processor(s):              2 Processor(s) Installed.

BIOS Version:              HP P68, 05/21/2018

Windows Directory:         C:\WINDOWS

System Directory:          C:\WINDOWS\system32

Boot Device:               \Device\HarddiskVolume1

System Locale:             en-us;English (United States)

Input Locale:              en-us;English (United States)

Time Zone:                 (UTC+02:00) Jerusalem

Total Physical Memory:     16,374 MB

Available Physical Memory: 5,051 MB

Virtual Memory: Max Size:  18,806 MB

Virtual Memory: Available: 7,398 MB

Virtual Memory: In Use:    11,408 MB

Page File Location(s):     C:\pagefile.sys

Domain:                    europe.ad.flextronics.com

Logon Server:              \EUMIG002

Hotfix(s):                 31 Hotfix(s) Installed.

Network Card(s):           4 NIC(s) Installed.

                                 Connection Name: Ethernet 3

                                 Status:          Media disconnected

                                 Connection Name: Ethernet 4

                                 DHCP Enabled:    No

                                 IP address(es)

                                 Connection Name: Ethernet 2

                                 Status:          Media disconnected

                                Connection Name: Ethernet

                                 Status:          Media disconnected

Hyper-V Requirements:      VM Monitor Mode Extensions: Yes

                           Virtualization Enabled In Firmware: Yes

                           Second Level Address Translation: Yes

                           Data Execution Prevention Available: Yes

`

Can you please help in figuring out what could be possibly wrong here?

Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,131 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,529 questions
{count} votes

Accepted answer
  1. Minxin Yu 10,031 Reputation points Microsoft Vendor
    2024-03-18T09:00:48.1966667+00:00

    Hi, @gupta, shivangi

    As Viorel said, sprintf_s and printf cannot convert it because of some characters (e.g. non-Latin letters).

    For example
    User's image

    User's image

    sprintf_s return error.

    User's image

    replace with swprintf_s

    TCHAR cp[1024];//wchar

    ......

    int cursize = swprintf_s(&cp[0], 1023, L"ts=%s %s", currTimeStr, currDateStr);

    wprintf(L"%ls\n", cp);

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. gupta, shivangi 20 Reputation points
    2024-03-17T10:03:07.8933333+00:00

    @RLWA32 I am also not able to reproduce it in my system. Its only reproducible in customer environment for which I have shared the systeminfo.


  2. gupta, shivangi 20 Reputation points
    2024-03-17T10:34:02.2+00:00

    @Viorel I have tried changing the locale to non-latin (Russia, Hebrew ) which couldnt reproduce the issue. Can you suggest some settings due to which GetDateFormatEx will return non-latin letters to fail sprintf?


  3. gupta, shivangi 20 Reputation points
    2024-04-22T06:40:13.1+00:00

    Thanks @Minxin Yu and @Viorel for helping me out. The issue was with sprintf. Its a very old code and this single customer has complaint about it. So, We have asked them to change their regional format.

    0 comments No comments