chore(codeql): address pre-existing CodeQL alerts #5243#5244
Conversation
| 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
Show autofix suggestion
Hide autofix suggestion
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_TYPEcase, changeif (code >= static_cast<int>(NET_UNREACHABLE) && code < static_cast<int>(DESTINATION_UNREACHABLE_UNKNOWN))toif (code < static_cast<int>(DESTINATION_UNREACHABLE_UNKNOWN)). - In the
REDIRECT_MESSAGE_TYPEcase, similarly drop thecode >= static_cast<int>(REDIRECT_NETWORK)part. - In the
TIME_EXCEEDED_TYPEcase, drop thecode >= static_cast<int>(TIME_TO_LIVE)part.
No new methods or imports are needed; we only simplify the if conditions in the shown function.
| @@ -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]; |
| 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
Show autofix suggestion
Hide autofix suggestion
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.
| @@ -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]; |
| 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
Show autofix suggestion
Hide autofix suggestion
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.
| @@ -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]; |
c572e8b to
66da389
Compare
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
66da389 to
6d45b9d
Compare
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:
FTPSClientSessionTest.hsocket.impl()inPollSet.cppPOCO_DATA_INVALID_ROWinstead of-1in testExtractor.hWarnings fixed:
ch != eofcheck inMessageHeader.cppsleepTimereset logic inSQLChannel.cppfor proper exponential backoffstatic_cast<int>()for enum comparisons inICMPv4PacketImpl.cppoperator=inHTTPAuthenticationParams(Rule of Zero)SplitterChannel::close(),SocketImpl::close(),FTPClientSession::receiveServerReadyReply()Notices fixed:
MailMessage,AbstractConfiguration,XMLStreamParser,CompressDOMImplementation::hasFeature()XMLStreamParserMessageHeader.cppread()inPollSet.cppFalse positives suppressed with comments:
Test plan
cmake -B build-dir -DENABLE_TESTS=ON && cmake --build build-dir— builds cleanly