Coverage Report

Created: 2025-11-24 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mosquitto/libcommon/utf8_common.c
Line
Count
Source
1
/*
2
Copyright (c) 2016-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.
17
*/
18
19
#include "config.h"
20
21
#include <stdio.h>
22
#include "mosquitto.h"
23
24
25
BROKER_EXPORT int mosquitto_validate_utf8(const char *str, int len)
26
305
{
27
305
  int i;
28
305
  int j;
29
305
  int codelen;
30
305
  int codepoint;
31
305
  const unsigned char *ustr = (const unsigned char *)str;
32
33
305
  if(!str){
34
0
    return MOSQ_ERR_INVAL;
35
0
  }
36
305
  if(len < 0 || len > 65536){
37
10
    return MOSQ_ERR_INVAL;
38
10
  }
39
40
81.4k
  for(i=0; i<len; i++){
41
81.2k
    if(ustr[i] == 0){
42
4
      return MOSQ_ERR_MALFORMED_UTF8;
43
81.2k
    }else if(ustr[i] <= 0x7f){
44
79.5k
      codelen = 1;
45
79.5k
      codepoint = ustr[i];
46
79.5k
    }else if((ustr[i] & 0xE0) == 0xC0){
47
      /* 110xxxxx - 2 byte sequence */
48
591
      if(ustr[i] == 0xC0 || ustr[i] == 0xC1){
49
        /* Invalid bytes */
50
2
        return MOSQ_ERR_MALFORMED_UTF8;
51
2
      }
52
589
      codelen = 2;
53
589
      codepoint = (ustr[i] & 0x1F);
54
1.14k
    }else if((ustr[i] & 0xF0) == 0xE0){
55
      /* 1110xxxx - 3 byte sequence */
56
493
      codelen = 3;
57
493
      codepoint = (ustr[i] & 0x0F);
58
647
    }else if((ustr[i] & 0xF8) == 0xF0){
59
      /* 11110xxx - 4 byte sequence */
60
631
      if(ustr[i] > 0xF4){
61
        /* Invalid, this would produce values > 0x10FFFF. */
62
1
        return MOSQ_ERR_MALFORMED_UTF8;
63
1
      }
64
630
      codelen = 4;
65
630
      codepoint = (ustr[i] & 0x07);
66
630
    }else{
67
      /* Unexpected continuation byte. */
68
16
      return MOSQ_ERR_MALFORMED_UTF8;
69
16
    }
70
71
    /* Reconstruct full code point */
72
81.2k
    if(i >= len-codelen+1){
73
      /* Not enough data */
74
33
      return MOSQ_ERR_MALFORMED_UTF8;
75
33
    }
76
84.6k
    for(j=0; j<codelen-1; j++){
77
3.39k
      if((ustr[++i] & 0xC0) != 0x80){
78
        /* Not a continuation byte */
79
11
        return MOSQ_ERR_MALFORMED_UTF8;
80
11
      }
81
3.38k
      codepoint = (codepoint<<6) | (ustr[i] & 0x3F);
82
3.38k
    }
83
84
    /* Check for UTF-16 high/low surrogates */
85
81.2k
    if(codepoint >= 0xD800 && codepoint <= 0xDFFF){
86
12
      return MOSQ_ERR_MALFORMED_UTF8;
87
12
    }
88
89
    /* Check for overlong or out of range encodings */
90
    /* Checking codelen == 2 isn't necessary here, because it is already
91
     * covered above in the C0 and C1 checks.
92
     * if(codelen == 2 && codepoint < 0x0080){
93
     *   return MOSQ_ERR_MALFORMED_UTF8;
94
     * }else
95
    */
96
81.2k
    if(codelen == 3 && codepoint < 0x0800){
97
12
      return MOSQ_ERR_MALFORMED_UTF8;
98
81.2k
    }else if(codelen == 4 && (codepoint < 0x10000 || codepoint > 0x10FFFF)){
99
18
      return MOSQ_ERR_MALFORMED_UTF8;
100
18
    }
101
102
    /* Check for non-characters */
103
81.1k
    if(codepoint >= 0xFDD0 && codepoint <= 0xFDEF){
104
6
      return MOSQ_ERR_MALFORMED_UTF8;
105
6
    }
106
81.1k
    if((codepoint & 0xFFFF) == 0xFFFE || (codepoint & 0xFFFF) == 0xFFFF){
107
7
      return MOSQ_ERR_MALFORMED_UTF8;
108
7
    }
109
    /* Check for control characters */
110
81.1k
    if(codepoint <= 0x001F || (codepoint >= 0x007F && codepoint <= 0x009F)){
111
16
      return MOSQ_ERR_MALFORMED_UTF8;
112
16
    }
113
81.1k
  }
114
157
  return MOSQ_ERR_SUCCESS;
115
295
}