Skip to content

Commit 7d284bf

Browse files
committed
fix clang-analyzer-core.NullDereference ...
which was due clang making false assumptions. But I think the modified code is easier for both humans and clang to understand.
1 parent 0901d5f commit 7d284bf

File tree

1 file changed

+47
-48
lines changed

1 file changed

+47
-48
lines changed

gdb.cc

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ GdbFormat::disp_summary(const gbfile* f) const
106106
gbWarning("%s\n", QByteArray(len + 21, '=').constData());
107107

108108
gbWarning("%s summary for \"%s\"\n",
109-
(f->mode == 'r') ? "Reader" : "Writer", gbLogCStr(f->name));
109+
(f->mode == 'r') ? "Reader" : "Writer", gbLogCStr(f->name));
110110

111111
gbWarning("%s\n", QByteArray(len + 21, '-').constData());
112112

@@ -116,7 +116,7 @@ GdbFormat::disp_summary(const gbfile* f) const
116116
gbWarning("%d track(s) with total %d point(s)\n", trk_ct, trkpt_ct);
117117

118118
gbWarning("%s\n", QByteArray(len + 21, '-').constData());
119-
} else {
119+
} else {
120120
Q_UNUSED(f);
121121
}
122122
}
@@ -245,9 +245,9 @@ GdbFormat::gdb_add_route_waypt(route_head* rte, Waypoint* ref, const int wpt_cla
245245

246246
if (fabs(dist) > 100) {
247247
gbFatal("Route point mismatch!\n" \
248-
" \"%s\" from waypoints differs to \"%s\"\n" \
249-
" from route table by more than %0.1f meters!\n", \
250-
gbLogCStr(tmp->shortname), gbLogCStr(ref->shortname), dist);
248+
" \"%s\" from waypoints differs to \"%s\"\n" \
249+
" from route table by more than %0.1f meters!\n", \
250+
gbLogCStr(tmp->shortname), gbLogCStr(ref->shortname), dist);
251251
}
252252
}
253253
Waypoint* res = nullptr;
@@ -451,37 +451,37 @@ GdbFormat::read_waypoint(gt_waypt_classes_e* waypt_class_out)
451451
if constexpr(GDB_DEBUG) {
452452
DBG(GDB_DBG_WPTe, true)
453453
gbDebug("wpt \"%s\" (%d): Altitude = %.1f\n",
454-
gbLogCStr(res->shortname), wpt_class, alt);
454+
gbLogCStr(res->shortname), wpt_class, alt);
455455
}
456456
}
457457
}
458458
if constexpr(GDB_DEBUG) {
459459
DBG(GDB_DBG_WPT, true)
460460
gbDebug("wpt \"%s\": coordinates = %c%0.6f %c%0.6f\n",
461-
gbLogCStr(res->shortname),
462-
res->latitude < 0 ? 'S' : 'N', res->latitude,
463-
res->longitude < 0 ? 'W' : 'E', res->longitude);
461+
gbLogCStr(res->shortname),
462+
res->latitude < 0 ? 'S' : 'N', res->latitude,
463+
res->longitude < 0 ? 'W' : 'E', res->longitude);
464464
}
465465
res->notes = fread_cstr();
466466
if constexpr(GDB_DEBUG) {
467467
DBG(GDB_DBG_WPTe, !res->notes.isNull())
468468
gbDebug("wpt \"%s\" (%d): notes = %s\n",
469-
gbLogCStr(res->shortname), wpt_class,
470-
gbLogCStr(QString(res->notes).replace("\r\n", ", ")));
469+
gbLogCStr(res->shortname), wpt_class,
470+
gbLogCStr(QString(res->notes).replace("\r\n", ", ")));
471471
}
472472
if (FREAD_C == 1) {
473473
res->set_proximity(FREAD_DBL);
474474
if constexpr(GDB_DEBUG) {
475475
DBG(GDB_DBG_WPTe, res->proximity_has_value())
476476
gbDebug("wpt \"%s\" (%d): Proximity = %.1f\n",
477-
gbLogCStr(res->shortname), wpt_class, res->proximity_value() / 1000);
477+
gbLogCStr(res->shortname), wpt_class, res->proximity_value() / 1000);
478478
}
479479
}
480480
int display = FREAD_i32;
481481
if constexpr(GDB_DEBUG) {
482482
DBG(GDB_DBG_WPTe, true)
483483
gbDebug("wpt \"%s\" (%d): display = %d\n",
484-
gbLogCStr(res->shortname), wpt_class, display);
484+
gbLogCStr(res->shortname), wpt_class, display);
485485
}
486486
switch (display) { /* display value */
487487
case gt_gdb_display_mode_symbol:
@@ -510,7 +510,7 @@ GdbFormat::read_waypoint(gt_waypt_classes_e* waypt_class_out)
510510
if constexpr(GDB_DEBUG) {
511511
DBG(GDB_DBG_WPTe, res->depth_has_value())
512512
gbDebug("wpt \"%s\" (%d): Depth = %.1f\n",
513-
gbLogCStr(res->shortname), wpt_class, res->depth_value());
513+
gbLogCStr(res->shortname), wpt_class, res->depth_value());
514514
}
515515
}
516516

