Skip to content

chore(codeql): address pre-existing CodeQL alerts #5243#5244

Merged
matejk merged 2 commits intomainfrom
5243-codeql-alerts
Apr 2, 2026
Merged

chore(codeql): address pre-existing CodeQL alerts #5243#5244
matejk merged 2 commits intomainfrom
5243-codeql-alerts

Conversation

@matejk
Copy link
Copy Markdown
Contributor

@matejk matejk commented Mar 15, 2026

Summary

Resolves pre-existing CodeQL alerts surfaced by PR #5235. Addresses errors, warnings, and notices across Foundation, Net, XML, JSON, Data, Zip, Prometheus, and Util components.

Closes #5243

Changes

Errors fixed:

  • Fix duplicate include guard in FTPSClientSessionTest.h
  • Add null checks for socket.impl() in PollSet.cpp
  • Use POCO_DATA_INVALID_ROW instead of -1 in test Extractor.h

Warnings fixed:

  • Remove tautological ch != eof check in MessageHeader.cpp
  • Fix sleepTime reset logic in SQLChannel.cpp for proper exponential backoff
  • Add explicit static_cast<int>() for enum comparisons in ICMPv4PacketImpl.cpp
  • Default operator= in HTTPAuthenticationParams (Rule of Zero)
  • Use qualified calls in destructors/constructors: SplitterChannel::close(), SocketImpl::close(), FTPClientSession::receiveServerReadyReply()

Notices fixed:

  • Remove commented-out code across 6 files
  • Remove trailing semicolons from doc comments (8 files)
  • Rename shadowing variables in MailMessage, AbstractConfiguration, XMLStreamParser, Compress
  • Simplify complex condition in DOMImplementation::hasFeature()
  • Convert trivial switch to if/else in XMLStreamParser
  • Fix Q-encoding bounds check and improve error handling in MessageHeader.cpp
  • Add error check for eventfd read() in PollSet.cpp

False positives suppressed with comments:

  • Authentication bypass by spoofing (proxy rewriting, IP parsing)
  • Cleartext transmission (base socket layer)
  • XXE (features exist to disable)
  • Local address stored in non-local memory (observer/DOM/SAX patterns)
  • Float equality (intentional in dynamic type system)
  • Raw arrays in interfaces (SAX specification API)

Test plan

  • cmake -B build-dir -DENABLE_TESTS=ON && cmake --build build-dir — builds cleanly
  • Foundation, XML, JSON, Util, Prometheus, Data, DataSQLite, Zip test suites all pass
  • Net test suite individual tests pass (suite times out due to network tests)

case DESTINATION_UNREACHABLE_TYPE:
if (code >= NET_UNREACHABLE && code < DESTINATION_UNREACHABLE_UNKNOWN)
// lower-bound check is defensive (enum values may change)
if (code >= static_cast<int>(NET_UNREACHABLE) && code < static_cast<int>(DESTINATION_UNREACHABLE_UNKNOWN))

Check warning

Code scanning / CodeQL

Comparison result is always the same

Comparison is always true because code >= 0.

Copilot Autofix

AI about 1 month ago

In general, to fix this type of issue you want the comparison to express a condition that can be either true or false given the actual runtime range of the operands. If part of a compound condition is provably always true (for example, checking that an unsigned‑derived value is >= 0), that part should be removed or the type/range assumptions revisited.

For this specific case in ICMPv4PacketImpl::errorDescription (Net/src/ICMPv4PacketImpl.cpp), the intent is to ensure code is within the valid range for indexing the corresponding error‑description arrays. Because code is derived from an ICMP header code field, which is an 8‑bit non‑negative value, only the upper bound of the range check is meaningful. The best fix that preserves behaviour is to remove the redundant lower‑bound checks while keeping the upper‑bound checks. Concretely:

  • In the DESTINATION_UNREACHABLE_TYPE case, change if (code >= static_cast<int>(NET_UNREACHABLE) && code < static_cast<int>(DESTINATION_UNREACHABLE_UNKNOWN)) to if (code < static_cast<int>(DESTINATION_UNREACHABLE_UNKNOWN)).
  • In the REDIRECT_MESSAGE_TYPE case, similarly drop the code >= static_cast<int>(REDIRECT_NETWORK) part.
  • In the TIME_EXCEEDED_TYPE case, drop the code >= static_cast<int>(TIME_TO_LIVE) part.

