Skip to content

Enable -fsanitize=undefined in meson option, and fix found bugs#681

Merged
lidaobing merged 7 commits into
masterfrom
sanitized
Jul 8, 2025
Merged

Enable -fsanitize=undefined in meson option, and fix found bugs#681
lidaobing merged 7 commits into
masterfrom
sanitized

Conversation

@lidaobing

@lidaobing lidaobing commented Jul 8, 2025

Copy link
Copy Markdown
Member

Summary by Sourcery

Refactor sort type parsing and client startup sequence; add undefined-behavior sanitizer support; guard against zero-length transfers; and ensure application startup is called in tests.

Bug Fixes:

  • Prevent division by zero in TransFileModel::getProgress by returning 0.0 when file length is non-positive.

Enhancements:

  • Refactor GtkSortTypeFromStr to use a boolean result with an output parameter and update its callers.
  • Reorder Application startup to initialize ProgramData and UiCoreThread before building the UI.

Build:

  • Add 'sanitize-undefined' Meson build option to enable -fsanitize=undefined flags.

Tests:

  • Invoke Application::startup in TestHelper to ensure proper initialization during tests.

@sourcery-ai

sourcery-ai Bot commented Jul 8, 2025

Copy link
Copy Markdown

Reviewer's Guide

This PR refactors the GtkSortTypeFromStr API to use an out-parameter and boolean return, updates its call sites, adds an undefined sanitizer toggle to the Meson build, adjusts application startup initialization, adds a division-by-zero guard in the file transfer model, and invokes startup in the test helper.

Sequence diagram for GtkSortTypeFromStr usage in MainWindow::onSortType

sequenceDiagram
    participant MainWindow
    participant UiModels
    MainWindow->>UiModels: GtkSortTypeFromStr(sortType, &sort_type)
    alt success
        UiModels-->>MainWindow: true, sort_type set
        MainWindow->>MainWindow: set sort_type_
    else failure
        UiModels-->>MainWindow: false
        MainWindow->>MainWindow: log warning, return
    end
Loading

Class diagram for GtkSortTypeFromStr API refactor

classDiagram
    class UiModels {
      +bool GtkSortTypeFromStr(const std::string& s, GtkSortType* outType)
      +const char* GtkSortTypeToStr(GtkSortType t)
    }
    class MainWindow {
      -GtkSortType sort_type_
      +void LoadConfig()
      +void onSortType(GSimpleAction* action, GVariant* value, MainWindow& self)
    }
    UiModels <.. MainWindow : uses
    note for UiModels "GtkSortTypeFromStr now returns bool and uses out-parameter."
Loading

Class diagram for TransFileModel division-by-zero guard

classDiagram
    class TransFileModel {
      +double getProgress() const
      -int64_t finishedLength
      -int64_t fileLength
    }
    note for TransFileModel "getProgress now guards against fileLength <= 0."
Loading

File-Level Changes

Change Details Files
Convert GtkSortTypeFromStr to return success via bool and out-parameter
  • Change function signature to bool with outType pointer
  • Remove invalid sort-type constant
  • Update LoadConfig and onSortType callers to use new API and handle failure
src/iptux/UiModels.cpp
src/iptux/UiModels.h
src/iptux/MainWindow.cpp
Add 'sanitize-undefined' option to Meson build
  • Declare new boolean option in meson_options.txt
  • Pass -fsanitize=undefined flags when enabled in meson.build
meson_options.txt
meson.build
Reorder and dedupe Application startup initialization
  • Move ProgramData and UiCoreThread construction before theme init
  • Remove duplicate instantiation after LogSystem creation
src/iptux/Application.cpp
Prevent division by zero in TransFileModel::getProgress
  • Check fileLength <= 0 and return 0.0 before computing percent
src/iptux-core/TransFileModel.cpp
Ensure test helper invokes application startup
  • Call app->startup() after construction in TestHelper
src/iptux/TestHelper.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @lidaobing - I've reviewed your changes and found some issues that need to be addressed.

Blocking issues:

  • Consider validating the outType pointer before dereferencing. (link)

General comments:

  • In GtkSortTypeFromStr, add an assertion or null-pointer check for outType before dereferencing to avoid potential segfaults if someone passes nullptr.
  • Reordering the creation of ProgramData/UiCoreThread before init_theme and logSystem may introduce startup races—ensure the core thread isn’t accessing uninitialized resources.
  • Wrap the -fsanitize=undefined flags behind a compiler capability check in meson.build to prevent build failures on toolchains that don’t support undefined behavior sanitizer.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In GtkSortTypeFromStr, add an assertion or null-pointer check for outType before dereferencing to avoid potential segfaults if someone passes nullptr.
- Reordering the creation of ProgramData/UiCoreThread before init_theme and logSystem may introduce startup races—ensure the core thread isn’t accessing uninitialized resources.
- Wrap the `-fsanitize=undefined` flags behind a compiler capability check in meson.build to prevent build failures on toolchains that don’t support undefined behavior sanitizer.

