Menu

replaceIllegalCharacters doesn't handle illegal names that contains newline

2026-07-31
2026-08-02
  • devashish yadav

    devashish yadav - 2026-07-31

    StringHelper::replaceIllegalCharacters() currently replaces only the standard Windows filename characters:

    wstring StringHelper::replaceIllegalCharacters(const wstring& filename)
    {
        return replaceCharacters(filename, L"<>:\"/\\|?*", L"_");
    }
    

    However, it does not remove ASCII control characters (e.g. \r, \n, \t).

    On my system, the Bluetooth audio endpoint property ({b3f8fa53-0004-438e-9003-51a46e139bfc},6) contains a trailing CRLF:

    20 50 65 62 62 6C 65 20 56 33 0D 0A
    

    which corresponds to:

    " Pebble V3\r\n"
    

    When DeviceSelector generates the backup filename:

    L"backup_" +
    StringHelper::replaceIllegalCharacters(deviceName) +
    L"_" +
    StringHelper::replaceIllegalCharacters(connectionName) +
    L".reg"
    

    the control characters are preserved, resulting in an invalid filename and the following error:

    Error while opening file backup_ Pebble V3 _Headphones.reg for writing
    

    A possible fix is to strip ASCII control characters before replacing the standard Windows filename characters:

    wstring StringHelper::replaceIllegalCharacters(const wstring& filename)
    {
        wstring sanitized;
    
        for (wchar_t c : filename)
        {
            if (c < 32)
                continue;
    
            sanitized += c;
        }
    
        return replaceCharacters(sanitized, L"<>:\"/\\|?*", L"_");
    }
    
     
  • Peter Verbeek

    Peter Verbeek - 2026-08-02

    Good solution. I hope the developer picks it up.

    Btw. I guess you have change the name of that particular device avoiding the problem.

     

Log in to post a comment.