GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CodeQL documentation
  • CodeQL overview
  • Writing CodeQL queries
  • CodeQL language guides
  • QL language reference
  • CodeQL standard-libraries
  • CodeQL query help
  • CodeQL repository
  • QL publications
  • CodeQL query help for C and C++
  • CodeQL query help for C#
  • CodeQL query help for GitHub Actions
  • CodeQL query help for Go
  • CodeQL query help for Java and Kotlin

This view

  • Original page
  • New address

GeezerVison is showing codeql.github.com — Conflicting function declarations — CodeQL query help documentation

Conflicting function declarations¶

ID: js/function-declaration-conflict
Kind: problem
Security severity: 
Severity: error
Precision: high
Tags:
   - quality
   - reliability
   - correctness
   - external/cwe/cwe-563
Query suites:
   - javascript-code-quality.qls
   - javascript-security-and-quality.qls

Click to see the query in the CodeQL repository

If two functions with the same name are declared in the same scope, one of the declarations overrides the other without warning. This makes the code hard to read and maintain. In some cases, which declaration overrides which may be platform dependent.

Recommendation¶

If the two declarations are duplicates, remove one of them. Otherwise, rename one of them to distinguish the two functions, or turn the function declarations into assignments of function expressions to the same local variable.

Example¶

In the following example, function converter is defined differently in the two branches of the if statement. However, the function definition appearing later in the program text will override the one appearing earlier, independent of the flow of execution through the if statement, so in this case it is always the second function that is returned. (Note that this may not be true on older browsers.)

function getConverter(dir) {
	if (dir === 'c2f') {
		function converter(c) {
			return c * 9/5 + 32;
		}
	} else {
		function converter(f) {
			return (f - 32) * 5/9;
		}
	}
	return converter;
}

To address this problem, introduce a local variable converter and convert the function declarations into assignments of function expressions to this variable:

function getConverter(dir) {
	var converter;
	if (dir === 'c2f') {
		converter = function (c) {
			return c * 9/5 + 32;
		};
	} else {
		converter = function (f) {
			return (f - 32) * 5/9;
		};
	}
	return converter;
}

References¶

  • Ecma International, ECMAScript Language Definition, 5.1 Edition, Section 10.5. ECMA, 2011.

  • Common Weakness Enumeration: CWE-563.

  • © GitHub, Inc.
  • Terms
  • Privacy
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.