Skip to content

Decode command line arguments as UTF-8 so non-ANSI project names open - #11985

Merged
ianalarina merged 3 commits into
audacity:masterfrom
jonata:fix-nonascii-path-argv
Sep 15, 2026
Merged

ianalarina merged 3 commits into
audacity:masterfrom
jonata:fix-nonascii-path-argv

Conversation

@jonata

@jonata jonata commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Resolves: #11976
Related: #11973

Project names containing characters outside the Windows ANSI code page —
Cyrillic, Japanese, CJK generally — could not be opened.

The defect

prepareArguments() decoded argv with QString::fromLocal8Bit(), but every
producer hands it UTF-8:

Producer Encoding
main.cpp:137 — CommandLineToArgvW → QString::toUtf8() UTF-8, with a comment saying it does this to avoid losing non-ANSI characters
guiapp.cpp:41 — muse::StringList::toStdStringList() UTF-8
POSIX argv UTF-8

main.cpp:195 reads the same buffers back with fromUtf8(). The parser was
the only place reading them as something else.

On Windows in Qt 6, fromLocal8Bit decodes with the process ANSI code page, so
the UTF-8 bytes of тест.aup4 are re-read as CP1251 and become mojibake — which
is exactly what #11976 describes ("Audacity tries to access the file using a
corrupted (mojibake) filename").

Off Windows this changes nothing at all. From Qt 6.10.1's own header:

struct Q_CORE_EXPORT QLocal8Bit
{
#if !defined(Q_OS_WIN) || defined(QT_BOOTSTRAPPED)
    static QString convertToUnicode(QByteArrayView in, QStringConverter::State *state)
    { return QUtf8::convertToUnicode(in, state); }
#else
    ...                              // system ANSI code page

So fromUtf8 is correct on all three platforms, and a no-op on two of them.

Three routes reach the parser and were all affected: double-clicking a project,
the single-instance handoff (guiapp.cpp:162), and the in-app route that opens
a project in a new window when one is already open
(projectactionscontroller.cpp:997).

QTextCodec::setCodecForLocale(UTF-8) in main.cpp:127 does not protect
against this. It is a Qt 5 mechanism; Qt 6 routes fromLocal8Bit through
QStringConverter::System inside QtCore, which never consults QTextCodec
(a separate Qt5Compat module). It is worth deleting, but I have left it alone
here rather than widen the change.

Second commit: a dangling buffer on the same open path

While tracing this I found unrelated but real UB in DBConnection::OpenStepByStep:

const char* name = fileName.ToUTF8();   // buffer freed at the end of this statement
int rc = sqlite3_open(name, &mDB);      // ...then read here, and again at line 199

ToUTF8() returns a wxScopedCharBuffer by value; where wxString is not
internally UTF-8 (Windows, macOS) it owns its memory. It usually survives
because nothing allocates in between, which is why it has not been obviously
broken — but it is a read of freed memory on the path that opens every project.
ProjectFileIO::ReadThumbnail already does this correctly by calling
uri.ToUTF8().data() inside the call.

On #11973 — why "Related" and not "Resolves"

#11973 (Cyrillic name, fails on first attempt, works on the second) is very
likely the same defect: the in-app new-window route sends the path through the
parser on the first attempt, and the second attempt — with no project open —
opens in-process and never touches it. That fits the reported asymmetry exactly.

But the second screenshot in that issue shows the Cyrillic name rendered
correctly in "Failed to open database file: …тест.aup4", which is not what a
mojibake path would produce. So either the message is built from a different
string than the one used to open, or something else is involved — possibly the
dangling buffer above. I have not claimed to resolve it.

Verification, and its limits

Both reports are Windows-only and this was developed on Linux, where
fromLocal8Bit is already UTF-8 — so I cannot demonstrate either fix
working
. What I can show:

  • The mechanism is fully determined by the Qt 6 source quoted above.
  • Building and opening a Cyrillic-named file on Linux still works
    (Open(…тест.wav) succeeded), confirming no regression where the code path
    was already correct.
  • ctest green, codestyle clean.

This needs testing on Windows with a non-ANSI project name before it should be
trusted.

  • I signed CLA
  • The title of the pull request describes an issue it addresses
  • If changes are extensive, then there is a sequence of easily reviewable commits
  • Each commit's message describes its purpose and effects
  • There are no behavior changes unnecessary for the stated purpose of the PR

Recommended:

  • Each commit compiles and runs on my machine without known undesirable changes of behavior

QA:

