Fix NaN comparison, loop logic, and null safety checks in Person.js - #236
Open
sonarqube-agent[bot] wants to merge 1 commit into
Open
Fix NaN comparison, loop logic, and null safety checks in Person.js#236sonarqube-agent[bot] wants to merge 1 commit into
sonarqube-agent[bot] wants to merge 1 commit into
Conversation
Fixed issues: - AZ9rJA3YkdvHrJAQ8SGf for javascript:S2688 rule - AZ9rJA3YkdvHrJAQ8SGh for javascript:S2688 rule - AZ9rJA3YkdvHrJAQ8SGl for javascript:S2251 rule - AZ9rJA3YkdvHrJAQ8SGn for javascript:S2259 rule Generated by SonarQube Agent (task: 099d490d-a89e-4e7e-a0ee-f68a29c1d7fa)
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



This PR resolves four MAJOR SonarQube issues in Person.js by replacing unsafe NaN comparisons with Number.isNaN(), converting a broken decrementing loop to a for-of loop, and fixing null dereference vulnerabilities by correcting logical operators from AND to OR in null checks. These fixes eliminate runtime errors and logic bugs that could cause infinite loops, TypeErrors, and incorrect conditional behavior.
View Project in SonarCloud
Fixed Issues
javascript:S2688 - Use the isNaN function to compare with NaN. • MAJOR • View issue
Location:
sonar-scanner/src/javascript/Person.js:17Why is this an issue?
In JavaScript,
NaNstands for "Not-a-Number." It is a special value representing a numeric data type that is not a valid number.NaNis returned as a result when an arithmetic operation or mathematical function is performed, and the result is undefined or unrepresentable as a valid number.What changed
This hunk replaces the direct comparison
a !== NaNwith!Number.isNaN(a). SinceNaN !== NaNis always true in JavaScript, the original conditiona !== NaNwas always true regardless of the value ofa. Using!Number.isNaN(a)correctly checks whetherais not NaN.javascript:S2688 - Use the isNaN function to compare with NaN. • MAJOR • View issue
Location:
sonar-scanner/src/javascript/Person.js:20Why is this an issue?
In JavaScript,
NaNstands for "Not-a-Number." It is a special value representing a numeric data type that is not a valid number.NaNis returned as a result when an arithmetic operation or mathematical function is performed, and the result is undefined or unrepresentable as a valid number.What changed
This hunk replaces the
forloop that had a decrementing counter (i--) with a termination condition that required incrementing (i < strings.length), which would cause an infinite loop sinceiwould never reach the stop condition. By switching to afor...ofloop, the iteration over thestringsarray is handled correctly without any manual counter management.javascript:S2251 - "i" is decremented and will never reach its stop condition. • MAJOR • View issue 1
javascript:S2259 - TypeError can be thrown as expression might be null or undefined here. • MAJOR • View issue 2
Location:
sonar-scanner/src/javascript/Person.js:24Why is this an issue?
In a
forloop, the update clause is responsible for modifying the loop counter variable in the appropriate direction to control the loop’s iteration. It determines how the loop counter variable changes with each iteration of the loop. The loop counter should move in the right direction to prevent infinite loops or unexpected behavior.In JavaScript,
nullandundefinedare primitive values that do not have properties or methods. When accessing a property on anullorundefinedvalue, JavaScript tries to access the property of an object that does not exist, which results in aTypeError.What changed
This hunk changes the logical operator from
&&to||in the null check condition. The original codestr == null && str.length == 0would attempt to access.lengthonstreven whenstris null or undefined (since both conditions of&&are evaluated when the first is true), causing a TypeError. By changing to||, the condition short-circuits: ifstr == nullis true,str.length == 0is never evaluated, preventing the null dereference. This fixes both the TypeError that can be thrown when accessing.lengthon a null or undefined expression, and the incorrect loop counter direction issue that is resolved by the earlier hunk converting to a for-of loop — together these hunks ensure the loop body operates safely on each string.SonarQube Remediation Agent uses AI. Check for mistakes.