JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
A socket operation is performed on a socket that is not in the correct state for that operation.
| Class Name | Socket In Wrong State | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Significance | reliability | ||||||||||||||||||
| Mnemonic | IO.SOCK.STATE | ||||||||||||||||||
| Categories |
|
||||||||||||||||||
| Availability | Available for C and C++. |
||||||||||||||||||
| Enabling | Checks for this warning class are
disabled by default. To enable them, add the following WARNING_FILTER
rule to the project configuration file.
WARNING_FILTER += allow class="Socket In Wrong State" |
#include <sys/socket.h>
#include <unistd.h>
int listen_no_bind(void){
int sock = socket(PF_LOCAL, SOCK_STREAM, 0);
struct sockaddr a;
socklen_t alen = sizeof(struct sockaddr);
int res;
if (sock < 0 ) return -1;
res = listen(sock, SOMAXCONN); /* 'Socket In Wrong State' warning issued here
* - socket has not been bound
*/
close(sock);
return res;
}
int listen_after_bind(void){
int sock = socket(PF_LOCAL, SOCK_STREAM, 0);
struct sockaddr a;
socklen_t alen = sizeof(struct sockaddr);
int res;
if (sock < 0 ) return -1;
res = bind(sock, &a, alen);
if (res >= 0){
res = listen(sock, SOMAXCONN); /* ok: socket has been bound with bind() */
}
close(sock);
return res;
}
| Triggering Functions | Triggered When |
|---|---|
| int accept(int s, struct sockaddr *addr, socklen_t *addrlen) | Socket s is not being listened to via a call to listen(). |
| int bind(int sockfd, void *my_addr, int addrlen); | Socket sockfd has not been initialized with a call to socket(). |
| int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen) | Socket sockfd is already being listened to (via a call to listen()). |
| int listen(int sockfd, int backlog); | Socket sockfd has not been bound with bind(). |
| int recv(int sockfd, void *buf, size_t len, int flags) | Socket sockfd has not been connected with connect(). |
| int send(int s, const void *msg, size_t len, int flags) | Socket s has not been connected with connect(). |
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.