No new methods or imports are needed; we only simplify the if conditions in the shown function.

Suggested changeset 1
Net/src/ICMPv4PacketImpl.cpp

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/Net/src/ICMPv4PacketImpl.cpp b/Net/src/ICMPv4PacketImpl.cpp
--- a/Net/src/ICMPv4PacketImpl.cpp
+++ b/Net/src/ICMPv4PacketImpl.cpp
@@ -215,8 +215,8 @@
 	switch (msgType)
 	{
 	case DESTINATION_UNREACHABLE_TYPE:
-		// lower-bound check is defensive (enum values may change)
-		if (code >= static_cast<int>(NET_UNREACHABLE) && code < static_cast<int>(DESTINATION_UNREACHABLE_UNKNOWN))
+		// check that code is within bounds of DESTINATION_UNREACHABLE_CODE
+		if (code < static_cast<int>(DESTINATION_UNREACHABLE_UNKNOWN))
 			err << DESTINATION_UNREACHABLE_CODE[code];
 		else
 			err << DESTINATION_UNREACHABLE_CODE[DESTINATION_UNREACHABLE_UNKNOWN];
@@ -227,16 +227,16 @@
 		break;
 
 	case REDIRECT_MESSAGE_TYPE:
-		// lower-bound check is defensive (enum values may change)
-		if (code >= static_cast<int>(REDIRECT_NETWORK) && code < static_cast<int>(REDIRECT_MESSAGE_UNKNOWN))
+		// check that code is within bounds of REDIRECT_MESSAGE_CODE
+		if (code < static_cast<int>(REDIRECT_MESSAGE_UNKNOWN))
 			err << REDIRECT_MESSAGE_CODE[code];
 		else
 			err << REDIRECT_MESSAGE_CODE[REDIRECT_MESSAGE_UNKNOWN];
 		break;
 
 	case TIME_EXCEEDED_TYPE:
-		// lower-bound check is defensive (enum values may change)
-		if (code >= static_cast<int>(TIME_TO_LIVE) && code < static_cast<int>(TIME_EXCEEDED_UNKNOWN))
+		// check that code is within bounds of TIME_EXCEEDED_CODE
+		if (code < static_cast<int>(TIME_EXCEEDED_UNKNOWN))
 			err << TIME_EXCEEDED_CODE[code];
 		else
 			err << TIME_EXCEEDED_CODE[TIME_EXCEEDED_UNKNOWN];
EOF
@@ -215,8 +215,8 @@
switch (msgType)
{
case DESTINATION_UNREACHABLE_TYPE:
// lower-bound check is defensive (enum values may change)
if (code >= static_cast<int>(NET_UNREACHABLE) && code < static_cast<int>(DESTINATION_UNREACHABLE_UNKNOWN))
// check that code is within bounds of DESTINATION_UNREACHABLE_CODE
if (code < static_cast<int>(DESTINATION_UNREACHABLE_UNKNOWN))
err << DESTINATION_UNREACHABLE_CODE[code];
else
err << DESTINATION_UNREACHABLE_CODE[DESTINATION_UNREACHABLE_UNKNOWN];
@@ -227,16 +227,16 @@
break;

case REDIRECT_MESSAGE_TYPE:
// lower-bound check is defensive (enum values may change)
if (code >= static_cast<int>(REDIRECT_NETWORK) && code < static_cast<int>(REDIRECT_MESSAGE_UNKNOWN))
// check that code is within bounds of REDIRECT_MESSAGE_CODE
if (code < static_cast<int>(REDIRECT_MESSAGE_UNKNOWN))
err << REDIRECT_MESSAGE_CODE[code];
else
err << REDIRECT_MESSAGE_CODE[REDIRECT_MESSAGE_UNKNOWN];
break;

case TIME_EXCEEDED_TYPE:
// lower-bound check is defensive (enum values may change)
if (code >= static_cast<int>(TIME_TO_LIVE) && code < static_cast<int>(TIME_EXCEEDED_UNKNOWN))
// check that code is within bounds of TIME_EXCEEDED_CODE
if (code < static_cast<int>(TIME_EXCEEDED_UNKNOWN))
err << TIME_EXCEEDED_CODE[code];
else
err << TIME_EXCEEDED_CODE[TIME_EXCEEDED_UNKNOWN];
Copilot is powered by AI and may make mistakes. Always verify output.
Unable to commit as this autofix suggestion is now outdated
case REDIRECT_MESSAGE_TYPE:
if (code >= REDIRECT_NETWORK && code < REDIRECT_MESSAGE_UNKNOWN)
// lower-bound check is defensive (enum values may change)
if (code >= static_cast<int>(REDIRECT_NETWORK) && code < static_cast<int>(REDIRECT_MESSAGE_UNKNOWN))

Check warning

Code scanning / CodeQL

Comparison result is always the same

Comparison is always true because code >= 0.

Copilot Autofix

AI about 1 month ago

In general, when a comparison is always the same truth value, remove or adjust the redundant part so that the condition can actually distinguish valid and invalid ranges, or rewrite the logic in terms of clearly defined bounds. Here, the intent in the REDIRECT_MESSAGE_TYPE case is to use code as an index into REDIRECT_MESSAGE_CODE only when it is in the valid range of redirect codes and fall back to an “unknown” entry otherwise.

The best fix while preserving behavior is to keep only the upper‑bound check, because code is already known to be non‑negative: replace

if (code >= static_cast<int>(REDIRECT_NETWORK) && code < static_cast<int>(REDIRECT_MESSAGE_UNKNOWN))
    err << REDIRECT_MESSAGE_CODE[code];
else
    err << REDIRECT_MESSAGE_CODE[REDIRECT_MESSAGE_UNKNOWN];

with

if (code < static_cast<int>(REDIRECT_MESSAGE_UNKNOWN))
    err << REDIRECT_MESSAGE_CODE[code];
else
    err << REDIRECT_MESSAGE_CODE[REDIRECT_MESSAGE_UNKNOWN];

This leaves indexing logic unchanged for all non‑negative code values and removes the redundant condition that CodeQL flags. No new methods, imports, or definitions are needed; the change is localized to the REDIRECT_MESSAGE_TYPE case in Net/src/ICMPv4PacketImpl.cpp, around lines 229–235.

Suggested changeset 1
Net/src/ICMPv4PacketImpl.cpp

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/Net/src/ICMPv4PacketImpl.cpp b/Net/src/ICMPv4PacketImpl.cpp
--- a/Net/src/ICMPv4PacketImpl.cpp
+++ b/Net/src/ICMPv4PacketImpl.cpp
@@ -227,8 +227,8 @@
 		break;
 
 	case REDIRECT_MESSAGE_TYPE:
-		// lower-bound check is defensive (enum values may change)
-		if (code >= static_cast<int>(REDIRECT_NETWORK) && code < static_cast<int>(REDIRECT_MESSAGE_UNKNOWN))
+		// check that code is within the known redirect message range
+		if (code < static_cast<int>(REDIRECT_MESSAGE_UNKNOWN))
 			err << REDIRECT_MESSAGE_CODE[code];
 		else
 			err << REDIRECT_MESSAGE_CODE[REDIRECT_MESSAGE_UNKNOWN];
EOF
@@ -227,8 +227,8 @@
break;

case REDIRECT_MESSAGE_TYPE:
// lower-bound check is defensive (enum values may change)
if (code >= static_cast<int>(REDIRECT_NETWORK) && code < static_cast<int>(REDIRECT_MESSAGE_UNKNOWN))
// check that code is within the known redirect message range
if (code < static_cast<int>(REDIRECT_MESSAGE_UNKNOWN))
err << REDIRECT_MESSAGE_CODE[code];
else
err << REDIRECT_MESSAGE_CODE[REDIRECT_MESSAGE_UNKNOWN];
Copilot is powered by AI and may make mistakes. Always verify output.
Unable to commit as this autofix suggestion is now outdated
case TIME_EXCEEDED_TYPE:
if (code >= TIME_TO_LIVE && code < TIME_EXCEEDED_UNKNOWN)
// lower-bound check is defensive (enum values may change)
if (code >= static_cast<int>(TIME_TO_LIVE) && code < static_cast<int>(TIME_EXCEEDED_UNKNOWN))

Check warning

Code scanning / CodeQL

Comparison result is always the same

Comparison is always true because code >= 0.

Copilot Autofix

AI about 1 month ago

In general, to fix “comparison result is always the same” issues, you either (1) remove the redundant part of the condition if it truly adds no value, or (2) adjust the operands or types to match the intended semantics if the redundancy is accidental. Here, the pattern is repeated for several ICMP message types: “lower-bound check is defensive (enum values may change)” followed by a code >= ... && code < ... guard. For the TIME_EXCEEDED_TYPE case, CodeQL determined the lower bound is always satisfied, so the first comparison is dead code.

The safest fix that does not change observable behavior is to drop the always‑true lower‑bound comparison in this specific case and rely solely on the upper bound check, which is the one that actually protects the array indexing. Concretely, in Net/src/ICMPv4PacketImpl.cpp, within ICMPv4PacketImpl::errorDescription, in the case TIME_EXCEEDED_TYPE: block, replace:

		// lower-bound check is defensive (enum values may change)
		if (code >= static_cast<int>(TIME_TO_LIVE) && code < static_cast<int>(TIME_EXCEEDED_UNKNOWN))
			err << TIME_EXCEEDED_CODE[code];
		else
			err << TIME_EXCEEDED_CODE[TIME_EXCEEDED_UNKNOWN];

with:

		if (code < static_cast<int>(TIME_EXCEEDED_UNKNOWN))
			err << TIME_EXCEEDED_CODE[code];
		else
			err << TIME_EXCEEDED_CODE[TIME_EXCEEDED_UNKNOWN];

This keeps the same behavior for all possible code values (since the removed condition was always true under the current type/value assumptions) and removes the condition that the analyzer flagged. No additional methods, imports, or definitions are required.

Suggested changeset 1
Net/src/ICMPv4PacketImpl.cpp

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/Net/src/ICMPv4PacketImpl.cpp b/Net/src/ICMPv4PacketImpl.cpp
--- a/Net/src/ICMPv4PacketImpl.cpp
+++ b/Net/src/ICMPv4PacketImpl.cpp
@@ -235,8 +235,7 @@
 		break;
 
 	case TIME_EXCEEDED_TYPE:
-		// lower-bound check is defensive (enum values may change)
-		if (code >= static_cast<int>(TIME_TO_LIVE) && code < static_cast<int>(TIME_EXCEEDED_UNKNOWN))
+		if (code < static_cast<int>(TIME_EXCEEDED_UNKNOWN))
 			err << TIME_EXCEEDED_CODE[code];
 		else
 			err << TIME_EXCEEDED_CODE[TIME_EXCEEDED_UNKNOWN];
EOF
@@ -235,8 +235,7 @@
break;

case TIME_EXCEEDED_TYPE:
// lower-bound check is defensive (enum values may change)
if (code >= static_cast<int>(TIME_TO_LIVE) && code < static_cast<int>(TIME_EXCEEDED_UNKNOWN))
if (code < static_cast<int>(TIME_EXCEEDED_UNKNOWN))
err << TIME_EXCEEDED_CODE[code];
else
err << TIME_EXCEEDED_CODE[TIME_EXCEEDED_UNKNOWN];
Copilot is powered by AI and may make mistakes. Always verify output.
Unable to commit as this autofix suggestion is now outdated
@matejk matejk requested review from aleks-f and obiltschnig March 15, 2026 12:55
@matejk matejk force-pushed the 5243-codeql-alerts branch 3 times, most recently from c572e8b to 66da389 Compare March 26, 2026 13:15
matejk added 2 commits April 2, 2026 17:44
Fix errors: duplicate include guard, null checks, default parameter,
tautological comparisons, redundant operator=, virtual calls from
ctor/dtor.

Fix warnings: comparison always same, Rule of Two, enum casts.

Fix notices: remove commented-out code, rename shadowing variables,
simplify complex condition, fix Q-encoding bounds check.

Suppress false positives: auth-bypass, cleartext-transmission, XXE,
local-address-stored, float-equality, raw-array-interface.
- Application.cpp: document that init() indirectly calls virtual
  defineOptions(), not just that init() itself is non-virtual
- SAXParser.cpp: external entities are disabled by default, not enabled
@matejk matejk force-pushed the 5243-codeql-alerts branch from 66da389 to 6d45b9d Compare April 2, 2026 15:52
@matejk matejk merged commit cbfd1bc into main Apr 2, 2026
1 of 97 checks passed
@matejk matejk deleted the 5243-codeql-alerts branch April 2, 2026 15:55
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.

Address pre-existing CodeQL alerts surfaced by PR #5235

2 participants