@@ -530,7 +530,7 @@ GdbFormat::read_waypoint(gt_waypt_classes_e* waypt_class_out)
530530
QString temp = FREAD_CSTR_AS_QSTR; /* undocumented & unused string */
531531
DBG(GDB_DBG_WPTe, !temp.isEmpty())
532532
gbDebug("wpt \"%s\" (%d): Unknown string = %s\n",
533-
gbLogCStr(res->shortname), wpt_class, gbLogCStr(temp));
533+
gbLogCStr(res->shortname), wpt_class, gbLogCStr(temp));
534534
} else {
535535
(void) FREAD_CSTR_AS_QSTR; /* undocumented & unused string */
536536
}
@@ -559,7 +559,7 @@ GdbFormat::read_waypoint(gt_waypt_classes_e* waypt_class_out)
559559
if constexpr(GDB_DEBUG) {
560560
DBG(GDB_DBG_WPTe, true)
561561
gbDebug("wpt \"%s\" (%d): duration = %u\n",
562-
gbLogCStr(res->shortname), wpt_class, duration);
562+
gbLogCStr(res->shortname), wpt_class, duration);
563563
}
564564
}
565565
int url_ct = FREAD_i32;
@@ -570,7 +570,7 @@ GdbFormat::read_waypoint(gt_waypt_classes_e* waypt_class_out)
570570
if constexpr(GDB_DEBUG) {
571571
DBG(GDB_DBG_WPTe, true)
572572
gbDebug("wpt \"%s\" (%d): url(%d) = %s\n",
573-
gbLogCStr(res->shortname), wpt_class, url_ct - i, gbLogCStr(str));
573+
gbLogCStr(res->shortname), wpt_class, url_ct - i, gbLogCStr(str));
574574
}
575575
}
576576
}
@@ -579,10 +579,10 @@ GdbFormat::read_waypoint(gt_waypt_classes_e* waypt_class_out)
579579
if constexpr(GDB_DEBUG) {
580580
DBG(GDB_DBG_WPTe, !res->description.isNull())
581581
gbDebug("wpt \"%s\" (%d): description = %s\n",
582-
gbLogCStr(res->shortname), wpt_class, gbLogCStr(res->description));
582+
gbLogCStr(res->shortname), wpt_class, gbLogCStr(res->description));
583583
DBG(GDB_DBG_WPTe, res->urls.HasUrlLink())
584584
gbDebug("wpt \"%s\" (%d): url = %s\n",
585-
gbLogCStr(res->shortname), wpt_class, gbLogCStr(res->urls.GetUrlLink().url_));
585+
gbLogCStr(res->shortname), wpt_class, gbLogCStr(res->urls.GetUrlLink().url_));
586586
}
587587
int category = FREAD_i16;
588588
if (category != 0) {
@@ -591,15 +591,15 @@ GdbFormat::read_waypoint(gt_waypt_classes_e* waypt_class_out)
591591
if constexpr(GDB_DEBUG) {
592592
DBG(GDB_DBG_WPTe, category)
593593
gbDebug("wpt \"%s\" (%d): category = %d\n",
594-
gbLogCStr(res->shortname), wpt_class, category);
594+
gbLogCStr(res->shortname), wpt_class, category);
595595
}
596596

