Skip to content

Commit bcd12d3

Browse files
committed
Fix potential issue with heap buffer overflow.
Thanks to sgbhat2 for fuzztesting the library and finding out.
1 parent c974604 commit bcd12d3

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/http_utils.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include <stdio.h>
3535
#include <stdlib.h>
36+
#include <ctype.h>
3637
#include <fstream>
3738
#include <iomanip>
3839
#include <iterator>
@@ -304,12 +305,13 @@ size_t http_unescape(std::string& val)
304305
{
305306
if (val.empty()) return 0;
306307

307-
int rpos = 0;
308-
int wpos = 0;
308+
unsigned int rpos = 0;
309+
unsigned int wpos = 0;
309310

310311
unsigned int num;
312+
unsigned int size = val.size();
311313

312-
while ('\0' != val[rpos])
314+
while (rpos < size && val[rpos] != '\0')
313315
{
314316
switch (val[rpos])
315317
{
@@ -319,8 +321,8 @@ size_t http_unescape(std::string& val)
319321
rpos++;
320322
break;
321323
case '%':
322-
if ( (1 == sscanf (val.substr(rpos + 1).c_str(), "%2x", &num)) ||
323-
(1 == sscanf (val.substr(rpos + 1).c_str(), "%2X", &num))
324+
if (size > rpos + 2 && ((1 == sscanf (val.substr(rpos + 1, 2).c_str(), "%2x", &num)) ||
325+
(1 == sscanf (val.substr(rpos + 1, 2).c_str(), "%2X", &num)))
324326
)
325327
{
326328
val[wpos] = (unsigned char) num;

0 commit comments

Comments
 (0)