Skip to content

Commit

Permalink
Mavgen C: Use strncpy instead of memcpy to pack char[n] fields
Browse files Browse the repository at this point in the history
Uses new macros mavlink_array_assign_[type] in place of direct calls to mav_array_memcpy.
The 'char' version then uses strncpy, while others call mav_array_memcpy as before
Update _mav_put_char_array function to use strncpy
  • Loading branch information
shancock884 committed Sep 18, 2024
1 parent feb81c4 commit f88ec4f
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 10 deletions.
34 changes: 32 additions & 2 deletions generator/C/include_v0.9/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,43 @@ static void mav_array_memcpy(void *dest, const void *src, size_t n)
}
}

/*
* Array direct assignment, for use when fields align and no byte swapping
* is required. Most are directly #defined to mav_array_memcpy, except for
* mav_array_assign_char, which uses strncpy instead.
*/
#if !MAVLINK_NEED_BYTE_SWAP && MAVLINK_ALIGNED_FIELDS
static inline void mav_array_assign_char(char *dest, const char *src, size_t n)
{
if (src == NULL) {
memset(dest, 0, n);
} else {
/* strncpy will zero pad dest array up to n bytes */
strncpy(dest, src, n);
}
}
#define mav_array_assign_uint8_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(uint8_t))
#define mav_array_assign_int8_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(int8_t))
#define mav_array_assign_uint16_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(uint16_t))
#define mav_array_assign_int16_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(int16_t))
#define mav_array_assign_uint32_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(uint32_t))
#define mav_array_assign_int32_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(int32_t))
#define mav_array_assign_uint64_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(uint64_t))
#define mav_array_assign_int64_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(int64_t))
#define mav_array_assign_float(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(float))
#define mav_array_assign_double(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(double))
#endif

/*
* Place a char array into a buffer
*/
static inline void _mav_put_char_array(char *buf, uint8_t wire_offset, const char *b, uint8_t array_length)
{
mav_array_memcpy(&buf[wire_offset], b, array_length);

if (b == NULL) {
memset(&buf[wire_offset], 0, array_length);
} else {
strncpy(&buf[wire_offset], b, array_length);
}
}

/*
Expand Down
34 changes: 32 additions & 2 deletions generator/C/include_v1.0/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,43 @@ static inline void mav_array_memcpy(void *dest, const void *src, size_t n)
}
}

/*
* Array direct assignment, for use when fields align and no byte swapping
* is required. Most are directly #defined to mav_array_memcpy, except for
* mav_array_assign_char, which uses strncpy instead.
*/
#if !MAVLINK_NEED_BYTE_SWAP && MAVLINK_ALIGNED_FIELDS
static inline void mav_array_assign_char(char *dest, const char *src, size_t n)
{
if (src == NULL) {
memset(dest, 0, n);
} else {
/* strncpy will zero pad dest array up to n bytes */
strncpy(dest, src, n);
}
}
#define mav_array_assign_uint8_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(uint8_t))
#define mav_array_assign_int8_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(int8_t))
#define mav_array_assign_uint16_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(uint16_t))
#define mav_array_assign_int16_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(int16_t))
#define mav_array_assign_uint32_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(uint32_t))
#define mav_array_assign_int32_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(int32_t))
#define mav_array_assign_uint64_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(uint64_t))
#define mav_array_assign_int64_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(int64_t))
#define mav_array_assign_float(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(float))
#define mav_array_assign_double(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(double))
#endif

/*
* Place a char array into a buffer
*/
static inline void _mav_put_char_array(char *buf, uint8_t wire_offset, const char *b, uint8_t array_length)
{
mav_array_memcpy(&buf[wire_offset], b, array_length);

if (b == NULL) {
memset(&buf[wire_offset], 0, array_length);
} else {
strncpy(&buf[wire_offset], b, array_length);
}
}

