---
title: "CWE-240: Improper Handling of Inconsistent Structural Elements"
url: https://radicalnotion.ai/cwe/cwe-240
type: cwe
description: "The product does not handle or incorrectly handles when two or more structural elements should be consistent, but are not."
updated: 2026-11-10
source: RadicalNotion.AI
license: CC BY-NC 4.0 (https://creativecommons.org/licenses/by-nc/4.0/)
---

# CWE-240: Improper Handling of Inconsistent Structural Elements

- **Abstraction:** Class
- **Structure:** Simple
- **Status:** Draft
- **CVEs with this as primary weakness:** 4
- **Of those, in CISA KEV:** 0
- **New in 2026:** 0
- **CWE catalog version:** 4.21

## Description

The product does not handle or incorrectly handles when two or more structural elements should be consistent, but are not.

## Modes of introduction

| Phase | Note |
| --- | --- |
| Implementation | - |

## Common consequences

| Scope | Impact | Note |
| --- | --- | --- |
| Integrity, Other | Varies by Context, Unexpected State | If multiple structural elements are inconsistent, then using these elements could place the system into an unexpected state that could lead to a crash, consume available resources, or other unintended behaviors. |

## Demonstrative examples

### Example 1

In the following C/C++ example, the method processMessageFromSocket() will get a message from a socket, save the message into a buffer, and place the contents of the buffer into a structure that contains the message length and the message body. Then a for loop is used to copy the message body into a local character string, which will be passed to another method for processing.

Bad code:

```c
int processMessageFromSocket(int socket) {
    // initialize variable to represent the failure case by default
    int success = -1;
    char buffer[BUFFER_SIZE];
    char message[MESSAGE_SIZE];

    // get message from socket and store into buffer
    // for clarity of this example, ignore the possibility that buffer > BUFFER_SIZE
    if (getMessage(socket, buffer, BUFFER_SIZE) > 0) {

        // place contents of the buffer into message structure
        ExMessage *msg = recastBuffer(buffer);

        // copy message body into string for processing
        int index;
        for (index = 0; index < msg->msgLength; index++) {
            message[index] = msg->msgBody[index];
        }
        message[index] = '\0';
        // process message
        success = processMessage(message);
    }
    return success;
}
```

However, the message length variable (msgLength) from the structure is used as the condition for ending the for loop without validating that msgLength accurately reflects the actual length of the message body (CWE-606). If msgLength indicates a length that is longer than the size of a message body (CWE-130), then this can result in a buffer over-read by reading past the end of the buffer (CWE-126).

## Observed examples

| CVE | Description |
| --- | --- |
| CVE-2014-0160 | Chain: "Heartbleed" bug receives an inconsistent length parameter (CWE-130) enabling an out-of-bounds read (CWE-126), returning memory that could include private cryptographic keys and other sensitive data. |
| CVE-2009-2299 | Web application firewall consumes excessive memory when an HTTP request contains a large Content-Length value but no POST data. |

## Real CVEs with this weakness

| CVE | Title | Severity | CVSS | EPSS | CISA KEV | Published |
| --- | --- | --- | --- | --- | --- | --- |
| CVE-2021-31890 | Improper Handling of Inconsistent Structural Elements in Siemens SIMOTICS CONNECT 400 | critical | 9.1 | - | No | 2021-11-09 |
| CVE-2025-4321 | DoS in RS9116W-WiSeConnect L2CAP protocol due to reception of malformed packets | high | 7.1 | - | No | 2025-11-17 |
| CVE-2022-40957 | Mozilla: Incoherent instruction cache when building WASM on ARM64 | medium | 6.5 | - | No | 2022-12-22 |
| CVE-2021-21640 | View name validation bypass in Jenkins | medium | 4.3 | - | No | 2021-04-07 |

## Related weaknesses

| Relationship | CWE | Name |
| --- | --- | --- |
| Parent | [CWE-237](https://radicalnotion.ai/cwe/cwe-237) | Improper Handling of Structural Elements |
| Parent | [CWE-707](https://radicalnotion.ai/cwe/cwe-707) | Improper Neutralization |
| Child | [CWE-130](https://radicalnotion.ai/cwe/cwe-130) | Improper Handling of Length Parameter Inconsistency |

## Taxonomy mappings

| Taxonomy | Entry | Id | Fit |
| --- | --- | --- | --- |
| PLOVER | Inconsistent Elements | - | - |

## Frequently asked questions

### What is CWE-240?

The product does not handle or incorrectly handles when two or more structural elements should be consistent, but are not.

### What CVEs are caused by CWE-240?

4 recorded CVEs are attributed to CWE-240, including CVE-2021-31890, CVE-2025-4321 and CVE-2022-40957.

### What are the consequences of CWE-240?

Exploiting CWE-240 can lead to: Varies by Context, Unexpected State.

### Is CWE-240 actively exploited?

4 recorded CVEs are caused by CWE-240; none are currently in CISA's KEV catalog of actively exploited flaws.

Primary source: [MITRE CWE](https://cwe.mitre.org/data/definitions/240.html)

---

Source: [RadicalNotion.AI](https://radicalnotion.ai/cwe/cwe-240). Licensed CC BY-NC 4.0; attribute RadicalNotion.AI and link the source page when you reuse this data.
