@cryptotaxi247 / netdata-1 / commits / 1f484030e

Use unsigned char for binary data in mqtt. (#16775)

This fixes the issue with the following code: ``` *WRITE_POS(frag) = (MQTT_CPT_SUBSCRIBE << 4) | 0x2 /* [MQTT-3.8.1-1] */; ``` assigning the constant 130 which is larger than 127.

vkalintiris committed Jan 13, 2024 at 08:58 UTC 1f484030e28dc5b6844e2cdabaf1c035d549a74e
2 files changed +10 -10
mqtt_websockets/src/include/mqtt_ng.h
+1 -1
@@ -19,7 +19,7 @@ struct mqtt_ng_client;
19 * @param output pointer to memory where output will be written to. Must allow up to 4 bytes to be written.
20 * @return number of bytes written to output or <= 0 if error in which case contents of output are undefined
21 */
22 -int uint32_to_mqtt_vbi(uint32_t input, char *output);
22 +int uint32_to_mqtt_vbi(uint32_t input, unsigned char *output);
23
24 struct mqtt_lwt_properties {
25 char *will_topic;
mqtt_websockets/src/mqtt_ng.c
+9 -9
@@ -49,7 +49,7 @@ struct buffer_fragment {
49 size_t sent;
50 buffer_frag_flag_t flags;
51 void (*free_fnc)(void *ptr);
52 - char *data;
52 + unsigned char *data;
53
54 uint16_t packet_id;
55
@@ -62,8 +62,8 @@ typedef struct buffer_fragment *mqtt_msg_data;
62 // not for actual data sent
63 struct header_buffer {
64 size_t size;
65 - char *data;
66 - char *tail;
65 + unsigned char *data;
66 + unsigned char *tail;
67 struct buffer_fragment *tail_frag;
68 };
69
@@ -259,7 +259,7 @@ struct mqtt_ng_client {
259 size_t max_msg_size;
260 };
261
262 -char pingreq[] = { MQTT_CPT_PINGREQ << 4, 0x00 };
262 +unsigned char pingreq[] = { MQTT_CPT_PINGREQ << 4, 0x00 };
263
264 struct buffer_fragment ping_frag = {
265 .data = pingreq,
@@ -271,7 +271,7 @@ struct buffer_fragment ping_frag = {
271 .packet_id = 0
272 };
273
274 -int uint32_to_mqtt_vbi(uint32_t input, char *output) {
274 +int uint32_to_mqtt_vbi(uint32_t input, unsigned char *output) {
275 int i = 1;
276 *output = 0;
277
@@ -478,7 +478,7 @@ static void buffer_rebuild(struct header_buffer *buf)
478 {
479 struct buffer_fragment *frag = (struct buffer_fragment*)buf->data;
480 do {
481 - buf->tail = (char*)frag + sizeof(struct buffer_fragment);
481 + buf->tail = (unsigned char *) frag + sizeof(struct buffer_fragment);
482 buf->tail_frag = frag;
483 if (!(frag->flags & BUFFER_FRAG_DATA_EXTERNAL)) {
484 buf->tail_frag->data = buf->tail;
@@ -529,7 +529,7 @@ static void buffer_garbage_collect(struct header_buffer *buf, mqtt_wss_log_ctx_t
529 }
530 #endif
531
532 - memmove(buf->data, frag, buf->tail - (char*)frag);
532 + memmove(buf->data, frag, buf->tail - (unsigned char *) frag);
533 buffer_rebuild(buf);
534 }
535
@@ -935,7 +935,7 @@ mqtt_msg_data mqtt_ng_generate_connect(struct transaction_buffer *trx_buf,
935 DATA_ADVANCE(&trx_buf->hdr_buffer, sizeof(mqtt_protocol_name_frag), frag);
936
937 // [MQTT-3.1.2.3] Connect flags
938 - char *connect_flags = WRITE_POS(frag);
938 + unsigned char *connect_flags = WRITE_POS(frag);
939 *connect_flags = 0;
940 if (auth->username)
941 *connect_flags |= MQTT_CONNECT_FLAG_USERNAME;
@@ -1949,7 +1949,7 @@ static int send_fragment(struct mqtt_ng_client *client) {
1949 struct buffer_fragment *frag = client->main_buffer.sending_frag;
1950
1951 // for readability
1952 - char *ptr = frag->data + frag->sent;
1952 + unsigned char *ptr = frag->data + frag->sent;
1953 size_t bytes = frag->len - frag->sent;
1954
1955 size_t processed = 0;