CodeQL documentation

Impossible array cast

ID: cs/impossible-array-cast
Kind: problem
Security severity: 
Severity: error
Precision: high
Tags:
   - quality
   - reliability
   - correctness
Query suites:
   - csharp-security-and-quality.qls

Click to see the query in the CodeQL repository

Some casts between array types are guaranteed to fail at runtime: the cast from Object[] to String[] will always fail, even if all the elements of the array are strings. Casts identified by this check either fail immediately, or (in the case of arrays with parameterized types) cause an InvalidCastException later on in the code.

Recommendation

Change the array creation expression to construct an array object of the right type.

Example

class ImpossibleArrayCast
{
    static void Main(string[] args)
    {
        // This will result in an InvalidCastException.
        String[] strs = (String[])new Object[] { "hello", "world" };
    }
}
  • © GitHub, Inc.
  • Terms
  • Privacy
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy