Back to all reviewers

use optional types safely

electron/electron
Based on 8 comments
Other

When functions may not return valid values, use `std::optional` or `absl::optional` instead of raw types that could represent invalid states. Always check the validity of optional values before accessing them to prevent null pointer dereferences and invalid value usage.

Null Handling Other

Reviewer Prompt

When functions may not return valid values, use std::optional or absl::optional instead of raw types that could represent invalid states. Always check the validity of optional values before accessing them to prevent null pointer dereferences and invalid value usage.

For functions that might fail or return invalid data, prefer optional return types:

// Instead of returning 0 or invalid values on failure
std::optional<DWORD> GetAccentColor() {
  // ... implementation that may fail
  if (RegOpenKeyEx(...) != ERROR_SUCCESS) {
    return std::nullopt;  // Explicit failure indication
  }
  return accent_color;
}

// Always check validity before use
std::optional<DWORD> system_accent_color = GetAccentColor();
if (system_accent_color.has_value()) {
  border_color = RGB(GetRValue(system_accent_color.value()),
                     GetGValue(system_accent_color.value()),
                     GetBValue(system_accent_color.value()));
} else {
  should_apply_accent = false;
}

For pointer returns, check for null before dereferencing:

auto* contents = electron::api::WebContents::From(web_contents);
return contents ? contents->ID() : -1;

// For optional pointers, use both checks
if (frame && frame.value()) {
  auto* frame_rfh = frame->render_frame_host();
  auto* rfh = frame_rfh ? frame_rfh->GetOutermostMainFrameOrEmbedder() : nullptr;
  if (rfh) {
    // Safe to use rfh
  }
}

This pattern prevents crashes from accessing invalid pointers or using sentinel values that could lead to unexpected behavior. Note that base::Optional has been deprecated in favor of absl::optional in newer codebases.

8
Comments Analyzed
Other
Primary Language
Null Handling
Category

Source Discussions