Skip to content

Commit 37a0613

Browse files
committed
Add safety checks
1 parent 484f93d commit 37a0613

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/lib/ndpi_serializer.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ int ndpi_json_string_escape(const char *src, int src_len, char *dst, int dst_max
9999

100100
if (c < 0x20 /* ' ' */ || c == 0x7F) {
101101
; // Non-printable ASCII character (skip)
102-
} else if (c < 0x7F) {
103-
/* Valid ASCII character (escape if required) */
102+
} else if (c >= 0x20 && c <= 0x7E) {
103+
// Valid ASCII character (escape if required by JSON)
104104
switch (c) {
105105
case '\\':
106106
case '"':
@@ -132,19 +132,19 @@ int ndpi_json_string_escape(const char *src, int src_len, char *dst, int dst_max
132132
dst[j++] = c;
133133
}
134134

135-
} else if ((c >= 0xC2 && c <= 0xDF) &&
135+
} else if ((c >= 0xC2 && c <= 0xDF) && (src_len - i) >= 2 &&
136136
((u_char) src[i+1] >= 0x80 && (u_char) src[i+1] <= 0xBF)) {
137137
// 2-byte sequence (U+0080 to U+07FF)
138138
dst[j++] = c;
139139
dst[j++] = src[++i];
140-
} else if ((c >= 0xE0 && c <= 0xEF) &&
140+
} else if ((c >= 0xE0 && c <= 0xEF) && (src_len - i) >= 3 &&
141141
((u_char) src[i+1] >= 0x80 && (u_char) src[i+1] <= 0xBF) &&
142142
((u_char) src[i+2] >= 0x80 && (u_char) src[i+2] <= 0xBF)) {
143143
// 3-byte sequence (U+0800 to U+FFFF)
144144
dst[j++] = c;
145145
dst[j++] = src[++i];
146146
dst[j++] = src[++i];
147-
} else if ((c >= 0xF0 && c <= 0xF4) &&
147+
} else if ((c >= 0xF0 && c <= 0xF4) && (src_len - i) >= 4 &&
148148
((u_char) src[i+1] >= 0x80 && (u_char) src[i+1] <= 0xBF) &&
149149
((u_char) src[i+2] >= 0x80 && (u_char) src[i+2] <= 0xBF) &&
150150
((u_char) src[i+3] >= 0x80 && (u_char) src[i+3] <= 0xBF)) {

0 commit comments

Comments
 (0)