@@ -4893,10 +4893,26 @@ static int handle_gc_private_message(const GC_Session *c, const GC_Chat *chat, c
48934893 return 0 ;
48944894}
48954895
4896+ /** @brief Returns false if a custom packet is too large. */
4897+ static bool custom_gc_packet_length_is_valid (uint16_t length , bool lossless )
4898+ {
4899+ if (lossless ) {
4900+ if (length > MAX_GC_CUSTOM_LOSSLESS_PACKET_SIZE ) {
4901+ return false;
4902+ }
4903+ } else {
4904+ if (length > MAX_GC_CUSTOM_LOSSY_PACKET_SIZE ) {
4905+ return false;
4906+ }
4907+ }
4908+
4909+ return true;
4910+ }
4911+
48964912int gc_send_custom_private_packet (const GC_Chat * chat , bool lossless , uint32_t peer_id , const uint8_t * message ,
48974913 uint16_t length )
48984914{
4899- if (length > MAX_GC_CUSTOM_PACKET_SIZE ) {
4915+ if (! custom_gc_packet_length_is_valid ( length , lossless ) ) {
49004916 return -1 ;
49014917 }
49024918
@@ -4926,16 +4942,23 @@ int gc_send_custom_private_packet(const GC_Chat *chat, bool lossless, uint32_t p
49264942
49274943 return ret ? 0 : -5 ;
49284944}
4945+
4946+
4947+
49294948/** @brief Handles a custom private packet.
49304949 *
49314950 * @retval 0 if packet is handled correctly.
49324951 * @retval -1 if packet has invalid size.
49334952 */
4934- non_null (1 , 2 , 3 , 4 ) nullable (6 )
4953+ non_null (1 , 2 , 3 , 4 ) nullable (7 )
49354954static int handle_gc_custom_private_packet (const GC_Session * c , const GC_Chat * chat , const GC_Peer * peer ,
4936- const uint8_t * data , uint16_t length , void * userdata )
4955+ const uint8_t * data , uint16_t length , bool lossless , void * userdata )
49374956{
4938- if (data == nullptr || length == 0 || length > MAX_GC_CUSTOM_PACKET_SIZE ) {
4957+ if (!custom_gc_packet_length_is_valid (length , lossless )) {
4958+ return -1 ;
4959+ }
4960+
4961+ if (data == nullptr || length == 0 ) {
49394962 return -1 ;
49404963 }
49414964
@@ -4952,7 +4975,7 @@ static int handle_gc_custom_private_packet(const GC_Session *c, const GC_Chat *c
49524975
49534976int gc_send_custom_packet (const GC_Chat * chat , bool lossless , const uint8_t * data , uint16_t length )
49544977{
4955- if (length > MAX_GC_CUSTOM_PACKET_SIZE ) {
4978+ if (! custom_gc_packet_length_is_valid ( length , lossless ) ) {
49564979 return -1 ;
49574980 }
49584981
@@ -4978,11 +5001,15 @@ int gc_send_custom_packet(const GC_Chat *chat, bool lossless, const uint8_t *dat
49785001 * Return 0 if packet is handled correctly.
49795002 * Return -1 if packet has invalid size.
49805003 */
4981- non_null (1 , 2 , 3 , 4 ) nullable (6 )
5004+ non_null (1 , 2 , 3 , 4 ) nullable (7 )
49825005static int handle_gc_custom_packet (const GC_Session * c , const GC_Chat * chat , const GC_Peer * peer , const uint8_t * data ,
4983- uint16_t length , void * userdata )
5006+ uint16_t length , bool lossless , void * userdata )
49845007{
4985- if (data == nullptr || length == 0 || length > MAX_GC_CUSTOM_PACKET_SIZE ) {
5008+ if (!custom_gc_packet_length_is_valid (length , lossless )) {
5009+ return -1 ;
5010+ }
5011+
5012+ if (data == nullptr || length == 0 ) {
49865013 return -1 ;
49875014 }
49885015
@@ -5913,12 +5940,12 @@ bool handle_gc_lossless_helper(const GC_Session *c, GC_Chat *chat, uint32_t peer
59135940 }
59145941
59155942 case GP_CUSTOM_PACKET : {
5916- ret = handle_gc_custom_packet (c , chat , peer , data , length , userdata );
5943+ ret = handle_gc_custom_packet (c , chat , peer , data , length , true, userdata );
59175944 break ;
59185945 }
59195946
59205947 case GP_CUSTOM_PRIVATE_PACKET : {
5921- ret = handle_gc_custom_private_packet (c , chat , peer , data , length , userdata );
5948+ ret = handle_gc_custom_private_packet (c , chat , peer , data , length , true, userdata );
59225949 break ;
59235950 }
59245951
@@ -6168,12 +6195,12 @@ static bool handle_gc_lossy_packet(const GC_Session *c, GC_Chat *chat, const uin
61686195 }
61696196
61706197 case GP_CUSTOM_PACKET : {
6171- ret = handle_gc_custom_packet (c , chat , peer , data , payload_len , userdata );
6198+ ret = handle_gc_custom_packet (c , chat , peer , data , payload_len , false, userdata );
61726199 break ;
61736200 }
61746201
61756202 case GP_CUSTOM_PRIVATE_PACKET : {
6176- ret = handle_gc_custom_private_packet (c , chat , peer , data , payload_len , userdata );
6203+ ret = handle_gc_custom_private_packet (c , chat , peer , data , payload_len , false, userdata );
61776204 break ;
61786205 }
61796206
0 commit comments