Skip to content

Commit 453c24c

Browse files
authored
Enable -fsanitize=undefined in meson option, and fix found bugs (#681)
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.
1 parent f5c6a8b commit 453c24c

12 files changed

Lines changed: 35 additions & 24 deletions

File tree

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
run: >-
3333
brew install --display-times ${{ env.MAC_PKGS }}
3434
- name: meson
35-
run: meson setup build
35+
run: meson setup -Dsanitize-address=true -Dsanitize-undefined=true build
3636
- name: build
3737
run: meson compile -C build
3838
- name: install
@@ -60,7 +60,7 @@ jobs:
6060
- name: meson
6161
run: |
6262
meson --version
63-
meson setup build
63+
meson setup -Dsanitize-address=true -Dsanitize-undefined=true build
6464
env:
6565
CXX: ${{ matrix.compiler }}
6666
- name: build
@@ -69,7 +69,7 @@ jobs:
6969
run: sudo meson install -C build
7070
- name: test
7171
run: |
72-
xvfb-run -a meson test -C build --verbose --no-stdsplit
72+
ASAN_OPTIONS=detect_leaks=0 xvfb-run -a meson test -C build --verbose --no-stdsplit
7373
7474
codecov:
7575
runs-on: ubuntu-latest

meson.build

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ if get_option('sanitize-address')
88
add_project_arguments('-fsanitize=address', language: 'cpp')
99
add_project_link_arguments('-fsanitize=address', language: 'cpp')
1010
endif
11+
if get_option('sanitize-undefined')
12+
add_project_arguments('-fsanitize=undefined', language: 'cpp')
13+
add_project_link_arguments('-fsanitize=undefined', language: 'cpp')
14+
endif
1115

1216
so_version = 1
1317
subdir('src')

meson_options.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,10 @@ option(
2525
value: false,
2626
description: 'enable -fsanitize=address compile/link flag',
2727
)
28+
29+
option(
30+
'sanitize-undefined',
31+
type: 'boolean',
32+
value: false,
33+
description: 'enable -fsanitize=undefined compile/link flag',
34+
)

src/iptux-core/TransFileModel.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ std::string TransFileModel::getFinishedLengthText() const {
108108
}
109109

110110
double TransFileModel::getProgress() const {
111+
if (fileLength <= 0) {
112+
return 0.0; // Avoid division by zero
113+
}
111114
return percent(finishedLength, fileLength);
112115
}
113116

src/iptux-core/internal/UdpData.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ UdpData::UdpData(CoreThread& coreThread,
5555
}
5656
}
5757

58-
UdpData::UdpData(const string& buf_, const string& ipv4String)
59-
: coreThread(*(CoreThread*)NULL), size(buf_.size()), encode(nullptr) {
60-
this->ipv4 = inAddrFromString(ipv4String);
61-
memcpy(buf, &buf_[0], buf_.size());
62-
}
63-
6458
/**
6559
* 类析构函数.
6660
*/

src/iptux-core/internal/UdpData.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ namespace iptux {
2525
class UdpData {
2626
public:
2727
UdpData(CoreThread& coreThread, in_addr ipv4, const char buf[], size_t size);
28-
UdpData(const std::string& buf, const std::string& ipv4String);
2928
~UdpData();
3029

3130
in_addr getIpv4() const { return ipv4; }

src/iptux-core/internal/UdpDataTest.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ using namespace std;
88
using namespace iptux;
99

1010
TEST(UdpData, getCommandNo) {
11-
ASSERT_EQ(UdpData("", "127.0.0.1").getCommandNo(), 0);
11+
auto coreThread = newCoreThread();
12+
UdpData data(*coreThread, inAddrFromString("127.0.0.1"), "", 0);
13+
ASSERT_EQ(data.getCommandNo(), 0);
1214
}

src/iptux/Application.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ void Application::activate() {
116116
}
117117

118118
void Application::onStartup(Application& self) {
119+
self.data = make_shared<ProgramData>(self.config);
120+
self.cthrd = make_shared<UiCoreThread>(&self, self.data);
119121
init_theme(&self);
120122
iptux_register_resource();
121123
self.menuBuilder =
122124
gtk_builder_new_from_resource(IPTUX_RESOURCE "gtk/menus.ui");
123-
self.data = make_shared<ProgramData>(self.config);
124125
self.logSystem = new LogSystem(self.data);
125-
self.cthrd = make_shared<UiCoreThread>(&self, self.data);
126126
if (self.enable_app_indicator_) {
127127
self.app_indicator = make_shared<IptuxAppIndicator>(&self);
128128
}

src/iptux/MainWindow.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,9 @@ void MainWindow::LoadConfig() {
387387
this->info_style_ = info_style;
388388
}
389389

390-
GtkSortType sort_type =
391-
GtkSortTypeFromStr(this->config->GetString(config_names[CFG_SORT_TYPE]));
392-
if (sort_type != GTK_SORT_TYPE_INVALID) {
390+
GtkSortType sort_type;
391+
if (GtkSortTypeFromStr(this->config->GetString(config_names[CFG_SORT_TYPE]),
392+
sort_type)) {
393393
this->sort_type_ = sort_type;
394394
}
395395

@@ -1062,10 +1062,10 @@ void MainWindow::onSortType(GSimpleAction* action,
10621062
GVariant* value,
10631063
MainWindow& self) {
10641064
string sortType = g_variant_get_string(value, nullptr);
1065-
GtkSortType sort_type = GtkSortTypeFromStr(sortType);
1065+
GtkSortType sort_type;
10661066

1067-
if (sort_type == GTK_SORT_TYPE_INVALID) {
1068-
LOG_WARN("unknown sorttype: %s", sortType.c_str());
1067+
if (!GtkSortTypeFromStr(sortType, sort_type)) {
1068+
LOG_WARN("unknown sort type: %s", sortType.c_str());
10691069
return;
10701070
}
10711071
self.sort_type_ = sort_type;

src/iptux/TestHelper.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Application* CreateApplication() {
1111
gtk_init(nullptr, nullptr);
1212
auto config = newTestIptuxConfig();
1313
Application* app = new Application(config);
14+
app->set_enable_app_indicator(false);
15+
app->startup();
1416
// g_application_register(G_APPLICATION(app->getApp()), nullptr, nullptr);
1517
// auto i = g_application_get_is_registered(G_APPLICATION(app->getApp()));
1618
// EXPECT_TRUE(i);

0 commit comments

Comments
 (0)