Remove VLA usage from send_audio_packet.

Also use `net_pack_u16` in that function instead of manual ntohs+memcpy
and `net_unpack_u16` in its receiving counterpart.
This commit is contained in:
iphydf 2018-07-07 17:01:22 +00:00
parent 08ff19a63b
commit b3889f0f05
No known key found for this signature in database
GPG Key ID: 3855DBA2D74403C9

View File

@ -406,11 +406,9 @@ static int handle_group_audio_packet(void *object, uint32_t groupnumber, uint32_
return -1; return -1;
} }
uint16_t sequnum; net_unpack_u16(packet, &pk->sequnum);
memcpy(&sequnum, packet, sizeof(sequnum));
pk->sequnum = net_ntohs(sequnum);
pk->length = length - sizeof(uint16_t); pk->length = length - sizeof(uint16_t);
memcpy(pk->data, packet + sizeof(uint16_t), length - sizeof(uint16_t)); memcpy(pk->data, packet + sizeof(uint16_t), pk->length);
if (queue(peer_av->buffer, pk) == -1) { if (queue(peer_av->buffer, pk) == -1) {
free(pk); free(pk);
@ -503,19 +501,26 @@ int join_av_groupchat(const Logger *log, Group_Chats *g_c, uint32_t friendnumber
*/ */
static int send_audio_packet(Group_Chats *g_c, uint32_t groupnumber, uint8_t *packet, uint16_t length) static int send_audio_packet(Group_Chats *g_c, uint32_t groupnumber, uint8_t *packet, uint16_t length)
{ {
if (!length) { if (length == 0 || length > MAX_CRYPTO_DATA_SIZE - 1 - sizeof(uint16_t)) {
return -1; return -1;
} }
Group_AV *group_av = (Group_AV *)group_get_object(g_c, groupnumber); const uint16_t plen = 1 + sizeof(uint16_t) + length;
VLA(uint8_t, data, 1 + sizeof(uint16_t) + length);
data[0] = GROUP_AUDIO_PACKET_ID;
uint16_t sequnum = net_htons(group_av->audio_sequnum); Group_AV *const group_av = (Group_AV *)group_get_object(g_c, groupnumber);
memcpy(data + 1, &sequnum, sizeof(sequnum));
memcpy(data + 1 + sizeof(sequnum), packet, length);
if (send_group_lossy_packet(g_c, groupnumber, data, SIZEOF_VLA(data)) == -1) { if (!group_av) {
return -1;
}
uint8_t data[MAX_CRYPTO_DATA_SIZE];
uint8_t *ptr = data;
*ptr++ = GROUP_AUDIO_PACKET_ID;
ptr += net_pack_u16(ptr, group_av->audio_sequnum);
memcpy(ptr, packet, length);
if (send_group_lossy_packet(g_c, groupnumber, data, plen) == -1) {
return -1; return -1;
} }