Skip to content

Commit 21adb91

Browse files
committed
Zero-init planner structs via value-init instead of memset
cppcheck flags memset(0) on structs containing doubles (memsetClassFloat) as non-portable. Use C++ value-initialization (`T x{};`, `*ptr = T{};`) instead: for these POD structs it compiles to the same zeroing, is portable (no reliance on all-zero-bits == 0.0), and clears the warning legitimately rather than suppressing it.
1 parent 6020ee3 commit 21adb91

3 files changed

Lines changed: 11 additions & 19 deletions

File tree

src/emc/motion_planning/blend_sizing.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ int optimizeBlendSize9(TC_STRUCT const * const prev_tc,
970970
return TP_ERR_MISSING_INPUT;
971971
}
972972

973-
memset(result, 0, sizeof(BlendSolution9));
973+
*result = BlendSolution9{};
974974
result->status = BLEND9_FAIL;
975975

976976
/* Get maximum blend region */
@@ -1157,7 +1157,7 @@ int createBlendSegment9(TC_STRUCT const * const prev_tc,
11571157
}
11581158

11591159
/* Zero-initialize */
1160-
memset(blend_tc, 0, sizeof(TC_STRUCT));
1160+
*blend_tc = TC_STRUCT{};
11611161

11621162
/* Initialize as TC_BEZIER */
11631163
tcInit(blend_tc,

src/emc/motion_planning/motion_planning_9d.cc

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,10 +2875,8 @@ bool computeBranch(TP_STRUCT *tp, TC_STRUCT *tc, double new_feed_scale,
28752875
// Store cascade info
28762876
tc->shared_9d.requested_feed_scale = new_feed_scale;
28772877

2878-
ruckig_profile_t brake_profile;
2879-
ruckig_profile_t main_profile;
2880-
memset(&brake_profile, 0, sizeof(brake_profile));
2881-
memset(&main_profile, 0, sizeof(main_profile));
2878+
ruckig_profile_t brake_profile{};
2879+
ruckig_profile_t main_profile{};
28822880

28832881
double brake_end_position = state.position; // Position after brake (relative to segment start)
28842882

@@ -2926,8 +2924,7 @@ bool computeBranch(TP_STRUCT *tp, TC_STRUCT *tc, double new_feed_scale,
29262924
double safe_acc = sqrt(2.0 * sv * default_jerk);
29272925
if (tc->maxaccel > safe_acc + 0.5) {
29282926
input.max_acceleration = {safe_acc};
2929-
ruckig_profile_t constrained;
2930-
memset(&constrained, 0, sizeof(constrained));
2927+
ruckig_profile_t constrained{};
29312928
auto r2 = otg.calculate(input, traj);
29322929
if (r2 == ruckig::Result::Working || r2 == ruckig::Result::Finished) {
29332930
copyRuckigProfile(traj, &constrained);
@@ -3880,8 +3877,7 @@ static int writeSpillOverStopProfiles(TP_STRUCT *tp, TC_STRUCT *tc,
38803877
if (result != ruckig::Result::Working &&
38813878
result != ruckig::Result::Finished) break;
38823879

3883-
ruckig_profile_t stop_profile;
3884-
memset(&stop_profile, 0, sizeof(stop_profile));
3880+
ruckig_profile_t stop_profile{};
38853881
copyRuckigProfile(traj, &stop_profile);
38863882
if (!stop_profile.valid ||
38873883
profileHasNegativeVelocity(&stop_profile)) {
@@ -3893,7 +3889,7 @@ static int writeSpillOverStopProfiles(TP_STRUCT *tp, TC_STRUCT *tc,
38933889
result = otg.calculate(input, traj);
38943890
if (result == ruckig::Result::Working ||
38953891
result == ruckig::Result::Finished) {
3896-
memset(&stop_profile, 0, sizeof(stop_profile));
3892+
stop_profile = {};
38973893
copyRuckigProfile(traj, &stop_profile);
38983894
}
38993895
}

src/emc/motion_planning/motion_planning_9d_userspace.cc

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ static int tcInit_9D(TC_STRUCT * const tc,
294294
{
295295
if (!tc) return -1;
296296

297-
memset(tc, 0, sizeof(TC_STRUCT));
297+
*tc = TC_STRUCT{};
298298

299299
tc->motion_type = motion_type;
300300
tc->canon_motion_type = canon_motion_type;
@@ -754,9 +754,7 @@ static int tpSetupBlend9D(TP_STRUCT *tp, TC_STRUCT *prev_tc, TC_STRUCT *tc)
754754

755755
// Build per-axis velocity and acceleration bounds
756756
// Use minimum of both segments' limits
757-
AxisBounds9 vel_bounds, acc_bounds;
758-
memset(&vel_bounds, 0, sizeof(vel_bounds));
759-
memset(&acc_bounds, 0, sizeof(acc_bounds));
757+
AxisBounds9 vel_bounds{}, acc_bounds{};
760758

761759
double v_min = fmin(prev_tc->maxvel, tc->maxvel);
762760
double a_min = fmin(prev_tc->maxaccel, tc->maxaccel);
@@ -803,8 +801,7 @@ static int tpSetupBlend9D(TP_STRUCT *tp, TC_STRUCT *prev_tc, TC_STRUCT *tc)
803801
// Compute Jacobian at junction for non-identity kinematics
804802
if (g_userspace_kins_planner.isEnabled() &&
805803
!g_userspace_kins_planner.isIdentity()) {
806-
EmcPose junction;
807-
memset(&junction, 0, sizeof(junction));
804+
EmcPose junction{};
808805
if (tc->motion_type == TC_LINEAR) {
809806
junction.tran = tc->coords.line.xyz.start;
810807
junction.a = tc->coords.line.abc.start.x;
@@ -1236,8 +1233,7 @@ static void tpComputeKinkVelocity_9D(TP_STRUCT *tp, TC_QUEUE_STRUCT *queue,
12361233
bool have_jacobian = false;
12371234
if (g_userspace_kins_planner.isEnabled() &&
12381235
!g_userspace_kins_planner.isIdentity()) {
1239-
EmcPose junction;
1240-
memset(&junction, 0, sizeof(junction));
1236+
EmcPose junction{};
12411237
if (tc->motion_type == TC_LINEAR) {
12421238
junction.tran = tc->coords.line.xyz.start;
12431239
junction.a = tc->coords.line.abc.start.x;

0 commit comments

Comments
 (0)