CWE Rule 198
Description
Rule Description
The product receives input from an upstream component, but it does not account for byte ordering (e.g. big-endian and little-endian) when processing the input, causing an incorrect number or value to be used.
Polyspace Implementation
The rule checker checks for Missing byte reordering when transferring data.
Examples
Missing byte reordering when transferring data
This issue occurs when you do not use a byte ordering function:
Before sending data to a network socket.
After receiving data from a network socket.
Some system architectures implement little endian byte ordering (least significant byte first), and other systems implement big endian (most significant byte first). If the endianness of the sent data does not match the endianness of the receiving system, the value returned when reading the data is incorrect.
After receiving data from a socket, use a byte
ordering function such as ntohl()
. Before sending data to a
socket, use a byte ordering function such as htonl()
.
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <byteswap.h> #include <unistd.h> #include <string.h> unsigned int func(int sock, int server) { unsigned int num; /* assume int is 32-bits */ if (server) { /* Server side */ num = 0x17; /* Endianness of server host may not match endianness of network. */ if (send(sock, (void *)&num, sizeof(num), 0) < (int)sizeof(num)) //Noncompliant { /* Handle error */ } return 0; } else { /* Endianness of client host may not match endianness of network. */ if (recv (sock, (void *)&num, sizeof(num), 0) < (int) sizeof(num)) { /* Handle error */ } /* Comparison may be inaccurate */ if (num> 255) //Noncompliant { return 255; } else { return num; } } }
In this example, variable num
is assigned hexadecimal value
0x17
and is sent over a network to the client from the server. If the
server host is little endian and the network is big endian, num
is
transferred as 0x17000000
. The client then reads an incorrect value for
num
and compares it to a local numeric value.
Before sending num
from the server host, use
htonl()
to convert from host to network byte ordering. Similarly,
before reading num
on the client host, use ntohl()
to convert from network to host byte ordering.
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <byteswap.h> #include <unistd.h> #include <string.h> unsigned int func(int sock, int server) { unsigned int num; /* assume int is 32-bits */ if (server) { /* Server side */ num = 0x17; /* Convert to network byte order. */ num = htonl(num); if (send(sock, (void *)&num, sizeof(num), 0) < (int)sizeof(num)) { /* Handle error */ } return 0; } else { if (recv (sock, (void *)&num, sizeof(num), 0) < (int) sizeof(num)) { /* Handle error */ } /* Convert to host byte order. */ num = ntohl(num); if (num > 255) { return 255; } else { return num; } } }
Check Information
Category: Others |
Version History
Introduced in R2024a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)