/*
Expand Down
34 changes: 32 additions & 2 deletions generator/C/include_v2.0/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,43 @@ static inline void mav_array_memcpy(void *dest, const void *src, size_t n)
}
}

/*
* Array direct assignment, for use when fields align and no byte swapping
* is required. Most are directly #defined to mav_array_memcpy, except for
* mav_array_assign_char, which uses strncpy instead.
*/
#if !MAVLINK_NEED_BYTE_SWAP && MAVLINK_ALIGNED_FIELDS
static inline void mav_array_assign_char(char *dest, const char *src, size_t n)
{
if (src == NULL) {
memset(dest, 0, n);
} else {
/* strncpy will zero pad dest array up to n bytes */
strncpy(dest, src, n);
}
}
#define mav_array_assign_uint8_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(uint8_t))
#define mav_array_assign_int8_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(int8_t))
#define mav_array_assign_uint16_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(uint16_t))
#define mav_array_assign_int16_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(int16_t))
#define mav_array_assign_uint32_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(uint32_t))
#define mav_array_assign_int32_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(int32_t))
#define mav_array_assign_uint64_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(uint64_t))
#define mav_array_assign_int64_t(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(int64_t))
#define mav_array_assign_float(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(float))
#define mav_array_assign_double(DEST,SRC,N) mav_array_memcpy(DEST,SRC,N*sizeof(double))
#endif

/*
* Place a char array into a buffer
*/
static inline void _mav_put_char_array(char *buf, uint8_t wire_offset, const char *b, uint8_t array_length)
{
mav_array_memcpy(&buf[wire_offset], b, array_length);

if (b == NULL) {
memset(&buf[wire_offset], 0, array_length);
} else {
strncpy(&buf[wire_offset], b, array_length);
}
}

/*
Expand Down
8 changes: 4 additions & 4 deletions generator/mavgen_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def generate_message_h(directory, m):
mavlink_${name_lower}_t packet;
${{scalar_fields: packet.${name} = ${putname};
}}
${{array_fields: mav_array_memcpy(packet.${name}, ${name}, sizeof(${type})*${array_length});
${{array_fields: mav_array_assign_${type}(packet.${name}, ${name}, ${array_length});
}}
memcpy(_MAV_PAYLOAD_NON_CONST(msg), &packet, MAVLINK_MSG_ID_${name}_LEN);
#endif
Expand Down Expand Up @@ -304,7 +304,7 @@ def generate_message_h(directory, m):
mavlink_${name_lower}_t packet;
${{scalar_fields: packet.${name} = ${putname};
}}
${{array_fields: mav_array_memcpy(packet.${name}, ${name}, sizeof(${type})*${array_length});
${{array_fields: mav_array_assign_${type}(packet.${name}, ${name}, ${array_length});
}}
memcpy(_MAV_PAYLOAD_NON_CONST(msg), &packet, MAVLINK_MSG_ID_${name}_LEN);
#endif
Expand Down Expand Up @@ -376,7 +376,7 @@ def generate_message_h(directory, m):
mavlink_${name_lower}_t packet;
${{scalar_fields: packet.${name} = ${putname};
}}
${{array_fields: mav_array_memcpy(packet.${name}, ${name}, sizeof(${type})*${array_length});
${{array_fields: mav_array_assign_${type}(packet.${name}, ${name}, ${array_length});
}}
_mav_finalize_message_chan_send(chan, MAVLINK_MSG_ID_${name}, (const char *)&packet, MAVLINK_MSG_ID_${name}_MIN_LEN, MAVLINK_MSG_ID_${name}_LEN, MAVLINK_MSG_ID_${name}_CRC);
#endif
Expand Down Expand Up @@ -417,7 +417,7 @@ def generate_message_h(directory, m):
mavlink_${name_lower}_t *packet = (mavlink_${name_lower}_t *)msgbuf;
${{scalar_fields: packet->${name} = ${putname};
}}
${{array_fields: mav_array_memcpy(packet->${name}, ${name}, sizeof(${type})*${array_length});
${{array_fields: mav_array_assign_${type}(packet->${name}, ${name}, ${array_length});
}}
_mav_finalize_message_chan_send(chan, MAVLINK_MSG_ID_${name}, (const char *)packet, MAVLINK_MSG_ID_${name}_MIN_LEN, MAVLINK_MSG_ID_${name}_LEN, MAVLINK_MSG_ID_${name}_CRC);
#endif
Expand Down

0 comments on commit f88ec4f

Please sign in to comment.