2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-05-11 14:40:53 +02:00
In PHP, references allow you to create multiple names for the same variable, enabling you to access and manipulate its value through different identifiers.
They are denoted by the ampersand symbol & placed before the variable name during declaration or assignment.
2021-04-28 16:49:39 +02:00
2023-05-11 14:40:53 +02:00
Any modification a method makes to a function parameter passed by reference will also be made to the original value.
2021-04-28 16:49:39 +02:00
2023-05-11 14:40:53 +02:00
This feature can be difficult to use correctly, particularly if the callee is not expecting a reference.
2021-04-28 16:49:39 +02:00
2023-05-11 14:40:53 +02:00
The improper use of references in function calls can make code less efficient rather than more efficient.
2021-04-28 16:49:39 +02:00
2023-05-11 14:40:53 +02:00
=== What is the potential impact?
2021-04-28 18:08:03 +02:00
2023-05-11 14:40:53 +02:00
While references can provide flexibility and efficiency in certain scenarios, they can also introduce complexity and potential pitfalls.
Incorrect usage of references may lead to unexpected behavior, difficult-to-debug code, and unintended side effects.
It's important to exercise caution and fully understand the implications before employing references.
2021-04-28 16:49:39 +02:00
2023-05-11 14:40:53 +02:00
== How to fix it in Core PHP
Refactor your code to not pass a reference as a function parameter.
=== Code examples
==== Noncompliant code example
[source,php,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
myfun(&$name); // Noncompliant
----
2023-05-11 14:40:53 +02:00
==== Compliant solution
2021-04-28 18:08:03 +02:00
2023-05-11 14:40:53 +02:00
[source,php,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
myfun($name);
----
2023-05-03 11:06:20 +02:00
== Resources
2021-04-28 16:49:39 +02:00
2023-05-11 14:40:53 +02:00
=== Standards
2022-04-07 08:53:59 -05:00
* https://cwe.mitre.org/data/definitions/374[MITRE, CWE-374] - Weakness Base Passing Mutable Objects to an Untrusted Method
2021-04-28 18:08:03 +02:00
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
2023-05-11 14:40:53 +02:00
2021-09-20 15:38:42 +02:00
== Implementation Specification
2023-05-11 14:40:53 +02:00
2021-09-20 15:38:42 +02:00
(visible only on this page)
include::message.adoc[]
2021-06-08 15:52:13 +02:00
'''
2023-05-11 14:40:53 +02:00
2021-06-02 20:44:38 +02:00
== Comments And Links
2023-05-11 14:40:53 +02:00
2021-06-02 20:44:38 +02:00
(visible only on this page)
include::comments-and-links.adoc[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]