Skip to content

Commit c65eda1

Browse files
committed
fix: clang-analyzer errors
These are various fixes reported by clang-analyzer regarding memory leaks, unsafe functions and inconsistent use use of new and free.
1 parent 601b01c commit c65eda1

File tree

9 files changed

+42
-21
lines changed

9 files changed

+42
-21
lines changed

.clang-tidy

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
Checks: >
2-
clang-diagnostic-*,
2+
-clang-analyzer-optin.core.EnumCastOutOfRange, # Disable enum cast range checker due to false positives in SpiderMonkey MOZ_ASSERT macro
33
clang-analyzer-*,
4-
cppcoreguidelines-*,
5-
modernize-*,
6-
performance-*,
7-
readability-*,
8-
-modernize-use-trailing-return-type,
9-
-readability-identifier-length
4+
# clang-diagnostic-*,
5+
# cppcoreguidelines-*,
6+
# modernize-*,
7+
# performance-*,
8+
# readability-*,
9+
# -modernize-use-trailing-return-type,
10+
# -readability-identifier-length
1011
1112
# Treat all warnings as errors
1213
WarningsAsErrors: '*'
@@ -17,5 +18,5 @@ HeaderFilterRegex: 'builtins/|runtime/'
1718
# Use the same style as clang-format (look for .clang-format)
1819
FormatStyle: file
1920

20-
# Don’t analyze temporary dtors (speeds up checking)
21-
AnalyzeTemporaryDtors: false
21+
# Explicitly exclude system/dependency headers
22+
SystemHeaders: false

