fix(companion): bound the build-date copy; fit the version stamp in the frame

Two halves of the same problem: the stamped version reached the device but
not the client.

RESP_CODE_DEVICE_INFO carries the build date in 12 bytes and the version in
20, NUL included -- 11 and 19 usable characters. The version field is
written with strzcpy() and truncates safely; the build date was a bare
strcpy() into a memset region, so a longer string simply ran past the end
into the manufacturer-name field. It never showed because strzcpy()
overwrote that field on the very next line. Benign today, and driven by a
compile-time constant rather than anything remote, but it is an unbounded
copy into a fixed frame slot and the only reason it was invisible is luck
about the write order. Now strzcpy(..., 12) like every other string there.

The stamp then had to fit. "v1.16.0-tth-0dbaba04" is 20 chars, so the node
reported "v1.16.0-tth-0dbaba0" -- readable, but a character short of the SHA
it was built from. "04 Aug 2026 11:28" is 17, so the time was cut clean off.
Now a 7-char SHA with a "-tth" marker (19 with the dirty '+', 18 without)
and a yymmdd hhmm date (11). The budget never sacrifices the base version or
the dirty marker -- a lost '+' would make an uncommitted build look
reproducible -- and warns instead of truncating silently if it ever cannot
fit.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5 committed 2026-08-04 11:43:53 +02:00
1 parent 0dbaba046c
commit 7c071d1515
2 files changed
+31 -5

No files matched your search

+7 -2
View File
@@ -1029,8 +1029,13 @@ void MyMesh::handleCmdFrame(size_t len) {
uint32_t reported_ble_pin = _serial->isConnectionSecure() ? _prefs.ble_pin : 0;
memcpy(&out_frame[i], &reported_ble_pin, 4);
i += 4;
memset(&out_frame[i], 0, 12);
strcpy((char *)&out_frame[i], FIRMWARE_BUILD_DATE);
// 12-byte field = 11 chars + NUL. strcpy() here was unbounded: the default
// FIRMWARE_BUILD_DATE literal ("6 Jun 2026") fits, but a stamped one from
// tools/build_version.py does not, and the overrun ran into the
// manufacturer-name field below -- invisibly, because strzcpy() overwrote it
// on the next line. strzcpy() truncates and NUL-pads instead (so the memset
// is redundant), same as every other string in this frame.
StrHelper::strzcpy((char *)&out_frame[i], FIRMWARE_BUILD_DATE, 12);
i += 12;
StrHelper::strzcpy((char *)&out_frame[i], board.getManufacturerName(), 40);
i += 40;
+24 -3
View File
@@ -43,14 +43,35 @@ except Exception:
sha, dirty = "nogit", ""
try:
sha = git(cwd, "rev-parse", "--short", "HEAD")
sha = git(cwd, "rev-parse", "--short=7", "HEAD")
if subprocess.call(["git", "diff", "--quiet"], cwd=cwd) != 0:
dirty = "+"
except Exception:
pass
version = "%s-tth-%s%s" % (base, sha, dirty)
build_date = datetime.now().strftime("%d %b %Y %H:%M")
# Both strings have to survive the companion protocol's fixed-width fields
# (RESP_CODE_DEVICE_INFO in companion_radio/MyMesh.cpp): 20 B for the version
# and 12 B for the build date, NUL included, so 19 and 11 usable characters.
# Anything longer is silently truncated on the wire -- which first showed up as
# a node reporting "v1.16.0-tth-956238c" for SHA 956238c3, and a build date of
# "04 Aug 2026 " with the time cut clean off. The repeater's text console has no
# such limit, but one format everywhere beats two.
VER_LIMIT, DATE_LIMIT = 19, 11
# Budget: base and the dirty marker are never sacrificed -- a lost '+' would
# turn an uncommitted build into something that looks reproducible. The SHA
# gives up characters instead; git needs 7 for uniqueness in a repo this size,
# so this only bites if upstream's version string grows unusually long.
marker = "-tth"
room = VER_LIMIT - len(base) - len(marker) - len(dirty)
version = "%s%s%s%s" % (base, marker, sha[:max(4, room)], dirty)
if len(version) > VER_LIMIT:
print("build_version.py: WARNING: %r is %d chars, will truncate to %d on "
"the companion protocol" % (version, len(version), VER_LIMIT))
# yymmdd hhmm = exactly 11. Not pretty, but complete and sortable; the readable
# "%d %b %Y %H:%M" is 17 and lost its time to the 12-byte field.
build_date = datetime.now().strftime("%y%m%d %H%M")[:DATE_LIMIT]
env.Append(CPPDEFINES=[
("FIRMWARE_VERSION", env.StringifyMacro(version)),
("FIRMWARE_BUILD_DATE", env.StringifyMacro(build_date)),