Skip to content

Revert "Mavgen C: Use strncpy instead of memcpy to pack char[n] fields"#1143

Merged
peterbarker merged 2 commits intoArduPilot:masterfrom
julianoes:pr-revert-strncpy
Nov 1, 2025
Merged

Revert "Mavgen C: Use strncpy instead of memcpy to pack char[n] fields"#1143
peterbarker merged 2 commits intoArduPilot:masterfrom
julianoes:pr-revert-strncpy

Conversation

@julianoes
Copy link
Copy Markdown
Contributor

This reverts commit ff57508 from #922.

The change broke the param_value field of PARAM_EXT_... messages which is defined as char[128] and can contain arbitrary data such as int/float or binary data.

Ideally, the type would be defined as uint8_t[128] and the issue avoided, however, this can't be changed at this stage as it would change the CRC_EXTRA and make the message incompatible.

Unfortunately, I don't see another way to fix this than to revert this change.

@shancock884, @hamishwillee

This reverts commit ff57508.

The change broke the param_value field of PARAM_EXT_... messages which
is defined as char[128] and can contain arbitrary data such as
int/float or binary data.

Ideally, the type would be defined as uint8_t[128] and the issue
avoided, however, this can't be changed at this stage as it would change
the CRC_EXTRA and make the message incompatible.

Unfortunately, I don't see another way to fix this than to revert this
change.
@peterbarker
Copy link
Copy Markdown
Contributor

@DonLakeFlyer this might also be relevant to #1142

@peterbarker
Copy link
Copy Markdown
Contributor

@julianoes I'd like to see a second patch in this PR adding a comment as to why we're using memcpy - so we don't break your toys again.

We tried to use strncpy in the past but it broke things.
@peterbarker
Copy link
Copy Markdown
Contributor

I'm definitely leaning towards the "unbreak things by merging this".

But I'd also like @shancock884 to comment first as his patches were trying to fix real problems.

Perhaps we need to add another API which passes in the maximum number of bytes of data which should be copied into the passed-in buffer? That parameter missing is just a sore thumb as it is.

@shancock884
Copy link
Copy Markdown
Contributor

Hmmm, ok. So if I understand right, char datatype has being used for a field which is really a binary blob.
I would have to agree to @julianoes - I don't see any clean ways to handle this.
1/ fix the data type - but break use of the message until everyone catches up.
2/ start a new 'correct' message, and deprecate this one, so that people migrate to correct one over time.
3/ some flag in the XML to say "this char[] is a blob not a string".
4/ revert memcpy/strncpy change as proposed.

In the short term 4/ seems like the 'least worst' option to fix it. (1/ is nasty, and 2/ can take years!).
3/ seems like a horrible cludge, but may be an option to allow best of both worlds?

@peterbarker
Copy link
Copy Markdown
Contributor

Thanks @shancock884 - sadly I think we do need to go with option 4 as we've seriously broken the PX4_EXT stuff.

Merging this one, thanks @julianoes

@peterbarker peterbarker merged commit 5977f44 into ArduPilot:master Nov 1, 2025
23 checks passed
@julianoes
Copy link
Copy Markdown
Contributor Author

@shancock884

1/ We really can't do that, as it causes breakage, unfortunately.
2/ Yes, I want to do param v3, but ArduPilot doesn't want it, from what I hear.
3/ Interesting idea... I wonder how this would matter or not for languages other than C. I haven't studied the other implemetnations.
4/ Done...

@julianoes
Copy link
Copy Markdown
Contributor Author

Thanks @peterbarker for merging.

@julianoes julianoes deleted the pr-revert-strncpy branch November 3, 2025 00:33
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.

3 participants