Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(HttpHandler): prevent exceptions from escaping destructors and cleanup#765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
lyriccoder wants to merge1 commit intoithewei:master
base:master
Choose a base branch
Loading
fromlyriccoder:master

Conversation

@lyriccoder
Copy link

Problem:
Currently,~HttpHandler(),Close(),killTimer(), anderase() can propagate exceptions through destructors or cleanup code.
This violates safe C++ practices because throwing exceptions from destructors is undefined behavior if another exception is already active (stack unwinding). In addition, placement new or STL container operations (likestd::deque::push_back) can throw, which might escape through the destructor.

This is flagged by static analyzers and linters as a critical issue: destructors must not throw. Even if the code works most of the time, a single allocation failure or exception in a callback can terminate the program unexpectedly.

Potential call stack where exceptions can propagate:

~HttpHandler() └─ Close()     └─ closeFile()         └─ killTimer()             └─ runInLoop(lambda)                 └─ queueInLoop()                     └─ postEvent()                         └─ customEvents.push(ev)  <-- std::length_error or other exceptions

There are 3 potential fixes, (I am suggesting the first one):

  1. ~HttpHandler() now wrapsClose() in atry/catch to silently swallow any exceptions.
  2. Close() andkillTimer() are markednoexcept and all potentially throwing operations are wrapped intry/catch.
voidHttpHandler::killTimer(TimerID timerID)noexcept {runInLoop([timerID,this]()noexcept {try {auto iter = timers.find(timerID);if (iter != timers.end()) {htimer_del(iter->second->timer);                timers.erase(iter);            }        }catch (...) {// exceptions swallowed safely        }    });}
  1. Replace placement new with move assignment, which isnoexcept if move constructor isnoexcept.
    Guarantees no exceptions propagate from erase.
size_type erase(const Key& key) {    for (auto it = this->begin(); it != this->end(); ++it) {        if (it->first == key) {            for (auto moveIt = it; moveIt + 1 != this->end(); ++moveIt) {                *moveIt = std::move(*(moveIt + 1));            }            Container::pop_back();            return 1;        }    }    return 0;}

This guarantees that destructors and cleanup routines cannot throw, satisfying the C++ Core Guidelines rule:C.64: Destructors should be noexcept
.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

@lyriccoder

[8]ページ先頭

©2009-2025 Movatter.jp