Skip to content

AP_NMEA_Output: use persistent pre-sized message buffers - #34403

Open
harsh839 wants to merge 2 commits into
ArduPilot:masterfrom
harsh839:pr-12514
Open

harsh839 wants to merge 2 commits into
ArduPilot:masterfrom
harsh839:pr-12514

Conversation

@harsh839

Copy link
Copy Markdown

Summary

Fixes #12514. AP_NMEA_Output no longer allocates a fresh buffer per message. It uses persistent pre-sized message buffers and a new non-allocating formatting helper, fixing latent snprintf buffer-size bugs.

Classification & Testing (check all that apply and add your own)

  • Checked by a human programmer
  • Non-functional change
  • No-binary change
  • Infrastructure change (e.g. unit tests, helper scripts)
  • Automated test(s) verify changes (e.g. unit test, autotest)
  • Tested manually, description below (e.g. SITL)
  • Tested on hardware
  • Logs attached
  • Logs available on request

Tested with SITL copter build, NMEA unit tests (tests/test_nmea_vaprint_buffer, tests/test_nmea_print, tests/test_nmea_vaprint), and check_branch_conventions.py.

Description

  • AP_Common: add NMEA::vaprint() and NMEA::vaprintf() helpers that format into caller-provided buffers without allocating, plus a test_nmea_vaprint_buffer unit test. Fixes the incorrect buffer lengths passed to the previous ::*printf variants in AP_NMEA_Output.
  • AP_NMEA_Output: store persistent, pre-sized message buffers in the message classes instead of building a temporary String per message. This removes periodic heap allocations, moves the data into contiguous memory, and keeps Mavlink copying in one piece.

Note: contribution is AI-assisted (written with the assistance of an LLM, reviewed by the human author).

Format NMEA messages directly into a caller supplied buffer instead of
allocating a new buffer with malloc() on every call. nmea_printf_buffer()
is reimplemented on top of the new nmea_vaprintf_buffer() and keeps its
previous behaviour: it returns 0 without touching the buffer if the
message does not fit, and a NUL terminated string is always produced when
it does fit.

Adds a unit test for the new formatting path.

--AI assistance: this change was AI-assisted, reviewed by a human before submission.
Keep the message and sub-string buffers as class members allocated once
for their maximum possible formatted length, instead of creating fresh
stack buffers sized by guesswork on every update() call. Sub-strings such
as the longitude are now guaranteed to be NUL terminated so they can never
be passed unterminated to the %s consumers of the NMEA message formats.

--AI assistance: this change was AI-assisted, reviewed by a human before submission.
@harsh839

Copy link
Copy Markdown
Author

Hi @WickedShell, this PR is ready for review whenever you get a chance. The failing copter test (ModeZigZag) is a known SITL flake unrelated to the NMEA changes. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nmea output code should use fixed buffer and fix snprintf issues

1 participant