---
title: "CWE-195: Signed to Unsigned Conversion Error"
url: https://radicalnotion.ai/cwe/cwe-195
type: cwe
description: "The product uses a signed primitive and performs a cast to an unsigned primitive, which can produce an unexpected value if the value of the signed primitive can not be represented using an unsigned primitive."
updated: 2026-11-10
source: RadicalNotion.AI
license: CC BY-NC 4.0 (https://creativecommons.org/licenses/by-nc/4.0/)
---

# CWE-195: Signed to Unsigned Conversion Error

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

## Description

The product uses a signed primitive and performs a cast to an unsigned primitive, which can produce an unexpected value if the value of the signed primitive can not be represented using an unsigned primitive.

## Modes of introduction

| Phase | Note |
| --- | --- |
| Implementation | It is dangerous to rely on implicit casts between signed and unsigned numbers because the result can take on an unexpected value and violate assumptions made by the program. |

## Applicable platforms

**Languages:** C (Undetermined), C++ (Undetermined)

## Common consequences

| Scope | Impact | Note |
| --- | --- | --- |
| Integrity | Unexpected State, Modify Memory, Read Memory | Conversion between signed and unsigned values can lead to a variety of errors, but from a security standpoint is most commonly associated with integer overflow and buffer overflow vulnerabilities. Often, functions will return negative values to indicate a failure. When the result of a function is to be used as a size parameter, using these negative return values can have unexpected results. For example, if negative size values are passed to the standard memory copy or allocation functions, they will be implicitly cast to a large unsigned value. This may lead to an exploitable buffer overflow or underflow condition. |

## Detection methods

### Automated Static Analysis

Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)

Effectiveness: High

## Demonstrative examples

### Example 1

In this example the variable amount can hold a negative value when it is returned. Because the function is declared to return an unsigned int, amount will be implicitly converted to unsigned.

Bad code:

```c
unsigned int readdata () {
    int amount = 0;
    ...
    if (result == ERROR)
    amount = -1;
    ...
    return amount;
}
```

If the error condition in the code above is met, then the return value of readdata() will be 4,294,967,295 on a system that uses 32-bit integers.

### Example 2

In this example, depending on the return value of accessmainframe(), the variable amount can hold a negative value when it is returned. Because the function is declared to return an unsigned value, amount will be implicitly cast to an unsigned number.

Bad code:

```c
unsigned int readdata () {
    int amount = 0;
    ...
    amount = accessmainframe();
    ...
    return amount;
}
```

If the return value of accessmainframe() is -1, then the return value of readdata() will be 4,294,967,295 on a system that uses 32-bit integers.

### Example 3

The following code is intended to read an incoming packet from a socket and extract one or more headers.

Bad code:

```c
DataPacket *packet;
int numHeaders;
PacketHeader *headers;

sock=AcceptSocketConnection();
ReadPacket(packet, sock);
numHeaders =packet->headers;

if (numHeaders > 100) {
    ExitError("too many headers!");
}
headers = malloc(numHeaders * sizeof(PacketHeader);
ParsePacketHeaders(packet, headers);
```

The code performs a check to make sure that the packet does not contain too many headers. However, numHeaders is defined as a signed int, so it could be negative. If the incoming packet specifies a value such as -3, then the malloc calculation will generate a negative number (say, -300 if each header can be a maximum of 100 bytes). When this result is provided to malloc(), it is first converted to a size\_t type. This conversion then produces a large value such as 4294966996, which may cause malloc() to fail or to allocate an extremely large amount of memory (CWE-195). With the appropriate negative numbers, an attacker could trick malloc() into using a very small positive number, which then allocates a buffer that is much smaller than expected, potentially leading to a buffer overflow.

### Example 4

This example processes user input comprised of a series of variable-length structures. The first 2 bytes of input dictate the size of the structure to be processed.

Bad code:

```c
char* processNext(char* strm) {
    char buf[512];
    short len = *(short*) strm;
    strm += sizeof(len);
    if (len <= 512) {
        memcpy(buf, strm, len);
        process(buf);
        return strm + len;
    }
    else {
        return -1;
    }
}
```

The programmer has set an upper bound on the structure size: if it is larger than 512, the input will not be processed. The problem is that len is a signed short, so the check against the maximum structure length is done with signed values, but len is converted to an unsigned integer for the call to memcpy() and the negative bit will be extended to result in a huge value for the unsigned integer. If len is negative, then it will appear that the structure has an appropriate size (the if branch will be taken), but the amount of memory copied by memcpy() will be quite large, and the attacker will be able to overflow the stack with data in strm.

### Example 5

In the following example, it is possible to request that memcpy move a much larger segment of memory than assumed:

Bad code:

```c
int returnChunkSize(void *) {

    /* if chunk info is valid, return the size of usable memory,

    * else, return -1 to indicate an error

    */
    ...
}
int main() {
    ...
    memcpy(destBuf, srcBuf, (returnChunkSize(destBuf)-1));
    ...
}
```

