Creation of Immutable Text Using String Concatenation

Incomplete Base
Structure: Simple
Description

This weakness occurs when code builds a string within a loop using simple concatenation (like +=), which unintentionally creates new string objects every time instead of efficiently appending to a buffer.

Extended Description

When you construct a string inside a loop—such as a FOR or WHILE loop—using the '+=' operator to add content, the programming language typically creates a brand new string object in memory during each iteration. This happens because standard strings are immutable; they can't be changed after creation. The old string content is copied into a new, larger object along with the appended text, making this process surprisingly resource-intensive. This pattern can lead to significant performance slowdowns, especially when handling large datasets or many iterations. While not a security vulnerability in itself, if an attacker can influence the input that controls these loops, they could trigger excessive resource consumption, leading to a denial-of-service (DoS) condition. The fix is to use a dedicated mutable text buffer or builder class designed for this scenario, which modifies content in place without creating unnecessary intermediate objects.

Common Consequences 1
Scope: Other

Impact: Reduce Performance

References 1
Automated Source Code Performance Efficiency Measure (ASCPEM)
Object Management Group (OMG)
01-2016
ID: REF-959
Related Weaknesses
Taxonomy Mapping
  • OMG ASCPEM