Skip to content

Commit 37e3db0

Browse files
authored
Merge branch 'main' into fix/ITEP-87923-validate-user-api-fields
2 parents bf84a75 + 6c84d2b commit 37e3db0

65 files changed

Lines changed: 971 additions & 218 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
<!-- SPDX-License-Identifier: Apache-2.0 -->
2+
<!-- SPDX-FileCopyrightText: (C) 2026 Intel Corporation -->
3+
4+
# Intel Secure C++ Specifics
5+
6+
## Purpose
7+
8+
This skill provides guidance for secure C++ coding in SceneScape's geometry and vision components, grounded in Intel Secure Coding Standards - C++ Specifics.
9+
10+
**Applies to:**
11+
12+
- `scene_common/src/fast_geometry/` (C++ geometry extension)
13+
- `controller/src/robot_vision/` (C++ vision processing)
14+
- `tracker/` (C++ tracking component)
15+
- Any new C++ modules
16+
17+
---
18+
19+
## Core Principles
20+
21+
### Memory Management (RAII)
22+
23+
**DO:**
24+
25+
```cpp
26+
// Use RAII: Resource Acquisition Is Initialization
27+
class CameraMatrix {
28+
std::unique_ptr<float[]> data_;
29+
size_t size_;
30+
public:
31+
CameraMatrix(size_t n) : data_(new float[n]), size_(n) {}
32+
~CameraMatrix() { /* automatically freed by unique_ptr */ }
33+
};
34+
35+
// Use smart pointers
36+
std::unique_ptr<Geometry> geom(new Geometry()); // Prefer unique_ptr
37+
std::shared_ptr<Camera> cam(new Camera()); // Use shared_ptr only if needed
38+
```
39+
40+
❌ **DON'T:**
41+
42+
```cpp
43+
// Raw new/delete
44+
float* data = new float[1024];
45+
// ... risk of memory leak if exception or early return ...
46+
delete[] data;
47+
48+
// malloc/free (C-style)
49+
float* data = (float*)malloc(1024 * sizeof(float));
50+
free(data);
51+
```
52+
53+
### String Handling
54+
55+
**DO:**
56+
57+
```cpp
58+
std::string camera_name = "camera_1";
59+
std::string full_path = camera_name + "_calibration.json";
60+
61+
// Use std::string_view for non-owning references
62+
void process_camera(std::string_view name) {
63+
// read-only access without copying
64+
}
65+
66+
// Use std::format (C++20) or fmt::format
67+
std::string message = std::format("Camera {} position: {}", id, pos);
68+
```
69+
70+
❌ **DON'T:**
71+
72+
```cpp
73+
// C-style strings and unsafe functions
74+
char name[255];
75+
strcpy(name, user_input); // Buffer overflow risk
76+
strcat(name, ".json"); // Dangerous
77+
78+
// printf family
79+
printf("Position: %d %d\n", x, y); // Prone to format string attacks
80+
sprintf(buffer, "Name: %s", name); // No bounds checking
81+
```
82+
83+
### Type Safety & Const Correctness
84+
85+
**DO:**
86+
87+
```cpp
88+
// Use strong typing
89+
struct Point { float x, y, z; };
90+
struct Camera { float focal_length; };
91+
92+
// Explicit casts
93+
int size = static_cast<int>(data.size());
94+
95+
// Const for read-only
96+
const Point& get_position() const { return position_; }
97+
void set_position(const Point& p) { position_ = p; }
98+
99+
// nullptr for null pointers
100+
Point* position = nullptr; // Strongly typed
101+
if (position != nullptr) { /* ... */ }
102+
```
103+
104+
❌ **DON'T:**
105+
106+
```cpp
107+
// Weak typing (void*)
108+
void* data = geometry_data; // What type is this?
109+
110+
// Implicit casting
111+
int size = data.size(); // May lose precision
112+
113+
// NULL macro (C-style)
114+
Point* position = NULL;
115+
116+
// Uninitialized pointers
117+
Point* position; // May contain garbage
118+
```
119+
120+
### Integer Arithmetic
121+
122+
**DO:**
123+
124+
```cpp
125+
// Check for overflow before operations
126+
size_t total_size = num_cameras * points_per_camera;
127+
if (num_cameras > SIZE_MAX / points_per_camera) {
128+
throw std::overflow_error("Size calculation overflow");
129+
}
130+
131+
// Guard division by zero
132+
if (divisor != 0) {
133+
result = numerator / divisor;
134+
} else {
135+
throw std::runtime_error("Division by zero");
136+
}
137+
138+
// Check INT_MIN/-1 for signed division
139+
if (!(value == INT_MIN && divisor == -1)) {
140+
result = value / divisor;
141+
}
142+
```
143+
144+
**DON'T:**
145+
146+
```cpp
147+
// Unsigned/signed mix
148+
int signed_val = -10;
149+
unsigned int unsigned_val = 20;
150+
int result = signed_val + unsigned_val; // Unexpected conversion
151+
152+
// Unguarded division
153+
result = a / b; // May crash if b == 0
154+
155+
// Integer overflow
156+
int overflow = INT_MAX + 1; // Undefined behavior
157+
```
158+
159+
### Error Handling
160+
161+
**DO:**
162+
163+
```cpp
164+
// Check return values
165+
int result = calibrate_camera(cam);
166+
if (result != SUCCESS) {
167+
log_error("Camera calibration failed");
168+
throw std::runtime_error("Calibration error");
169+
}
170+
171+
// Use exceptions, not assert for security
172+
if (!validate_matrix(M)) {
173+
throw std::invalid_argument("Invalid matrix");
174+
}
175+
176+
// Use std::optional for optional results
177+
std::optional<Point> find_feature(const Image& img) {
178+
if (/* found */) return Point{x, y};
179+
return std::nullopt;
180+
}
181+
```
182+
183+
❌ **DON'T:**
184+
185+
```cpp
186+
// Ignore return values
187+
calibrate_camera(cam); // Result ignored; may fail silently
188+
189+
// Use assert for runtime validation
190+
assert(validate_matrix(M)); // Removed in release builds!
191+
192+
// Silent failures
193+
if (load_config(file)) { /* continue */ } // What failed?
194+
```
195+
196+
### Function Design
197+
198+
**DO:**
199+
200+
```cpp
201+
// Limit parameters (ideally ≤6)
202+
Point apply_transform(const Point& p, const Matrix& M, const Vector& t) {
203+
return M * p + t;
204+
}
205+
206+
// Clear error handling
207+
[[nodiscard]] bool validate_image(const Image& img);
208+
209+
// Pure functions where possible
210+
float distance(const Point& a, const Point& b) {
211+
return std::sqrt((a.x - b.x) * (a.x - b.x) +
212+
(a.y - b.y) * (a.y - b.y));
213+
}
214+
```
215+
216+
❌ **DON'T:**
217+
218+
```cpp
219+
// Too many parameters
220+
Point complex_op(int a, int b, int c, int d, int e, int f, int g) { /* ... */ }
221+
222+
// Variadic functions (error-prone)
223+
void log_message(const char* fmt, ...); // Use std::format instead
224+
225+
// Functions with hidden side effects
226+
Point get_position(); // Modifies global state? Unclear
227+
```
228+
229+
---
230+
231+
## Review Checklist
232+
233+
When reviewing or generating C++ code:
234+
235+
- [ ] All heap allocations use `std::unique_ptr<T>` or `std::shared_ptr<T>` (no raw `new`/`delete`)
236+
- [ ] No `malloc`/`free` (C-style memory management)
237+
- [ ] All pointers initialized to `nullptr` (not `NULL`)
238+
- [ ] All strings use `std::string` (not C-style char arrays)
239+
- [ ] No `printf` family functions (use `std::format` or `fmt::format`)
240+
- [ ] Return values checked for all functions that may fail
241+
- [ ] Integer arithmetic guarded against overflow/underflow
242+
- [ ] Division by zero prevented (checked divisor)
243+
- [ ] Const-correctness enforced (const members, const references)
244+
- [ ] Assert not used for security validation (use exceptions)
245+
- [ ] Type casts use `static_cast<T>`, `const_cast<T>` explicitly (never C-style casts)
246+
- [ ] No `void*` for type safety

0 commit comments

Comments
 (0)