If returnChunkSize() happens to encounter an error it will return -1. Notice that the return value is not checked before the memcpy operation (CWE-252), so -1 can be passed as the size argument to memcpy() (CWE-805). Because memcpy() assumes that the value is unsigned, it will be interpreted as MAXINT-1 (CWE-195), and therefore will copy far more memory than is likely available to the destination buffer (CWE-787, CWE-788).

### Example 6

This example shows a typical attempt to parse a string with an error resulting from a difference in assumptions between the caller to a function and the function's action.

Bad code:

```c
int proc_msg(char *s, int msg_len)
{
// Note space at the end of the string - assume all strings have preamble with space
int pre_len = sizeof("preamble: ");
char buf[pre_len - msg_len];
... Do processing here if we get this far
}
char *s = "preamble: message\n";
char *sl = strchr(s, ':');        // Number of characters up to ':' (not including space)
int jnklen = sl == NULL ? 0 : sl - s;    // If undefined pointer, use zero length
int ret_val = proc_msg ("s",  jnklen);    // Violate assumption of preamble length, end up with negative value, blow out stack
```

The buffer length ends up being -1, resulting in a blown out stack. The space character after the colon is included in the function calculation, but not in the caller's calculation. This, unfortunately, is not usually so obvious but exists in an obtuse series of calculations.

## Observed examples

| CVE | Description |
| --- | --- |
| CVE-2025-27363 | Font rendering library does not properly handle assigning a signed short value to an unsigned long (CWE-195), leading to an integer wraparound (CWE-190), causing too small of a buffer (CWE-131), leading to an out-of-bounds write (CWE-787). |
| CVE-2007-4268 | Chain: integer signedness error (CWE-195) passes signed comparison, leading to heap overflow (CWE-122) |

## Real CVEs with this weakness (sample of 12 of 34)

| CVE | Title | Severity | CVSS | EPSS | CISA KEV | Published |
| --- | --- | --- | --- | --- | --- | --- |
| CVE-2022-43663 | WellinTech KingHistorian SORBAx64.dll RecvPacket integer conversion vulnerability | critical | 9.8 | - | No | 2023-03-20 |
| CVE-2026-74221 | U-Boot before 2026.10-rc5 Buffer Overflow via NFS READLINK | high | 8.8 | - | No | 2026-09-29 |
| CVE-2026-74220 | U-Boot before 2026.10-rc5 Buffer Overflow via NFS READ Reply | high | 8.8 | - | No | 2026-09-29 |
| CVE-2011-3045 | libpng integer issue from upstream | high | 8.8 | - | No | 2012-03-22 |
| CVE-2026-17507 | MLS membership checks compare a uint32 leaf\_index as signed, admitting an out-of-range sender | high | 8.7 | - | No | 2026-10-02 |
| CVE-2026-85441 | MOOS core-moos through 10.4.0 MOOSDB Denial of Service via Negative Serialized String Length | high | 8.7 | - | No | 2026-09-03 |
| CVE-2016-6560 | - | high | 8.6 | - | No | 2017-03-31 |
| CVE-2024-25388 | - | high | 8.4 | - | No | 2024-03-27 |
| CVE-2026-77406 | RabbitMQ amqp091-go: Consumer Message Flooding via Signed-to-Unsigned Integer Casting in Qos Configuration | high | 8.2 | - | No | 2026-09-16 |
| CVE-2020-1913 | Signed to Unsigned Conversion Error in Facebook Hermes | high | 8.1 | - | No | 2020-09-09 |
| CVE-2020-6096 | glibc: signed comparison vulnerability in the ARMv7 memcpy function | high | 8.1 | - | No | 2020-04-01 |
| CVE-2023-33034 | Signed-to-unsigned conversion error in Audio | high | 7.8 | - | No | 2023-10-03 |

## Related weaknesses

| Relationship | CWE | Name |
| --- | --- | --- |
| Parent | [CWE-681](https://radicalnotion.ai/cwe/cwe-681) | Incorrect Conversion between Numeric Types |
| Can precede | [CWE-119](https://radicalnotion.ai/cwe/cwe-119) | Improper Restriction of Operations within the Bounds of a Memory Buffer |
| Related | [CWE-197](https://radicalnotion.ai/cwe/cwe-197) | Numeric Truncation Error |
| Related | [CWE-839](https://radicalnotion.ai/cwe/cwe-839) | Numeric Range Comparison Without Minimum Check |

## Taxonomy mappings

| Taxonomy | Entry | Id | Fit |
| --- | --- | --- | --- |
| CLASP | Signed to unsigned conversion error | - | - |
| Software Fault Patterns | Glitch in computation | SFP1 | - |
| CERT C Secure Coding | Ensure that integer conversions do not result in lost or misinterpreted data | INT31-C | CWE More Specific |

## Frequently asked questions

### What is CWE-195?

The product uses a signed primitive and performs a cast to an unsigned primitive, which can produce an unexpected value if the value of the signed primitive can not be represented using an unsigned primitive.

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

34 recorded CVEs are attributed to CWE-195, including CVE-2022-43663, CVE-2026-74221 and CVE-2026-74220.

### How is CWE-195 detected?

Automated Static Analysis: Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)

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

Exploiting CWE-195 can lead to: Unexpected State, Modify Memory, Read Memory.

### Is CWE-195 actively exploited?

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

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

---

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