builtins/web/console.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ namespace builtins::web::console {
413413

414414
__attribute__((weak))
415415
void builtin_impl_console_log(Console::LogType log_ty, const char *msg) {
416-
const char *prefix = "";
416+
const char *prefix = nullptr;
417417
switch (log_ty) {
418418
case Console::LogType::Log:
419419
prefix = "Log";
@@ -430,7 +430,12 @@ void builtin_impl_console_log(Console::LogType log_ty, const char *msg) {
430430
case Console::LogType::Error:
431431
prefix = "Error";
432432
break;
433+
default:
434+
prefix = "";
435+
break;
433436
}
437+
MOZ_ASSERT(prefix);
438+
434439
fprintf(stdout, "%s: %s\n", prefix, msg);
435440
fflush(stdout);
436441
}

builtins/web/fetch/fetch-api.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,9 @@ bool fetch_https(JSContext *cx, HandleObject request_obj, HandleObject response_
121121
// before marking the request as pending.
122122
if (!streaming) {
123123
// TODO: what about non streaming?
124-
auto task = mozilla::MakeRefPtr<ResponseFutureTask>(request_obj, pending_handle);
125-
auto weak = mozilla::WeakPtr<ResponseFutureTask>(task);
126-
127-
ENGINE->queue_async_task(task);
124+
auto task = js::MakeUnique<ResponseFutureTask>(request_obj, pending_handle);
125+
auto weak = mozilla::WeakPtr<ResponseFutureTask>(task.get());
126+
ENGINE->queue_async_task(task.release());
128127

129128
RootedObject signal(cx, Request::signal(request_obj));
130129
MOZ_ASSERT(signal);

builtins/web/fetch/headers.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ void skip_values_for_header_from_list(JSContext *cx, JS::HandleObject self, size
349349
bool validate_guard(JSContext *cx, HandleObject self, string_view header_name, const char *fun_name,
350350
bool *is_valid) {
351351
MOZ_ASSERT(Headers::is_instance(self));
352+
*is_valid = false;
353+
352354
Headers::HeadersGuard guard = Headers::guard(self);
353355
switch (guard) {
354356
case Headers::HeadersGuard::None:

builtins/web/fetch/request-response.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ bool normalize_http_method(char *method) {
147147

148148
// Note: Safe because `strcasecmp` returning 0 above guarantees
149149
// same-length strings.
150-
strcpy(method, name);
150+
strcpy(method, name); // NOLINT
151151
return true;
152152
}
153153
}
@@ -2009,7 +2009,7 @@ Value Response::aborted(JSObject *obj) {
20092009

20102010
// TODO(jake): Remove this when the reason phrase host-call is implemented
20112011
void Response::set_status_message_from_code(JSContext *cx, JSObject *obj, uint16_t code) {
2012-
auto phrase = "";
2012+
const char *phrase = nullptr;
20132013

20142014
switch (code) {
20152015
case 100: // 100 Continue - https://tools.ietf.org/html/rfc7231#section-6.2.1
@@ -2196,6 +2196,9 @@ void Response::set_status_message_from_code(JSContext *cx, JSObject *obj, uint16
21962196
phrase = "";
21972197
break;
21982198
}
2199+
2200+
MOZ_ASSERT(phrase);
2201+
21992202
JS::SetReservedSlot(obj, static_cast<uint32_t>(Slots::StatusMessage),
22002203
JS::StringValue(JS_NewStringCopyN(cx, phrase, strlen(phrase))));
22012204
}

builtins/web/streams/transform-stream.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ bool TransformStream::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
478478
// 0).
479479
// 6. Let readableSizeAlgorithm be !
480480
// [ExtractSizeAlgorithm](readableStrategy).
481-
double readableHighWaterMark;
481+
double readableHighWaterMark = 0.0;
482482
JS::RootedFunction readableSizeAlgorithm(cx);
483483
if (!ExtractStrategy(cx, args.get(2), 0, &readableHighWaterMark, &readableSizeAlgorithm)) {
484484
return false;
@@ -488,7 +488,7 @@ bool TransformStream::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
488488
// 1).
489489
// 8. Let writableSizeAlgorithm be !
490490
// [ExtractSizeAlgorithm](writableStrategy).
491-
double writableHighWaterMark;
491+
double writableHighWaterMark = 1.0;
492492
JS::RootedFunction writableSizeAlgorithm(cx);
493493
if (!ExtractStrategy(cx, args.get(1), 1, &writableHighWaterMark, &writableSizeAlgorithm)) {
494494
return false;

builtins/web/url.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,9 @@ bool URL::createObjectURL(JSContext *cx, unsigned argc, JS::Value *vp) {
590590
}
591591

592592
bool URL::revokeObjectURL(JSContext *cx, unsigned argc, JS::Value *vp) {
593+
// Suppress false positive in Mozilla's HashTable implementation
594+
// about null dereference, the remove method will check if the entry exists.
595+
// NOLINTBEGIN
593596
CallArgs args = JS::CallArgsFromVp(argc, vp);
594597
if (!args.requireAtLeast(cx, "createObjectURL", 1)) {
595598
return false;
@@ -602,8 +605,8 @@ bool URL::revokeObjectURL(JSContext *cx, unsigned argc, JS::Value *vp) {
602605
return false;
603606
}
604607

605-
// 2. If urlRecord’s scheme is not "blob", return.
606-
// 3. Let entry be urlRecord’s blob URL entry.
608+
// 2. If urlRecord’s scheme is not "blob", return.
609+
// 3. Let entry be urlRecord’s blob URL entry.
607610
std::string url_record(chars.ptr.get());
608611
if (!url_record.starts_with("blob:")) {
609612
return true;
@@ -613,7 +616,9 @@ bool URL::revokeObjectURL(JSContext *cx, unsigned argc, JS::Value *vp) {
613616
// 5. Let isAuthorized be the result of checking for same-partition blob URL usage with entry and the current settings object.
614617
// 6. If isAuthorized is false, then return.
615618
// 7. Remove an entry from the Blob URL Store for url.
616-
URL_STORE.get().remove(chars.ptr.get());
619+
620+
URL_STORE.get().remove(url_record);
621+
// NOLINTEND
617622
return true;
618623
}
619624

cmake/lint.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ add_custom_target(clang-tidy-fix
2323
-config-file=${CMAKE_SOURCE_DIR}/.clang-tidy
2424
-fix
2525
${CMAKE_SOURCE_DIR}/builtins
26+
${CMAKE_SOURCE_DIR}/runtime
2627
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
2728
COMMENT "Running clang‑tidy over the codebase and applying fixes"
2829
VERBATIM)

runtime/encode.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ HostString encode_byte_string(JSContext *cx, JS::HandleValue val) {
6262
} else {
6363
length = JS::GetStringLength(str);
6464
}
65+
66+
if (length == 0) {
67+
return HostString();
68+
}
69+
6570
char *buf = static_cast<char *>(malloc(length));
6671
if (!JS_EncodeStringToBuffer(cx, str, buf, length))
6772
MOZ_ASSERT_UNREACHABLE();

0 commit comments

Comments
 (0)