  • Testflow test cases have been run

@coderabbitai

coderabbitai Bot commented Sep 5, 2026 •

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: CHILL

Plan: Advanced

Run ID: 73fa72d4-209f-40a1-bb13-6e6270fbdeb7

📥 Commits

Reviewing files that changed from the base of the PR and between 3e5163edecb580eb65910005d80b4bf6b71f19d4 and e19f84d9af780e4b4f7581c44d329e4bd0a9995c.

📒 Files selected for processing (2)
  • au3/libraries/au3-project-file-io/DBConnection.cpp
  • src/app/commandlineparser.cpp
💤 Files with no reviewable changes (2)
  • src/app/commandlineparser.cpp
  • au3/libraries/au3-project-file-io/DBConnection.cpp

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

OpenStepByStep now keeps the UTF-8 filename buffer alive during both SQLite open calls. prepareArguments now decodes command-line arguments as UTF-8 on all platforms instead of using the local encoding.

Priority: ➖ Normal — Schedule the Unicode filename fix because it addresses medium-severity failures opening macOS projects with Japanese, Cyrillic, and other non-ANSI paths on Windows.

Estimated code review effort: 2 (Simple) | ~10 minutes

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to a52b0

The update enables UTF-8 command-line project paths and preserves the SQLite filename buffer during database opening. No current merge-blocking risk is identified.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the primary change: decoding command-line arguments as UTF-8 to open projects with non-ANSI names.
Description check ✅ Passed The description identifies the linked issue, explains the defect and motivation, documents the two fixes, and completes the required checklist. Windows verification remains pending, but the descriptio…
Linked Issues check ✅ Passed The changes address issue #11976 by decoding Windows command-line arguments as UTF-8 and preserving the UTF-8 filename buffer during SQLite opening. These changes target the reported mojibake filename…
Out of Scope Changes check ✅ Passed The changes are within scope. The command-line decoding fix directly addresses the linked issue, and the SQLite buffer-lifetime fix applies to the same project-opening path and prevents related undefi…

Warning

Linked repositories: Public OSS repositories can only analyze public repositories installed in this organization. No linked repositories were analyzed; skipped musescore/musescore.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@kryksyh

kryksyh commented Sep 8, 2026

Copy link
Copy Markdown
Member

@jonata please remove or rewrite comments in the code, there is no need to be that verbose.
Otherwise looks good, thank you!

@jonata

jonata commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Comments removed, thanks! 🫡

@ianalarina
ianalarina requested a review from kryksyh September 8, 2026 14:23
@ianalarina ianalarina linked an issue Sep 8, 2026 that may be closed by this pull request
Opening a project whose name contains characters outside the Windows ANSI code
page failed: Cyrillic, CJK and anything else non-ANSI arrived mangled and the
file could not be found.

prepareArguments() decoded argv with QString::fromLocal8Bit(), but every
producer hands it UTF-8. main.cpp rebuilds argv from CommandLineToArgvW through
QString::toUtf8() on Windows - with a comment saying it does so precisely to
avoid losing characters that do not fit the local code page - and then reads
those same buffers back with fromUtf8() at line 195. GuiApp::makeContextOptions
builds them from muse::StringList, also UTF-8. The parser was the one place
reading them as anything else.

On Windows in Qt 6 that means the process ANSI code page, so the UTF-8 bytes of
"тест.aup4" are re-read as CP1251 and become mojibake. Off Windows,
QLocal8Bit::convertToUnicode is literally QUtf8::convertToUnicode
(qstringconverter_p.h), so this changes nothing on Linux or macOS and is the
correct decode everywhere.

Three routes reach it: the process command line (double-clicking a project),
the single instance handoff, and the in-app route that opens a project in a new
window when one is already open.

Note that QTextCodec::setCodecForLocale(UTF-8) in main.cpp does not protect
this. It is a Qt 5 mechanism; Qt 6 routes fromLocal8Bit through
QStringConverter::System in QtCore, which never consults it.
OpenStepByStep did

    const char* name = fileName.ToUTF8();

ToUTF8() returns a wxScopedCharBuffer by value. Where wxString is not
internally UTF-8 - which is Windows and macOS - that buffer owns its memory and
is freed at the end of the declaration, so `name` dangles before either of the
two sqlite3_open calls that use it reads it.

It usually survives, because nothing allocates between the free and the reads,
which is why this has not been obviously broken. It is still a read of freed
memory on the path that opens every project.

The buffer is now held in a named variable for as long as the pointer is used.
ProjectFileIO::ReadThumbnail already does this correctly by calling
uri.ToUTF8().data() inside the call, where the temporary lives to the end of
the full expression.
@ianalarina
ianalarina force-pushed the fix-nonascii-path-argv branch from e19f84d to a52b0c2 Compare September 15, 2026 10:24
@ianalarina
ianalarina merged commit e24c106 into audacity:master Sep 15, 2026
7 of 10 checks passed
kryksyh added a commit that referenced this pull request Sep 22, 2026
Decode command line arguments as UTF-8 so non-ANSI project names open (#11985)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants