Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible DnsServer memory leak reported by coverity #2870

Merged
merged 2 commits into from
Jul 24, 2024

Conversation

slaff
Copy link
Contributor

@slaff slaff commented Jul 24, 2024

No description provided.

@slaff slaff added this to the 5.2.1 milestone Jul 24, 2024
Comment on lines 87 to 90
if(requestLen != buf->tot_len) {
delete[] buffer;
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I was chewing on whether this check is actually necessary since by inspection we pass buf->tot_len so the call is guaranteed to succeed. Hence this is really just shutting up the static analyser.

If we just remove lines 87-89 the code becomes much simpler.

We should also remove line 101 as I cannot see any use case for the user-provided callback here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Something like these lines below ?

void DnsServer::onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort)
{
	debug_d("DNS REQ from %s:%d", remoteIP.toString().c_str(), remotePort);

	// Allocated buffer with additional room for answer
	char* buffer = new char[buf->tot_len + MAX_ANSWER_LENGTH];
	if(buffer == nullptr) {
		return;
	}

	unsigned requestLen = pbuf_copy_partial(buf, buffer, buf->tot_len, 0);
	
	debug_hex(DBG, "< DNS", buffer, requestLen);

	auto responseLen = processQuestion(buffer, requestLen);
	if(responseLen != 0) {
		debug_hex(DBG, "> DNS", buffer, responseLen);
		sendTo(remoteIP, remotePort, buffer, responseLen);
	}

	delete[] buffer;
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, that's much cleaner!

@mikee47 mikee47 changed the title Fix memory leak reported by coverity. Possible DnsServer memory leak reported by coverity Jul 24, 2024
@slaff slaff requested a review from mikee47 July 24, 2024 08:56
@slaff slaff merged commit 16cf83d into SmingHub:develop Jul 24, 2024
46 checks passed
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.

3 participants