597597
if (FREAD_C == 1) {
598598
res->set_temperature(FREAD_DBL);
599599
if constexpr(GDB_DEBUG) {
600600
DBG(GDB_DBG_WPTe, res->temperature_has_value())
601601
gbDebug("wpt \"%s\" (%d): temperature = %.1f\n",
602-
gbLogCStr(res->shortname), wpt_class, res->temperature_value());
602+
gbLogCStr(res->shortname), wpt_class, res->temperature_value());
603603
}
604604
}
605605

@@ -628,7 +628,7 @@ GdbFormat::read_waypoint(gt_waypt_classes_e* waypt_class_out)
628628
if constexpr(GDB_DEBUG) {
629629
DBG(GDB_DBG_WPTe, icon != kGDBDefIcon)
630630
gbDebug("wpt \"%s\" (%d): icon = \"%s\" (MapSource symbol %d)\n",
631-
gbLogCStr(res->shortname), wpt_class, gbLogCStr(res->icon_descr), icon);
631+
gbLogCStr(res->shortname), wpt_class, gbLogCStr(res->icon_descr), icon);
632632
}
633633
QString str;
634634
if (!(str = garmin_fs_t::get_cc(gmsd, nullptr)).isEmpty()) {
@@ -681,7 +681,7 @@ GdbFormat::read_route()
681681
if constexpr(GDB_DEBUG) {
682682
DBG(GDB_DBG_RTE, true)
683683
gbDebug("rte \"%s\": loading route with %d point(s)...\n",
684-
gbLogCStr(rte->rte_name), points);
684+
gbLogCStr(rte->rte_name), points);
685685
}
686686

