Repository: opencv/opencv
File: modules/core/src/system.cpp (C++)

Code before:

    ::GetTempPathW(kMaxPathSize, temp_dir_ce);

    if(0 != ::GetTempFileNameW(temp_dir_ce, L"ocv", 0, temp_file)) {
        DeleteFileW(temp_file);
        char aname[MAX_PATH];
        size_t copied = wcstombs(aname, temp_file, MAX_PATH);
        CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
        fname = String(aname);
    }
#else
    // Use GUID-based naming to avoid race condition with GetTempFileNameA
    // See issue #19648
    char temp_dir2[MAX_PATH] = { 0 };

    if (temp_dir.empty())
    {
        ::GetTempPathA(sizeof(temp_dir2), temp_dir2);
        temp_dir = std::string(temp_dir2);
    }

    GUID g;
    CoCreateGuid(&g);
    char guidStr[40];
    const char* mask = "%08x_%04x_%04x_%02x%02x_%02x%02x%02x%02x%02x%02x";
    sprintf(guidStr, mask,
            g.Data1, g.Data2, g.Data3, (unsigned int)g.Data4[0], (unsigned int)g.Data4[1],
            (unsigned int)g.Data4[2], (unsigned int)g.Data4[3], (unsigned int)g.Data4[4],
            (unsigned int)g.Data4[5], (unsigned int)g.Data4[6], (unsigned int)g.Data4[7]);

    fname = temp_dir;
    if (!fname.empty() && fname[fname.size()-1] != '\\' && fname[fname.size()-1] != '/')
        fname += "\\";
    fname = fname + "ocv" + guidStr;
#endif
# else
#  ifdef __ANDROID__
    //char defaultTemplate[] = "/mnt/sdcard/__opencv_temp.XXXXXX";
    char defaultTemplate[] = "/data/local/tmp/__opencv_temp.XXXXXX";
#  else
    char defaultTemplate[] = "/tmp/__opencv_temp.XXXXXX";
#  endif

    if (temp_dir.empty())
        fname = defaultTemplate;
    else
    {
        fname = temp_dir;
        char ech = fname[fname.size() - 1];
        if(ech != '/' && ech != '\\')
            fname = fname + "/";

Review comment (bug):
Use of unsafe `sprintf` instead of `snprintf`. Although the buffer size (40 bytes) is sufficient for the formatted GUID string (~36 characters + null terminator), using `snprintf` would be safer and more explicit:
```cpp
snprintf(guidStr, sizeof(guidStr), mask,
        g.Data1, g.Data2, g.Data3, (unsigned int)g.Data4[0], (unsigned int)g.Data4[1],
        (unsigned int)g.Data4[2], (unsigned int)g.Data4[3], (unsigned int)g.Data4[4],
        (unsigned int)g.Data4[5], (unsigned int)g.Data4[6], (unsigned int)g.Data4[7]);
```
This prevents potential buffer overflows if the format string is modified in the future.
```suggestion
    snprintf(guidStr, sizeof(guidStr), mask,
```

Code after:

    ::GetTempPathW(kMaxPathSize, temp_dir_ce);

    if(0 != ::GetTempFileNameW(temp_dir_ce, L"ocv", 0, temp_file)) {
        DeleteFileW(temp_file);
        char aname[MAX_PATH];
        size_t copied = wcstombs(aname, temp_file, MAX_PATH);
        CV_Assert((copied != MAX_PATH) && (copied != (size_t)-1));
        fname = String(aname);
    }
#else
    // Use GUID-based naming to avoid race condition with GetTempFileNameA
    // See issue #19648
    char temp_dir2[MAX_PATH] = { 0 };

    if (temp_dir.empty())
    {
        ::GetTempPathA(sizeof(temp_dir2), temp_dir2);
        temp_dir = std::string(temp_dir2);
    }

    GUID g;
    HRESULT hr = CoCreateGuid(&g);
    if (FAILED(hr))
        return String();
    char guidStr[40];
    const char* mask = "%08x_%04x_%04x_%02x%02x_%02x%02x%02x%02x%02x%02x";
    snprintf(guidStr, sizeof(guidStr), mask,
            g.Data1, g.Data2, g.Data3, (unsigned int)g.Data4[0], (unsigned int)g.Data4[1],
            (unsigned int)g.Data4[2], (unsigned int)g.Data4[3], (unsigned int)g.Data4[4],
            (unsigned int)g.Data4[5], (unsigned int)g.Data4[6], (unsigned int)g.Data4[7]);

    fname = temp_dir;
    if (!fname.empty() && fname[fname.size()-1] != '\\' && fname[fname.size()-1] != '/')
        fname += "\\";
    fname = fname + "ocv" + guidStr;
#endif
# else
#  ifdef __ANDROID__
    //char defaultTemplate[] = "/mnt/sdcard/__opencv_temp.XXXXXX";
    char defaultTemplate[] = "/data/local/tmp/__opencv_temp.XXXXXX";
#  else
    char defaultTemplate[] = "/tmp/__opencv_temp.XXXXXX";
#  endif

    if (temp_dir.empty())
        fname = defaultTemplate;
    else
    {
        fname = temp_dir;
        char ech = fname[fname.size() - 1];