Skip to content

Add missing pointer deletion in AutoPtr move assignment operator#9005

Open
Noremos wants to merge 2 commits intoFirebirdSQL:masterfrom
Noremos:master_autoptr_missing_clear
Open

Add missing pointer deletion in AutoPtr move assignment operator#9005
Noremos wants to merge 2 commits intoFirebirdSQL:masterfrom
Noremos:master_autoptr_missing_clear

Conversation

@Noremos
Copy link
Copy Markdown
Contributor

@Noremos Noremos commented Apr 23, 2026

No description provided.

@sim1984
Copy link
Copy Markdown
Contributor

sim1984 commented Apr 24, 2026

Move assignment can be implemented more simply:

		AutoPtr& operator=(AutoPtr&& r) noexcept
		{
			std::swap(ptr, r.ptr);

			return *this;
		}

In this case, you don't need to call any cleanup functions. The destructor of the moved object will do the cleanup. You can also safely remove the self-assignment check.

@aafemt
Copy link
Copy Markdown
Contributor

aafemt commented Apr 24, 2026

Is self-swap a defined behavior? I don't see mention of it on https://en.cppreference.com/cpp/algorithm/swap

The problem may be that lifetime of the old content become unpredictable in common case. Moving from global static object will let it live forever that may be unexpected. Of course, in most cases the target pointer is empty, that's why this code lived for years unnoticed.

@sim1984
Copy link
Copy Markdown
Contributor

sim1984 commented Apr 24, 2026

Is self-swap a defined behavior? I don't see mention of it on https://en.cppreference.com/cpp/algorithm/swap

For trivial types, which pointers are, yes.

The problem may be that lifetime of the old content become unpredictable in common case. Moving from global static object will let it live forever that may be unexpected. Of course, in most cases the target pointer is empty, that's why this code lived for years unnoticed.

Do you seriously think that global static objects will be wrapped in AutoPtr? And even if they are, who would think of moving them?

@hvlad
Copy link
Copy Markdown
Member

hvlad commented Apr 24, 2026

Of course, in most cases the target pointer is empty, that's why this code lived for years unnoticed.

The real reason is that this code never used.
I've just commented out both move ctor and move assignment operator and succesfully build whole solution.

@hvlad
Copy link
Copy Markdown
Member

hvlad commented Apr 24, 2026

Move assignment can be implemented more simply:

Don't replace move assigment by swap. This is different things for different purposes.

@Noremos
Copy link
Copy Markdown
Contributor Author

Noremos commented Apr 24, 2026

The real reason is that this code never used. I've just commented out both move ctor and move assignment operator and succesfully build whole solution.

I use this in my JSON implementation. And that's how I found the bug.

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.

4 participants