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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sming/Components/Network/src/Network/DnsServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ void DnsServer::onReceive(pbuf* buf, IpAddress remoteIP, uint16_t remotePort)

unsigned requestLen = pbuf_copy_partial(buf, buffer, buf->tot_len, 0);
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!


Expand Down
Loading