Skip to content

Replace manual memory management with RAII containers#10926

Open
aditya-prasad1 wants to merge 3 commits into
Icinga:masterfrom
aditya-prasad1:fix-raii-10813
Open

Replace manual memory management with RAII containers#10926
aditya-prasad1 wants to merge 3 commits into
Icinga:masterfrom
aditya-prasad1:fix-raii-10813

Conversation

@aditya-prasad1

@aditya-prasad1 aditya-prasad1 commented Jul 2, 2026

Copy link
Copy Markdown

Replaced manual temporary buffer management with std::vector in:

  • Process::IOThreadProc()
  • IdoMysqlConnection::Escape()

This removes explicit allocation/deallocation and relies on RAII while preserving existing behavior.

Closes #10813.

@cla-bot

cla-bot Bot commented Jul 2, 2026

Copy link
Copy Markdown

Thank you for your pull request. Before we can look at it, you'll need to sign a Contributor License Agreement (CLA).

Please follow instructions at https://icinga.com/company/contributor-agreement to sign the CLA.

After that, please reply here with a comment and we'll verify.

Contributors that have not signed yet: @aditya-prasad1

Details
  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Please contact us if you think this is the case.

  • If you signed the CLA as a corporation, your GitHub username may not have been submitted to us. Please reach out to the responsible person in your organization.

@aditya-prasad1

Copy link
Copy Markdown
Author

I've signed the CLA

@bobapple

bobapple commented Jul 2, 2026

Copy link
Copy Markdown
Member

@cla-bot check

@cla-bot cla-bot Bot added the cla/signed label Jul 2, 2026
@bobapple bobapple removed their assignment Jul 2, 2026

@jschmidt-icinga jschmidt-icinga left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In case you're unaware what the failed AUTHORS check means, you need to add yourself to the AUTHORS file. You can add a second commit to this PR, it doesn't have to be a separate PR.

PS: Never mind the failed debian:11 check. It does that sometimes, but it will likely go away on your next push and if it doesn't, you can just restart it (or let us worry about that).

PPS: Ideally make the change to lib/base/process.cpp and lib/db_ido_mysql/idomysqlconnection.cpp separate commits. Wouldn't block a merge in my eyes, but would be preferable.

Comment thread lib/base/process.cpp Outdated
Comment thread lib/db_ido_mysql/idomysqlconnection.cpp Outdated
Comment thread lib/db_ido_mysql/idomysqlconnection.cpp Outdated

@jschmidt-icinga jschmidt-icinga left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Aside from the whitespace errors below this looks good to me now.

When you're done with that, please squash down or reformat your commits so you have exactly three: One for process.cpp, one for idomysqlconnection.cpp and one for AUTHORS.

PS: Also maybe label the AUTHORS commit something like Add Aditya to AUTHORS file or just Update AUTHORS file to avoid first person.

Comment thread lib/db_ido_mysql/idomysqlconnection.cpp
@aditya-prasad1 aditya-prasad1 force-pushed the fix-raii-10813 branch 2 times, most recently from 361b17d to 829492c Compare July 3, 2026 14:13
@aditya-prasad1

Copy link
Copy Markdown
Author

@jschmidt-icinga Can you approve the CI check workflow?

@jschmidt-icinga jschmidt-icinga added this to the 2.17.0 milestone Jul 6, 2026
@jschmidt-icinga jschmidt-icinga enabled auto-merge July 6, 2026 06:40

@jschmidt-icinga jschmidt-icinga left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I just quickly fixed the whitespace myself, but now it looks ready to merge. Thank you for your contribution.

Comment thread lib/base/process.cpp
Comment on lines -600 to +603
HANDLE *handles = nullptr;
HANDLE *fhandles = nullptr;
std::vector<HANDLE> handles;
std::vector<HANDLE> fhandles;
#else /* _WIN32 */
pollfd *pfds = nullptr;
std::vector<pollfd> pfds;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm surprised that this PR does not remove any free() calls, but there aren't any for these. Does that mean that this fixes a memory leak (technically, it should only leak the allocation when exiting the process, so not something you would really notice)? Or asking differently: did someone check that these free() calls were just missing (there would be a possibility that there was an API that wants a malloc()-allocated pointer and takes ownership of it, at first glance, this doesn't look like this here, but better double-check it)?

(This is just a question for the moment, request changes is for the other comment.)

@jschmidt-icinga jschmidt-icinga Jul 6, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this was relying on Process::IOThreadProc() running for the entire program duration (the for(;;) loop is never escaped) and only ever using realloc() to allocate and free previous allocations. So yes, this had a memory leak, but it's only ever the last allocation that never gets deallocated when the program shuts down.

This doesn't really fix that though, since the for loop is still never escaped so the RAII doesn't really do anything here. Also this means the threads are never joined, which probably isn't ideal either... 🤦

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I didn't want to put a focus on fixing a more or less theoretical memory leak here, just on making sure that the free() wasn't missing intentionally.

Comment on lines -729 to -731
String result = String(to);
size_t escapedLength = m_Mysql->real_escape_string(&m_Connection, to.GetData().data(), utf8s.CStr(), length);

delete [] to;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

mysql_real_escape_string() may return (unsigned long)-1 to signal errors which would not be handled correctly now (I don't really know if it was before, didn't read anything on whether it is guaranteed to write '\0' into to on errors).

@jschmidt-icinga jschmidt-icinga Jul 6, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks like we set the SQL mode to a known value without NO_BACKSLASH_ESCAPE here:

Query("SET SESSION SQL_MODE='NO_AUTO_VALUE_ON_ZERO'");

Otherwise IDO wouldn't function, with or without this change, even if the output was always initialized with '\0'.

@Al2Klimov Al2Klimov Jul 7, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@jschmidt-icinga jschmidt-icinga disabled auto-merge July 6, 2026 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replace realloc(3) and maybe malloc(3) with std::vector

5 participants