Bug Summary

File:var/lib/jenkins/workspace/firefox-scan-build/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 -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=/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/toolkit/components/remote -fcoverage-compilation-dir=/var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/toolkit/components/remote -resource-dir /usr/lib/llvm-19/lib/clang/19 -include /var/lib/jenkins/workspace/firefox-scan-build/config/gcc_hidden.h -include /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/mozilla-config.h -I /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/stl_wrappers -I /var/lib/jenkins/workspace/firefox-scan-build/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 /var/lib/jenkins/workspace/firefox-scan-build/toolkit/components/remote -I /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/toolkit/components/remote -I /var/lib/jenkins/workspace/firefox-scan-build/toolkit/profile -I /var/lib/jenkins/workspace/firefox-scan-build/toolkit/xre -I /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include -I /var/lib/jenkins/workspace/firefox-scan-build/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /var/lib/jenkins/workspace/firefox-scan-build/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 -I /usr/include/pango-1.0 -I /usr/include/glib-2.0 -I /usr/lib/x86_64-linux-gnu/glib-2.0/include -I /usr/include/sysprof-6 -I /usr/include/harfbuzz -I /usr/include/freetype2 -I /usr/include/libpng16 -I /usr/include/libmount -I /usr/include/blkid -I /usr/include/fribidi -I /usr/include/cairo -I /usr/include/pixman-1 -I /usr/include/gdk-pixbuf-2.0 -I /usr/include/x86_64-linux-gnu -I /usr/include/webp -I /usr/include/gio-unix-2.0 -I /usr/include/cloudproviders -I /usr/include/atk-1.0 -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/gtk-3.0/unix-print -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/x86_64-linux-gnu/c++/14 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/backward -internal-isystem /usr/lib/llvm-19/lib/clang/19/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -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-deprecated-this-capture -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 -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 -fskip-odr-check-in-gmf -fno-sized-deallocation -fno-aligned-allocation -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2024-09-22-115206-3586786-1 -x c++ /var/lib/jenkins/workspace/firefox-scan-build/toolkit/components/remote/nsUnixRemoteServer.cpp
1/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* vim:expandtab:shiftwidth=2:tabstop=2:
3 */
4/* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7
8#include "nsUnixRemoteServer.h"
9#include "nsGTKToolkit.h"
10#include "nsCOMPtr.h"
11#include "nsICommandLineRunner.h"
12#include "nsCommandLine.h"
13#include "nsIFile.h"
14
15// Set desktop startup ID to the passed ID, if there is one, so that any created
16// windows get created with the right window manager metadata, and any windows
17// that get new tabs and are activated also get the right WM metadata.
18// The timestamp will be used if there is no desktop startup ID, or if we're
19// raising an existing window rather than showing a new window for the first
20// time.
21void nsUnixRemoteServer::SetStartupTokenOrTimestamp(
22 const nsACString& aStartupToken, uint32_t aTimestamp) {
23 nsGTKToolkit* toolkit = nsGTKToolkit::GetToolkit();
24 if (!toolkit) {
25 return;
26 }
27
28 if (!aStartupToken.IsEmpty()) {
29 toolkit->SetStartupToken(aStartupToken);
30 }
31
32 toolkit->SetFocusTimestamp(aTimestamp);
33}
34
35static bool FindExtensionParameterInCommand(const char* aParameterName,
36 const nsACString& aCommand,
37 char aSeparator,
38 nsACString* aValue) {
39 nsAutoCString searchFor;
40 searchFor.Append(aSeparator);
41 searchFor.Append(aParameterName);
42 searchFor.Append('=');
43
44 nsACString::const_iterator start, end;
45 aCommand.BeginReading(start);
46 aCommand.EndReading(end);
47 if (!FindInReadable(searchFor, start, end)) return false;
48
49 nsACString::const_iterator charStart, charEnd;
50 charStart = end;
51 aCommand.EndReading(charEnd);
52 nsACString::const_iterator idStart = charStart, idEnd;
53 if (FindCharInReadable(aSeparator, charStart, charEnd)) {
54 idEnd = charStart;
55 } else {
56 idEnd = charEnd;
57 }
58 *aValue = nsDependentCSubstring(idStart, idEnd);
59 return true;
60}
61
62const char* nsUnixRemoteServer::HandleCommandLine(
63 mozilla::Span<const char> aBuffer, uint32_t aTimestamp) {
64 nsCOMPtr<nsICommandLineRunner> cmdline(new nsCommandLine());
65
66 // the commandline property is constructed as an array of int32_t
67 // followed by a series of null-terminated strings:
68 //
69 // [argc][offsetargv0][offsetargv1...]<workingdir>\0<argv[0]>\0argv[1]...\0
70 // (offset is from the beginning of the buffer)
71 if (aBuffer.size() < sizeof(uint32_t)) {
1
Assuming the condition is false
2
Taking false branch
72 return "500 command not parseable";
73 }
74
75 uint32_t argc =
76 TO_LITTLE_ENDIAN32(*reinterpret_cast<const uint32_t*>(aBuffer.data()))(*reinterpret_cast<const uint32_t*>(aBuffer.data()));
77 uint32_t offsetFilelist = ((argc + 1) * sizeof(uint32_t));
78 if (offsetFilelist >= aBuffer.size()) {
3
Assuming the condition is false
4
Taking false branch
79 return "500 command not parseable";
80 }
81 const char* workingDir = aBuffer.data() + offsetFilelist;
82
83 nsCOMPtr<nsIFile> lf;
84 nsresult rv = NS_NewNativeLocalFile(nsDependentCString(workingDir), true,
85 getter_AddRefs(lf));
86 if (NS_FAILED(rv)((bool)(__builtin_expect(!!(NS_FAILED_impl(rv)), 0)))) {
5
Assuming the condition is true
6
Taking false branch
87 return "509 internal error";
88 }
89
90 const char** argv = (const char**)malloc(sizeof(char*) * argc);
7
Memory is allocated
91 if (!argv) {
8
Assuming 'argv' is non-null
9
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) {
10
Assuming 'i' is < 'argc'
11
Loop condition is true. Entering loop body
100 uint32_t argvOffset = TO_LITTLE_ENDIAN32(offset[i])(offset[i]);
101 if (argvOffset >= aBuffer.size()) {
12
Assuming the condition is true
13
Taking true branch
102 return "500 command not parseable";
14
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}