Skip to content

Commit 61fba02

Browse files
committed
postprocess: object_detect_udp: Fix compiler warnings and styling
gcc 14 was throwing false positives about possible buffer overflows when populating the udp_data_buffer. Fix this by refactoring the code to be more explicit with the memcpy. Also fix some styling issues with the source file. Signed-off-by: Naushir Patuck <[email protected]>
1 parent eca9928 commit 61fba02

File tree

1 file changed

+81
-76
lines changed

1 file changed

+81
-76
lines changed

post_processing_stages/object_detect_udp_stage.cpp

Lines changed: 81 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ObjectDetectUDPStage : public PostProcessingStage
5151
std::string udp_broadcast_address = "127.0.0.1";
5252
u_int16_t udp_broadcast_port = 12345;
5353
int sockfd_;
54-
struct sockaddr_in servaddr_;
54+
struct sockaddr_in servaddr_;
5555
};
5656

5757
#define NAME "object_detect_udp"
@@ -64,11 +64,11 @@ char const *ObjectDetectUDPStage::Name() const
6464

6565
ObjectDetectUDPStage::~ObjectDetectUDPStage()
6666
{
67-
if (sockfd_ != -1)
68-
{
69-
close(sockfd_);
70-
std::cerr << "UDP socket closed." << std::endl;
71-
}
67+
if (sockfd_ != -1)
68+
{
69+
close(sockfd_);
70+
std::cerr << "UDP socket closed." << std::endl;
71+
}
7272
}
7373

7474

@@ -77,92 +77,97 @@ void ObjectDetectUDPStage::Configure()
7777
stream_ = app_->GetMainStream();
7878

7979
// Initialize UDP socket
80-
sockfd_ = socket(AF_INET, SOCK_DGRAM, 0);
81-
if (sockfd_ < 0)
82-
{
83-
perror("UDP socket creation failed");
84-
// Handle error, perhaps throw an exception or exit
85-
return;
86-
}
87-
88-
memset(&servaddr_, 0, sizeof(servaddr_));
89-
90-
// Filling server information
91-
servaddr_.sin_family = AF_INET;
92-
servaddr_.sin_port = htons(udp_broadcast_port);
93-
if (inet_pton(AF_INET, udp_broadcast_address.c_str(), &servaddr_.sin_addr) <= 0)
94-
{
95-
perror("Invalid address/ Address not supported");
96-
close(sockfd_);
97-
sockfd_ = -1; // Mark as invalid
98-
return;
99-
}
100-
101-
std::cerr << "UDP socket initialized for IP: " << udp_broadcast_address << ", Port: " << udp_broadcast_port << std::endl;
102-
}
80+
sockfd_ = socket(AF_INET, SOCK_DGRAM, 0);
81+
if (sockfd_ < 0)
82+
{
83+
perror("UDP socket creation failed");
84+
// Handle error, perhaps throw an exception or exit
85+
return;
86+
}
87+
88+
memset(&servaddr_, 0, sizeof(servaddr_));
89+
90+
// Filling server information
91+
servaddr_.sin_family = AF_INET;
92+
servaddr_.sin_port = htons(udp_broadcast_port);
93+
if (inet_pton(AF_INET, udp_broadcast_address.c_str(), &servaddr_.sin_addr) <= 0)
94+
{
95+
perror("Invalid address/ Address not supported");
96+
close(sockfd_);
97+
sockfd_ = -1; // Mark as invalid
98+
return;
99+
}
103100

101+
std::cerr
102+
<< "UDP socket initialized for IP: " << udp_broadcast_address << ", Port: " << udp_broadcast_port << std::endl;
103+
}
104104

105105
void ObjectDetectUDPStage::Read(boost::property_tree::ptree const &params)
106106
{
107107
udp_broadcast_address = params.get<std::string>("ip", UDP_IP);
108108
udp_broadcast_port = params.get<u_int16_t>("port", UDP_PORT);
109109
}
110110