687687
for (int i = 0; i < points; i++) {
@@ -723,7 +723,7 @@ GdbFormat::read_route()
723723
if constexpr(GDB_DEBUG) {
724724
DBG(GDB_DBG_RTE, links)
725725
gbDebug("rte_pt \"%s\" (%d): %d interlink step(s)\n",
726-
gbLogCStr(wpt->shortname), wpt_class, links);
726+
gbLogCStr(wpt->shortname), wpt_class, links);
727727
}
728728
for (int j = 0; j < links; j++) {
729729
garmin_ilink_t il_step;
@@ -747,9 +747,9 @@ GdbFormat::read_route()
747747
if constexpr(GDB_DEBUG) {
748748
DBG(GDB_DBG_RTEe, true) {
749749
gbDebug("rte_il \"%s\" (%d of %d): %c%0.6f %c%0.6f\n",
750-
gbLogCStr(wpt->shortname), j + 1, links,
751-
il_step.lat < 0 ? 'S' : 'N', il_step.lat,
752-
il_step.lon < 0 ? 'W' : 'E', il_step.lon);
750+
gbLogCStr(wpt->shortname), j + 1, links,
751+
il_step.lat < 0 ? 'S' : 'N', il_step.lat,
752+
il_step.lon < 0 ? 'W' : 'E', il_step.lon);
753753
}
754754
}
755755
}
@@ -796,9 +796,9 @@ GdbFormat::read_route()
796796
if constexpr(GDB_DEBUG) {
797797
DBG(GDB_DBG_RTE, true)
798798
gbDebug("rte_pt \"%s\": coordinates = %c%0.6f, %c%0.6f\n",
799-
gbLogCStr(wpt->shortname),
800-
wpt->latitude < 0 ? 'S' : 'N', wpt->latitude,
801-
wpt->longitude < 0 ? 'W' : 'E', wpt->longitude);
799+
gbLogCStr(wpt->shortname),
800+
wpt->latitude < 0 ? 'S' : 'N', wpt->latitude,
801+
wpt->longitude < 0 ? 'W' : 'E', wpt->longitude);
802802
}
803803
wpt = gdb_add_route_waypt(rte, wpt, wpt_class);
804804
if (wpt != nullptr) {
@@ -838,9 +838,9 @@ GdbFormat::read_route()
838838
if constexpr(GDB_DEBUG) {
839839
DBG(GDB_DBG_RTE, true)
840840
gbDebug("rte_pt: autoroute info: route style %d, calculation type %d, vehicle type %d, road selection %d\n"
841-
" driving speeds (kph) %.0f, %.0f, %.0f, %.0f, %.0f\n",
842-
route_style, calc_type, vehicle_type, road_selection,
843-
driving_speed[0], driving_speed[1], driving_speed[2], driving_speed[3], driving_speed[4]);
841+
" driving speeds (kph) %.0f, %.0f, %.0f, %.0f, %.0f\n",
842+
route_style, calc_type, vehicle_type, road_selection,
843+
driving_speed[0], driving_speed[1], driving_speed[2], driving_speed[3], driving_speed[4]);
844844
} else {
845845
Q_UNUSED(route_style);
846846
Q_UNUSED(calc_type);
@@ -919,7 +919,7 @@ GdbFormat::read_track()
919919
if constexpr(GDB_DEBUG) {
920920
DBG(GDB_DBG_TRK, res->rte_urls.HasUrlLink())
921921
gbDebug("trk \"%s\": url = %s\n",
922-
gbLogCStr(res->rte_name), gbLogCStr(res->rte_urls.GetUrlLink().url_));
922+
gbLogCStr(res->rte_name), gbLogCStr(res->rte_urls.GetUrlLink().url_));
923923
}
924924
return res;
925925
}
@@ -1041,7 +1041,7 @@ GdbFormat::read()
10411041
}
10421042
if (typ == 'W')
10431043
gbWarning("(%d%c-%02d): delta = %d (flag=%3d/%02x)-",
1044-
gdb_ver, typ, wpt_class, delta, waypt_flag, waypt_flag);
1044+
gdb_ver, typ, wpt_class, delta, waypt_flag, waypt_flag);
10451045
else {
10461046
gbWarning("(%d%c): delta = %d -", gdb_ver, typ, delta);
10471047
}
@@ -1215,7 +1215,7 @@ GdbFormat::write_waypoint(
12151215
QString str;
12161216

12171217
if (wpt_class < gt_waypt_class_map_point) { /* street address */
1218-
str = garmin_fs_t::get_addr(gmsd, "");
1218+
str = garmin_fs_t::get_addr(gmsd, "");
12191219
} else {
12201220
str = "";
12211221
}
@@ -1314,23 +1314,22 @@ GdbFormat::write_route(const route_head* rte, const QString& rte_name)
13141314
int points = rte->rte_waypt_ct();
13151315
FWRITE_i32(points);
13161316

1317-
int index = 0;
1318-
1319-
for (auto it =rte->waypoint_list.cbegin(); it != rte->waypoint_list.cend(); ++it) {
1317+
for (auto begin = rte->waypoint_list.cbegin(), end = rte->waypoint_list.cend(), it = begin;
1318+
it != end; ++it) {
13201319

1320+
bool firstpass = it == begin;
13211321
Waypoint* wpt = *it;
1322-
Waypoint* next = nullptr;
1323-
if (index < points) {
1324-
next = *std::next(it);
1325-
}
13261322

1327-
index++;
1323+
auto next_it = std::next(it);
1324+
bool lastpass = next_it == end;
1325+
Waypoint* next = lastpass? nullptr : *next_it;
1326+
13281327
rtept_ct++; /* increase informational number of written route points */
13291328

1330-
if (index == 1) {
1329+
if (firstpass) {
13311330
gdb_check_waypt(wpt);
13321331
}
1333-
if (index < points) {
1332+
if (!lastpass) {
13341333
gdb_check_waypt(next);
13351334
}
13361335

@@ -1368,10 +1367,10 @@ GdbFormat::write_route(const route_head* rte, const QString& rte_name)
13681367
FWRITE(ffbuf, 4);
13691368
FWRITE(zbuf, 10);
13701369

1371-
if (index == points) {
1370+
if (lastpass) {
13721371
FWRITE_i32(0); /* no more steps */
13731372
FWRITE_C(1); /* skip bounds */
1374-
} else { /* if (index < points) */
1373+
} else { /* !last_pass */
13751374
FWRITE_i32(2); /* two interstep links */
13761375

13771376
FWRITE_LATLON(wpt->latitude);

0 commit comments

Comments
 (0)