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

Code before:
    const auto kMaxPathSize = MAX_PATH+1;
    wchar_t temp_dir_ce[kMaxPathSize] = {0};
    wchar_t temp_file[kMaxPathSize] = {0};

    ::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;

Review comment (bug):
Missing error handling for `CoCreateGuid()`. Although rare, this function can fail (e.g., if the system is out of resources). Consider checking the return value:
```cpp
HRESULT hr = CoCreateGuid(&g);
if (FAILED(hr))
    return String();
```
This would be consistent with error handling patterns elsewhere in the function (e.g., line 1083-1084 in the WinRT branch).
```suggestion
    HRESULT hr = CoCreateGuid(&g);
    if (FAILED(hr))
        return String();
```

Code after:
    const auto kMaxPathSize = MAX_PATH+1;
    wchar_t temp_dir_ce[kMaxPathSize] = {0};
    wchar_t temp_file[kMaxPathSize] = {0};

    ::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