111+
template <typename T>
112+
void append(std::vector<char> &buffer, const T &value)
113+
{
114+
const char *raw = reinterpret_cast<const char *>(&value);
115+
buffer.insert(buffer.end(), raw, raw + sizeof(T));
116+
}
117+
111118
bool ObjectDetectUDPStage::Process(CompletedRequestPtr &completed_request)
112119
{
113120
if (!stream_)
114-
return false;
121+
return false;
115122

116-
std::vector<Detection> detections;
117-
completed_request->post_process_metadata.Get("object_detect.results", detections);
123+
std::vector<Detection> detections;
124+
completed_request->post_process_metadata.Get("object_detect.results", detections);
118125

119-
if (sockfd_ == -1)
120-
return false;
126+
if (sockfd_ == -1)
127+
return false;
121128

122-
for (auto &detection : detections)
129+
for (auto &detection : detections)
123130
{
124-
// Draw rectangle and text on the image
125-
std::stringstream text_stream;
126-
text_stream << detection.name << " " << (int)(detection.confidence * 100) << "%";
127-
std::string text = text_stream.str();
131+
// Draw rectangle and text on the image
132+
std::stringstream text_stream;
133+
text_stream << detection.name << " " << (int)(detection.confidence * 100) << "%";
134+
std::string text = text_stream.str();
128135

129-
// Use a vector to dynamically build the binary message
130-
std::vector<char> udp_data_buffer;
131-
132-
// 1. Add start delimiter (4 bytes)
133-
udp_data_buffer.insert(udp_data_buffer.end(), (char*)&START_DELIMITER, (char*)&START_DELIMITER + sizeof(START_DELIMITER));
134-
135-
// 2. Add x, y, width, height (4 bytes each)
136-
const int32_t x = detection.box.x;
137-
const int32_t y = detection.box.y;
138-
const int32_t width = detection.box.width;
139-
const int32_t height = detection.box.height;
140-
udp_data_buffer.insert(udp_data_buffer.end(), (char*)&x, (char*)&x + sizeof(x));
141-
udp_data_buffer.insert(udp_data_buffer.end(), (char*)&y, (char*)&y + sizeof(y));
142-
udp_data_buffer.insert(udp_data_buffer.end(), (char*)&width, (char*)&width + sizeof(width));
143-
udp_data_buffer.insert(udp_data_buffer.end(), (char*)&height, (char*)&height + sizeof(height));
144-
145-
// 3. Add name length and name string
146-
uint32_t name_length = detection.name.length();
147-
if (name_length > 255) {
148-
// Truncate or handle error for names longer than 255 chars
149-
name_length = 255;
150-
}
151-
udp_data_buffer.push_back(name_length);
152-
udp_data_buffer.insert(udp_data_buffer.end(), detection.name.begin(), detection.name.begin() + name_length);
153-
154-
// 4. Add confidence (4 bytes)
155-
const float confidence = detection.confidence;
156-
udp_data_buffer.insert(udp_data_buffer.end(), (char*)&confidence, (char*)&confidence + sizeof(confidence));
157-
158-
// Send data via UDP
159-
const ssize_t bytes_sent = sendto(sockfd_, udp_data_buffer.data(), udp_data_buffer.size(), 0,
160-
(const struct sockaddr *)&servaddr_, sizeof(servaddr_));
161-
if (bytes_sent < 0)
162-
{
163-
perror("Failed to send UDP message");
164-
}
165-
136+
// Use a vector to dynamically build the binary message
137+
std::vector<char> udp_data_buffer;
138+
139+
// 1. Add start delimiter (4 bytes)
140+
append(udp_data_buffer, START_DELIMITER);
141+
142+
// 2. Add x, y, width, height (4 bytes each)
143+
const int32_t x = detection.box.x;
144+
const int32_t y = detection.box.y;
145+
const int32_t width = detection.box.width;
146+
const int32_t height = detection.box.height;
147+
148+
append(udp_data_buffer, x);
149+
append(udp_data_buffer, y);
150+
append(udp_data_buffer, width);
151+
append(udp_data_buffer, height);
152+
153+
// 3. Add name length and name string
154+
constexpr uint8_t name_length = 255;
155+
udp_data_buffer.push_back(static_cast<char>(name_length));
156+
157+
uint8_t name[name_length];
158+
memcpy(name, detection.name.c_str(), name_length - 2);
159+
name[name_length - 1] = '\0';
160+
append(udp_data_buffer, name);
161+
162+
// 4. Add confidence (4 bytes)
163+
const float confidence = detection.confidence;
164+
append(udp_data_buffer, confidence);
165+
166+
// Send data via UDP
167+
const ssize_t bytes_sent = sendto(sockfd_, udp_data_buffer.data(), udp_data_buffer.size(), 0,
168+
(const struct sockaddr *)&servaddr_, sizeof(servaddr_));
169+
if (bytes_sent < 0)
170+
perror("Failed to send UDP message");
166171
}
167172

168173
return false;

0 commit comments

Comments
 (0)