Skip to content

Commit ae0886c

Browse files
committed
Fix path JSON: use snprintf length, not declared array size, for MMSI keys
1 parent d02e2bd commit ae0886c

3 files changed

Lines changed: 19 additions & 6 deletions

File tree

Source/Application/WebViewer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -973,8 +973,8 @@ std::string WebViewer::buildMultiPathJSON(ReceiverTracker *s, const std::string
973973
if (mmsi >= 1 && mmsi <= 999999999)
974974
{
975975
char keybuf[12];
976-
snprintf(keybuf, sizeof(keybuf), "%d", mmsi);
977-
w.key(keybuf).raw_val(s ? s->getPathJSON(mmsi) : std::string("{}"));
976+
int n = snprintf(keybuf, sizeof(keybuf), "%d", mmsi);
977+
w.key(keybuf, n).raw_val(s ? s->getPathJSON(mmsi) : std::string("{}"));
978978
}
979979
}
980980
catch (const std::invalid_argument &)

Source/JSON/StringBuilder.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,19 @@ namespace JSON
721721
return *this;
722722
}
723723

724+
// Explicit (ptr, len) overload for runtime-built keys (e.g. snprintf
725+
// into a stack buffer). Avoids the KeyRef array ctor, which captures
726+
// the declared array size N-1 — correct for literals, wrong for
727+
// partially-filled buffers.
728+
Writer &key(const char *s, size_t n)
729+
{
730+
reserve_more(n + 4);
731+
put_sep_raw();
732+
put_kvkey_raw(s, n);
733+
need_sep = false;
734+
return *this;
735+
}
736+
724737
// Like val() but for already-serialised JSON fragments. Honors need_sep.
725738
Writer &raw_val(const char *raw, size_t rawlen)
726739
{

Source/Tracking/DB.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ std::string DB::getAllPathJSON()
376376
break;
377377

378378
char keybuf[16];
379-
snprintf(keybuf, sizeof(keybuf), "%u", ship.mmsi);
380-
w.key(keybuf);
379+
int n = snprintf(keybuf, sizeof(keybuf), "%u", ship.mmsi);
380+
w.key(keybuf, n);
381381
writeSinglePathJSONCompact(ptr, w);
382382
}
383383
ptr = ships[ptr].incoming.next;
@@ -482,8 +482,8 @@ std::string DB::getAllPathJSONSince(std::time_t since)
482482
if (ship.mmsi != 0 && hasPathPointsSince(ptr, since))
483483
{
484484
char keybuf[16];
485-
snprintf(keybuf, sizeof(keybuf), "%u", ship.mmsi);
486-
w.key(keybuf);
485+
int n = snprintf(keybuf, sizeof(keybuf), "%u", ship.mmsi);
486+
w.key(keybuf, n);
487487
writeSinglePathJSONCompactSince(ptr, since, w);
488488
}
489489
ptr = ships[ptr].incoming.next;

0 commit comments

Comments
 (0)