Coverage Report

Created: 2025-11-09 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mosquitto/lib/handle_unsuback.c
Line
Count
Source
1
/*
2
Copyright (c) 2009-2021 Roger Light <roger@atchoo.org>
3
4
All rights reserved. This program and the accompanying materials
5
are made available under the terms of the Eclipse Public License 2.0
6
and Eclipse Distribution License v1.0 which accompany this distribution.
7
8
The Eclipse Public License is available at
9
   https://www.eclipse.org/legal/epl-2.0/
10
and the Eclipse Distribution License is available at
11
  http://www.eclipse.org/org/documents/edl-v10.php.
12
13
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
14
15
Contributors:
16
   Roger Light - initial implementation and documentation.
17
*/
18
19
#include "config.h"
20
21
#include <assert.h>
22
#include <stdio.h>
23
#include <string.h>
24
25
#ifdef WITH_BROKER
26
#  include "mosquitto_broker_internal.h"
27
#endif
28
29
#include "callbacks.h"
30
#include "mosquitto.h"
31
#include "logging_mosq.h"
32
#include "messages_mosq.h"
33
#include "mosquitto/mqtt_protocol.h"
34
#include "net_mosq.h"
35
#include "packet_mosq.h"
36
#include "property_mosq.h"
37
#include "read_handle.h"
38
#include "send_mosq.h"
39
#include "util_mosq.h"
40
41
42
int handle__unsuback(struct mosquitto *mosq)
43
31
{
44
31
  uint16_t mid;
45
31
  int rc;
46
31
  mosquitto_property *properties = NULL;
47
31
  int *reason_codes = NULL;
48
31
  int reason_code_count = 0;
49
50
31
  assert(mosq);
51
52
31
  if(mosquitto__get_state(mosq) != mosq_cs_active){
53
0
    return MOSQ_ERR_PROTOCOL;
54
0
  }
55
31
  if(mosq->in_packet.command != CMD_UNSUBACK){
56
0
    return MOSQ_ERR_MALFORMED_PACKET;
57
0
  }
58
59
31
#ifdef WITH_BROKER
60
31
  if(mosq->bridge == NULL){
61
    /* Client is not a bridge, so shouldn't be sending SUBACK */
62
0
    return MOSQ_ERR_PROTOCOL;
63
0
  }
64
31
  log__printf(NULL, MOSQ_LOG_DEBUG, "Received UNSUBACK from %s", SAFE_PRINT(mosq->id));
65
#else
66
  log__printf(mosq, MOSQ_LOG_DEBUG, "Client %s received UNSUBACK", SAFE_PRINT(mosq->id));
67
#endif
68
31
  rc = packet__read_uint16(&mosq->in_packet, &mid);
69
31
  if(rc){
70
0
    return rc;
71
0
  }
72
31
  if(mid == 0){
73
1
    return MOSQ_ERR_PROTOCOL;
74
1
  }
75
76
30
  if(mosq->protocol == mosq_p_mqtt5){
77
30
    rc = property__read_all(CMD_UNSUBACK, &mosq->in_packet, &properties);
78
30
    if(rc){
79
2
      return rc;
80
2
    }
81
82
28
    uint8_t byte;
83
28
    reason_code_count = (int)(mosq->in_packet.remaining_length - mosq->in_packet.pos);
84
28
    reason_codes = mosquitto_malloc((size_t)reason_code_count*sizeof(int));
85
28
    if(!reason_codes){
86
0
      mosquitto_property_free_all(&properties);
87
0
      return MOSQ_ERR_NOMEM;
88
0
    }
89
2.15M
    for(int i=0; i<reason_code_count; i++){
90
2.15M
      rc = packet__read_byte(&mosq->in_packet, &byte);
91
2.15M
      if(rc){
92
0
        mosquitto_FREE(reason_codes);
93
0
        mosquitto_property_free_all(&properties);
94
0
        return rc;
95
0
      }
96
2.15M
      reason_codes[i] = (int)byte;
97
2.15M
    }
98
28
  }
99
100
#ifndef WITH_BROKER
101
  callback__on_unsubscribe(mosq, mid, reason_code_count, reason_codes, properties);
102
#endif
103
28
  mosquitto_property_free_all(&properties);
104
28
  mosquitto_FREE(reason_codes);
105
106
28
  return MOSQ_ERR_SUCCESS;
107
30
}