nasa / bplib

Apache License 2.0
27 stars 13 forks source link

Const-correct object pointers #60

Closed jphickey closed 2 years ago

jphickey commented 2 years ago

For bplib API calls that accept a pointer to an object but do not modify that object, the pointer should be qualified as const. This is important as it allows this API call to be made from application code where the data object is already const. Otherwise, the application developer must "cast-away" the const-ness, which can be risky/dangerous.

Specifically affected BPLIB APIs:

Potentially affected dependent/internal APIs:

Background/Rationale: In general, C/C++ APIs that accept pointers should always be clear as to whether that pointer is used only for input (const, read-only) or may do output as well (non-const).

Note that for use within CFE/CFS, the messages received from Software Bus (SB) are now (or will be) qualified as "const" - because this is a publish/subscribe design with 1:N distribution, the same buffer might be received by many applications. If an application wants to modify the payload in some way, it must always make a copy of the buffer before making the modification, to avoid potential race conditions with other applications that may have received the same buffer.

jphickey commented 2 years ago

Note with respect to bplib_process(): This function mostly does not modify its input bundle, except in one circumstance - The internal implementation v6_receive_bundle() may determine that the block needs to be forwarded without processing, in which case the block flags are actually updated within the buffer (i.e. sdnv_write is invoked). See here:

https://github.com/nasa/bplib/blob/e6e43b3cf644aabc43feaa72aea9c5e2fd99a47f/v6/v6.c#L620-L626

The whole premise of this is a little strange/suspect, somewhat of an oxymoron - in order to indicate the block was forwarded "without processing", it must modify the very block it just said it didn't touch...?