Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AP_Arming: Change the initialization of the string buffer #27380

Conversation

muramura
Copy link
Contributor

@muramura muramura commented Jun 24, 2024

It's not necessary to clear the entire string buffer with zeros.
It's sufficient to set the first byte of the string buffer to NULL.
This method can free up 8 bytes of FLUSH memory.

STM32 has limited FLUSH memory.
I want to prioritize practical benefits over appearance.

AFTER
UILD SUMMARY
Build directory: /home/muramura/work2/ardupilot/build/fmuv3
Target Text (B) Data (B) BSS (B) Total Flash Used (B) Free Flash (B) External Flash Used (B)
bin/arducopter 1550208 3988 192768 1554196 526560 Not Applicable

BEFORE
BUILD SUMMARY
Build directory: /home/muramura/work2/ardupilot/build/fmuv3
Target Text (B) Data (B) BSS (B) Total Flash Used (B) Free Flash (B) External Flash Used (B)
bin/arducopter 1550284 3988 192772 1554272 526480 Not Applicable

@muramura muramura changed the title AP_Arming: change the initialization of the string buffer AP_Arming: Change the initialization of the string buffer Jun 24, 2024
@peterbarker
Copy link
Contributor

If memory serves this buffer gets memcpy'd into the packet we send on the network, leaking information from the autopilot.

Can you confirm you have checked this doesn't happen, please?

Copy link
Contributor

@peterbarker peterbarker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this, we use these as arguments to a varargs snprintf-like function.

That means we only require these to be null-terminated, and these changes do actually ensure that's the case.

Saves real numbers of bytes:

Board                    AP_Periph  blimp  bootloader  copter  heli  iofirmware  plane  rover  sub
CubeOrange-periph-heavy  *                                                                     
Durandal                            -64    *           -112    -104              -64    -64    -64
Hitec-Airspeed           *                                                                     
KakuteH7-bdshot                     -24    *           -64     -64               -32    -32    -32
MatekF405                           -16    *           -32     -32               -16    -24    -24
Pixhawk1-1M-bdshot                  -24                -40     -40               -32    -24    -32
f103-QiotekPeriph        *                                                                     
f303-Universal           *                                                                     
iomcu                                                                *                         
revo-mini                           -16    *           -40     -32               -16    -16    -24
skyviper-v2450                                         -40                                     

I gave it a spin through valgrind and it didn't find anything either.

@peterbarker peterbarker force-pushed the AP_Change_the_initialization_of_the_string_buffer branch from 4588759 to 72339e0 Compare September 4, 2024 08:03
@rmackay9
Copy link
Contributor

rmackay9 commented Sep 4, 2024

I can't say I'm a fan of this pattern really. What was one line is now two and it's less clear. Anyway..

If PeterB wants to merge it as-is then fine but I would actually vote for closing without merging, sorry

@peterbarker
Copy link
Contributor

OK, I was convinced by the flash savings.

I was concerned by the change in pattern - leaving the bytes uninitialised on the stack is safe here, but isn't safe most of the time.

tridge was also concerned with that.

Randy doesn't like the change in coding style.

So, I'm sorry, while this does achieve your goal of saving some flash space, I think in this case we are going to spend a few bytes because it makes the code slightly safer, doesn't use a pattern we're trying to avoid, and does lead to more code lines (which the reader has to decipher each time to assure themselves it is correct).

Closing this now.

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.

4 participants