Bug Summary

File:root/firefox-clang/toolkit/components/remote/nsUnixRemoteServer.cpp
Warning:line 102, column 14
Potential leak of memory pointed to by 'argv'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -O2 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name nsUnixRemoteServer.cpp -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -ffp-contract=off -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/toolkit/components/remote -fcoverage-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/toolkit/components/remote -resource-dir /usr/lib/llvm-23/lib/clang/23 -include /root/firefox-clang/config/gcc_hidden.h -include /root/firefox-clang/obj-x86_64-pc-linux-gnu/mozilla-config.h -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/stl_wrappers -D _GLIBCXX_ASSERTIONS=1 -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/system_wrappers -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=2 -D DEBUG=1 -D MOZ_HAS_MOZGLUE -D MOZILLA_INTERNAL_API -D IMPL_LIBXUL -D MOZ_SUPPORT_LEAKCHECKING -D STATIC_EXPORTABLE_JS_API -I /root/firefox-clang/toolkit/components/remote -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/toolkit/components/remote -I /root/firefox-clang/toolkit/profile -I /root/firefox-clang/toolkit/xre -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nss -D MOZILLA_CLIENT -I /usr/include/dbus-1.0 -I /usr/lib/x86_64-linux-gnu/dbus-1.0/include -I /usr/include/gtk-3.0/unix-print -I /usr/include/gtk-3.0 -I /usr/include/pango-1.0 -I /usr/include/cloudproviders -I /usr/include/at-spi2-atk/2.0 -I /usr/include/at-spi-2.0 -I /usr/include/dbus-1.0 -I /usr/lib/x86_64-linux-gnu/dbus-1.0/include -I /usr/include/fribidi -I /usr/include/harfbuzz -I /usr/include/atk-1.0 -I /usr/include/cairo -I /usr/include/pixman-1 -I /usr/include/gdk-pixbuf-2.0 -I /usr/include/glycin-2 -I /usr/include/freetype2 -I /usr/include/libpng16 -I /usr/include/gio-unix-2.0 -I /usr/include/glib-2.0 -I /usr/lib/x86_64-linux-gnu/glib-2.0/include -I /usr/include/libmount -I /usr/include/blkid -I /usr/include/sysprof-6 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/x86_64-linux-gnu/c++/16 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../include/c++/16/backward -internal-isystem /usr/lib/llvm-23/lib/clang/23/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/16/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-error=pessimizing-move -Wno-error=large-by-value-copy=128 -Wno-error=implicit-int-float-conversion -Wno-error=thread-safety-analysis -Wno-error=tautological-type-limit-compare -Wno-invalid-offsetof -Wno-range-loop-analysis -Wno-deprecated-anon-enum-enum-conversion -Wno-deprecated-enum-enum-conversion -Wno-inline-new-delete -Wno-error=deprecated-declarations -Wno-error=array-bounds -Wno-error=free-nonheap-object -Wno-error=atomic-alignment -Wno-error=deprecated-builtins -Wno-psabi -Wno-error=builtin-macro-redefined -Wno-vla-cxx-extension -Wno-unknown-warning-option -Wno-character-conversion -std=gnu++20 -fdeprecated-macro -ferror-limit 19 -fstrict-flex-arrays=1 -stack-protector 2 -fstack-clash-protection -ftrivial-auto-var-init=pattern -fno-rtti -fgnuc-version=4.2.1 -fno-implicit-modules -fskip-odr-check-in-gmf -fno-sized-deallocation -fno-aligned-allocation -fdiagnostics-absolute-paths -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -mllvm -dwarf-linkage-names=Abstract -faddrsig -fdwarf2-cfi-asm -o /tmp/scan-build-2026-09-01-224014-2642839-1 -x c++ /root/firefox-clang/toolkit/components/remote/nsUnixRemoteServer.cpp
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5#include "nsUnixRemoteServer.h"
6#include "nsGTKToolkit.h"
7#include "nsCOMPtr.h"
8#include "nsICommandLineRunner.h"
9#include "nsCommandLine.h"
10#include "nsIFile.h"
11
12// Set desktop startup ID to the passed ID, if there is one, so that any created
13// windows get created with the right window manager metadata, and any windows
14// that get new tabs and are activated also get the right WM metadata.
15// The timestamp will be used if there is no desktop startup ID, or if we're
16// raising an existing window rather than showing a new window for the first
17// time.
18void nsUnixRemoteServer::SetStartupTokenOrTimestamp(
19 const nsACString& aStartupToken, uint32_t aTimestamp) {
20 nsGTKToolkit* toolkit = nsGTKToolkit::GetToolkit();
21 if (!toolkit) {
22 return;
23 }
24
25 if (!aStartupToken.IsEmpty()) {
26 toolkit->SetActivationToken(aStartupToken);
27 }
28
29 toolkit->SetFocusTimestamp(aTimestamp);
30}
31
32static bool FindExtensionParameterInCommand(const char* aParameterName,
33 const nsACString& aCommand,
34 char aSeparator,
35 nsACString* aValue) {
36 nsAutoCString searchFor;
37 searchFor.Append(aSeparator);
38 searchFor.Append(aParameterName);
39 searchFor.Append('=');
40
41 nsACString::const_iterator start, end;
42 aCommand.BeginReading(start);
43 aCommand.EndReading(end);
44 if (!FindInReadable(searchFor, start, end)) return false;
45
46 nsACString::const_iterator charStart, charEnd;
47 charStart = end;
48 aCommand.EndReading(charEnd);
49 nsACString::const_iterator idStart = charStart, idEnd;
50 if (FindCharInReadable(aSeparator, charStart, charEnd)) {
51 idEnd = charStart;
52 } else {
53 idEnd = charEnd;
54 }
55 *aValue = nsDependentCSubstring(idStart, idEnd);
56 return true;
57}
58
59const char* nsUnixRemoteServer::HandleCommandLine(
60 mozilla::Span<const char> aBuffer, uint32_t aTimestamp) {
61 nsCOMPtr<nsICommandLineRunner> cmdline(new nsCommandLine());
62
63 // the commandline property is constructed as an array of int32_t
64 // followed by a series of null-terminated strings:
65 //
66 // [argc][offsetargv0][offsetargv1...]<workingdir>\0<argv[0]>\0argv[1]...\0
67 // (offset is from the beginning of the buffer)
68 if (aBuffer.size() < sizeof(uint32_t) ||
1
Assuming the condition is false
3
Taking false branch
69 aBuffer[aBuffer.size() - 1] != '\0') {
2
Assuming the condition is false
70 return "500 command not parseable";
71 }
72
73 uint32_t argc =
74 TO_LITTLE_ENDIAN32(*reinterpret_cast<const uint32_t*>(aBuffer.data()))(*reinterpret_cast<const uint32_t*>(aBuffer.data()));
75
76 mozilla::CheckedInt<uint32_t> offsetFilelist =
77 ((mozilla::CheckedInt<uint32_t>(argc) + 1) * sizeof(uint32_t));
78 if (!offsetFilelist.isValid() || offsetFilelist.value() >= aBuffer.size()) {
4
Assuming the condition is false
5
Taking false branch
79 return "500 command not parseable";
80 }
81 const char* workingDir = aBuffer.data() + offsetFilelist.value();
82
83 nsCOMPtr<nsIFile> lf;
84 nsresult rv =
85 NS_NewNativeLocalFile(nsDependentCString(workingDir), getter_AddRefs(lf));
86 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
6
Assuming the condition is true
7
Taking false branch
87 return "509 internal error";
88 }
89
90 const char** argv = (const char**)malloc(sizeof(char*) * argc);
8
Memory is allocated
91 if (!argv) {
9
Assuming 'argv' is non-null
10
Taking false branch
92 return "509 internal error";
93 }
94
95 const uint32_t* offset =
96 reinterpret_cast<const uint32_t*>(aBuffer.data()) + 1;
97 nsAutoCString desktopStartupID;
98
99 for (unsigned int i = 0; i < argc; ++i) {
11
Assuming 'i' is < 'argc'
12
Loop condition is true. Entering loop body
100 uint32_t argvOffset = TO_LITTLE_ENDIAN32(offset[i])(offset[i]);
101 if (argvOffset >= aBuffer.size()) {
13
Assuming the condition is true
14
Taking true branch
102 return "500 command not parseable";
15
Potential leak of memory pointed to by 'argv'
103 }
104
105 argv[i] = aBuffer.data() + argvOffset;
106
107 if (i == 0) {
108 nsDependentCString cmd(argv[0]);
109 FindExtensionParameterInCommand("STARTUP_TOKEN", cmd, ' ',
110 &desktopStartupID);
111 }
112 }
113
114 rv = cmdline->Init(argc, argv, lf, nsICommandLine::STATE_REMOTE_AUTO);
115
116 free(argv);
117 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
118 return "509 internal error";
119 }
120
121 SetStartupTokenOrTimestamp(desktopStartupID, aTimestamp);
122
123 rv = cmdline->Run();
124
125 if (NS_ERROR_ABORT == rv) {
126 return "500 command not parseable";
127 }
128
129 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
130 return "509 internal error";
131 }
132
133 return "200 executed command";
134}