Bug Summary

File:daq/slc/system/src/TCPSocket.cc
Warning:line 114, column 15
An undefined value may be read from 'errno'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -O3 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name TCPSocket.cc -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/data/b2soft/buildbot/development/build -fcoverage-compilation-dir=/data/b2soft/buildbot/development/build -resource-dir /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/lib/clang/21 -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include/c++ -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include/c++/x86_64-redhat-linux -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include/c++/backward -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/include -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include/python3.12 -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/include/CLHEP -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include/Geant4 -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/include/root -isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/include/belle_legacy -I include/ -D _PACKAGE_="daq" -D G4UI_USE_TCSH -D RaveDllExport= -D HAS_SQLITE -D HAS_CALLGRIND -I include -I /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/include/libxml2 -internal-isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/bin/../lib64/gcc/x86_64-redhat-linux/15.2.0/../../../../include/c++ -internal-isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/bin/../lib64/gcc/x86_64-redhat-linux/15.2.0/../../../../include/c++/x86_64-redhat-linux -internal-isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/bin/../lib64/gcc/x86_64-redhat-linux/15.2.0/../../../../include/c++/backward -internal-isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/lib/clang/21/include -internal-isystem /usr/local/include -internal-isystem /cvmfs/belle.cern.ch/el9/externals/v02-04-00/Linux_x86_64/common/bin/../lib64/gcc/x86_64-redhat-linux/15.2.0/../../../../x86_64-redhat-linux/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-missing-braces -Wno-unused-command-line-argument -std=c++20 -fdeprecated-macro -ferror-limit 19 -fgnuc-version=4.2.1 -fno-implicit-modules -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /scan_build/2026-05-31-004316-385593-1 -x c++ daq/slc/system/src/TCPSocket.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include "daq/slc/system/TCPSocket.h"
9
10#include <daq/slc/base/IOException.h>
11
12#include <cstdio>
13#include <cstring>
14
15#include <sys/socket.h>
16#include <arpa/inet.h>
17
18#include <unistd.h>
19#include <netinet/in.h>
20#include <errno(*__errno_location ()).h>
21#include <netdb.h>
22
23using namespace Belle2;
24
25int TCPSocket::connect(const std::string& ip, unsigned short port)
26{
27 m_ip = ip;
28 m_port = port;
29 return connect();
30}
31
32int TCPSocket::connect()
33{
34 if (m_fd > 0) {
35 throw (IOException("Socket is working already."));
36 }
37 sockaddr_in addr;
38 memset(&addr, 0, sizeof(sockaddr_in));
39 addr.sin_family = AF_INET2;
40 addr.sin_addr.s_addr = INADDR_ANY((in_addr_t) 0x00000000);
41 addr.sin_port = htons(m_port)__bswap_16 (m_port);
42
43 if ((m_fd = ::socket(PF_INET2, SOCK_STREAMSOCK_STREAM, 0)) < 0) {
44 throw (IOException("Failed to create socket"));
45 }
46 struct hostent* host = NULL__null;
47 host = gethostbyname(m_ip.c_str());
48 if (host == NULL__null) {
49 unsigned long ip_address = inet_addr(m_ip.c_str());
50 if ((signed long) ip_address < 0) {
51 throw (IOException("Wrong host name or ip"));
52 } else {
53 host = gethostbyaddr((char*)&ip_address, sizeof(ip_address), AF_INET2);
54 }
55 }
56 if (host == NULL__null) {
57 close();
58 throw (IOException("Failed to connect host %s:%d",
59 m_ip.c_str(), m_port));
60 }
61 addr.sin_addr.s_addr = (*(unsigned long*) host->h_addr_list[0]);
62
63 if (::connect(m_fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
64 close();
65 throw (IOException("Failed to connect host %s:%d",
66 m_ip.c_str(), m_port));
67 }
68
69 return m_fd;
70}
71
72void TCPSocket::setBufferSize(int size)
73{
74 if (size > 0) {
75 if (setsockopt(m_fd, SOL_SOCKET1, SO_SNDBUF7, &size, sizeof(size)) != 0) {
76 throw (IOException("failed to SO_SNDBUF: %s\n", strerror(errno(*__errno_location ()))));
77 }
78 if (setsockopt(m_fd, SOL_SOCKET1, SO_RCVBUF8, &size, sizeof(size)) != 0) {
79 throw (IOException("error on SO_RCVBUF: %s\n", strerror(errno(*__errno_location ()))));
80 }
81 }
82}
83
84size_t TCPSocket::write(const void* buf, size_t count)
85{
86 size_t c = 0;
87 while (c < count) {
88 errno(*__errno_location ()) = 0;
89 int ret = send(m_fd, ((unsigned char*)buf + c), (count - c), MSG_NOSIGNALMSG_NOSIGNAL);
90 if (ret <= 0) {
91 switch (errno(*__errno_location ())) {
92 case EINTR4: continue;
93 case ENETUNREACH101:
94 case EHOSTUNREACH113:
95 case ETIMEDOUT110:
96 usleep(500);
97 continue;
98 default:
99 throw (IOException("Error while writing"));
100 }
101 }
102 c += ret;
103 }
104 return c;
105}
106
107size_t TCPSocket::read(void* buf, size_t count)
108{
109 size_t c = 0;
110 while (c < count) {
1
Assuming 'c' is < 'count'
2
Loop condition is true. Entering loop body
4
Assuming 'c' is < 'count'
5
Loop condition is true. Entering loop body
111 errno(*__errno_location ()) = 0;
112 int ret = recv(m_fd, ((unsigned char*)buf + c), (count - c), 0);
6
Assuming that 'recv' is successful; 'errno' becomes undefined after the call
113 if (ret
2.1
'ret' is > 0
6.1
'ret' is <= 0
<= 0) {
3
Taking false branch
7
Taking true branch
114 switch (errno(*__errno_location ())) {
8
An undefined value may be read from 'errno'
115 case EINTR4: continue;
116 case EAGAIN11: continue;
117 default:
118 throw (IOException("TCPSocket::read Error while reading."));
119 }
120 }
121 c += ret;
122 }
123 return c;
124}
125
126size_t TCPSocket::read_once(void* buf, size_t count)
127{
128 errno(*__errno_location ()) = 0;
129 while (true) {
130 int ret = recv(m_fd, buf, count, 0);
131 if (ret <= 0) {
132 switch (errno(*__errno_location ())) {
133 case EINTR4: continue;
134 case EAGAIN11: continue;
135 default:
136 throw (IOException("TCPSocket::read_once Error while reading."));
137 }
138 }
139 return ret;
140 }
141 return 0;
142}
143
144void TCPSocket::print()
145{
146 sockaddr_in sa;
147 memset(&sa, 0, sizeof(sockaddr_in));
148 socklen_t sa_len = sizeof(sa);
149 if (getsockname(m_fd, (struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
150 perror("getsockname");
151 }
152 printf("Local IP address is: %s\n", inet_ntoa(sa.sin_addr));
153 printf("Local port is: %d\n", (int) ntohs(sa.sin_port)__bswap_16 (sa.sin_port));
154}
155
156const std::string TCPSocket::getLocalIP()
157{
158 sockaddr_in sa;
159 memset(&sa, 0, sizeof(sockaddr_in));
160 socklen_t sa_len = sizeof(sa);
161 if (getsockname(m_fd, (struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
162 return "";
163 }
164 return inet_ntoa(sa.sin_addr);
165}
166
167int TCPSocket::getLocalAddress()
168{
169 sockaddr_in sa;
170 memset(&sa, 0, sizeof(sockaddr_in));
171 socklen_t sa_len = sizeof(sa);
172 if (getsockname(m_fd, (struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
173 return 0;
174 }
175 return sa.sin_addr.s_addr;
176}
177
178int TCPSocket::getLocalPort()
179{
180 sockaddr_in sa;
181 memset(&sa, 0, sizeof(sockaddr_in));
182 socklen_t sa_len = sizeof(sa);
183 if (getsockname(m_fd, (struct sockaddr*)&sa, (socklen_t*)&sa_len) != 0) {
184 return 0;
185 }
186 return ntohs(sa.sin_port)__bswap_16 (sa.sin_port);
187}
188
189unsigned int TCPSocket::getAddress()
190{
191 struct hostent* host = NULL__null;
192 host = gethostbyname(m_ip.c_str());
193 if (host == NULL__null) {
194 unsigned long ip_address = inet_addr(m_ip.c_str());
195 if ((signed long) ip_address < 0) {
196 throw (std::exception());
197 // throw (IOException("Wrong host name or ip")); // TODO which throw is the correct one?
198 } else {
199 host = gethostbyaddr((char*)&ip_address, sizeof(ip_address), AF_INET2);
200 }
201 }
202 return (*(unsigned long*) host->h_addr_list[0]);
203}