## Individual Comments

### Comment 1
<location> `src/iptux/UiModels.cpp:358` </location>
<code_context>
 };

-GtkSortType GtkSortTypeFromStr(const std::string& s) {
+bool GtkSortTypeFromStr(const std::string& s, GtkSortType* outType) {
   for (int i = GTK_SORT_ASCENDING; i <= GTK_SORT_DESCENDING; ++i) {
     if (s == gtk_sort_type_names[i]) {
</code_context>

<issue_to_address>
Consider validating the outType pointer before dereferencing.

Dereferencing a nullptr outType will cause undefined behavior. Please add a check for nullptr and return false if detected.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
bool GtkSortTypeFromStr(const std::string& s, GtkSortType* outType) {
  for (int i = GTK_SORT_ASCENDING; i <= GTK_SORT_DESCENDING; ++i) {
    if (s == gtk_sort_type_names[i]) {
      *outType = (GtkSortType)i;
      return true;
    }
  }
  return false;
}
=======
bool GtkSortTypeFromStr(const std::string& s, GtkSortType* outType) {
  if (!outType) {
    return false;
  }
  for (int i = GTK_SORT_ASCENDING; i <= GTK_SORT_DESCENDING; ++i) {
    if (s == gtk_sort_type_names[i]) {
      *outType = (GtkSortType)i;
      return true;
    }
  }
  return false;
}
>>>>>>> REPLACE

</suggested_fix>

### Comment 2
<location> `src/iptux-core/TransFileModel.cpp:111` </location>
<code_context>
 }

 double TransFileModel::getProgress() const {
+  if (fileLength <= 0) {
+    return 0.0;  // Avoid division by zero
+  }
   return percent(finishedLength, fileLength);
</code_context>

<issue_to_address>
Returning 0.0 for non-positive fileLength may mask input errors.

Consider logging a warning if fileLength is negative, as this may signal a logic error.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/iptux/UiModels.cpp Outdated
Comment on lines 358 to 366
bool GtkSortTypeFromStr(const std::string& s, GtkSortType* outType) {
for (int i = GTK_SORT_ASCENDING; i <= GTK_SORT_DESCENDING; ++i) {
if (s == gtk_sort_type_names[i]) {
return (GtkSortType)i;
*outType = (GtkSortType)i;
return true;
}
}
return GTK_SORT_TYPE_INVALID;
return false;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Consider validating the outType pointer before dereferencing.

Dereferencing a nullptr outType will cause undefined behavior. Please add a check for nullptr and return false if detected.

Suggested change
bool GtkSortTypeFromStr(const std::string& s, GtkSortType* outType) {
for (int i = GTK_SORT_ASCENDING; i <= GTK_SORT_DESCENDING; ++i) {
if (s == gtk_sort_type_names[i]) {
return (GtkSortType)i;
*outType = (GtkSortType)i;
return true;
}
}
return GTK_SORT_TYPE_INVALID;
return false;
}
bool GtkSortTypeFromStr(const std::string& s, GtkSortType* outType) {
if (!outType) {
return false;
}
for (int i = GTK_SORT_ASCENDING; i <= GTK_SORT_DESCENDING; ++i) {
if (s == gtk_sort_type_names[i]) {
*outType = (GtkSortType)i;
return true;
}
}
return false;
}

Comment on lines +111 to +112
if (fileLength <= 0) {
return 0.0; // Avoid division by zero

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Returning 0.0 for non-positive fileLength may mask input errors.

Consider logging a warning if fileLength is negative, as this may signal a logic error.

@github-actions

github-actions Bot commented Jul 8, 2025

Copy link
Copy Markdown

Test Results

66 tests  ±0   66 ✅ ±0   3s ⏱️ -1s
32 suites ±0    0 💤 ±0 
 1 files   ±0    0 ❌ ±0 

Results for commit abca8a1. ± Comparison against base commit f5c6a8b.

♻️ This comment has been updated with latest results.

@lidaobing lidaobing changed the title 1 Enable -fsanitize=undefined in meson option, and fix found bugs Jul 8, 2025
@codecov

codecov Bot commented Jul 8, 2025

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 90.90909% with 1 line in your changes missing coverage. Please review.

Project coverage is 52.38%. Comparing base (f5c6a8b) to head (abca8a1).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/iptux/MainWindow.cpp 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #681      +/-   ##
==========================================
- Coverage   52.42%   52.38%   -0.04%     
==========================================
  Files          64       64              
  Lines        8603     8599       -4     
==========================================
- Hits         4510     4505       -5     
- Misses       4093     4094       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lidaobing
lidaobing merged commit 453c24c into master Jul 8, 2025
9 of 19 checks passed
@lidaobing
lidaobing deleted the sanitized branch July 8, 2025 06:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant