Skip to content

Commit

Permalink
Handle overflow problem reported in #23
Browse files Browse the repository at this point in the history
1. Assert that names must be non-zero length.
2. Keep unicode_to_utf8 from underflowing.
  • Loading branch information
verdammelt committed May 28, 2017
1 parent 5af0791 commit cfdd80f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/mapi_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ mapi_attr_read (size_t len, unsigned char *buf)
CHECKINT32(idx, len); a->names[i].len = GETINT32(buf+idx); idx += 4;

/* read the data into a buffer */
/* read the data into a buffer */
assert(a->names[i].len != 0);
assert(idx+a->names[i].len <= len);
a->names[i].data = unicode_to_utf8(a->names[i].len, buf+idx);

Expand Down
43 changes: 22 additions & 21 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,29 @@ unicode_to_utf8 (size_t len, unsigned char* buf)
int j = 0;
unsigned char *utf8 = malloc (3 * len/2 + 1); /* won't get any longer than this */

for (i = 0; i < len - 1; i += 2)
{
uint32 c = GETINT16(buf + i);
if (c <= 0x007f)
{
utf8[j++] = 0x00 | ((c & 0x007f) >> 0);
}
else if (c < 0x07ff)
{
utf8[j++] = 0xc0 | ((c & 0x07c0) >> 6);
utf8[j++] = 0x80 | ((c & 0x003f) >> 0);
}
else
{
utf8[j++] = 0xe0 | ((c & 0xf000) >> 12);
utf8[j++] = 0x80 | ((c & 0x0fc0) >> 6);
utf8[j++] = 0x80 | ((c & 0x003f) >> 0);
}
if (len > 0) {
for (i = 0; i < len - 1; i += 2)
{
uint32 c = GETINT16(buf + i);
if (c <= 0x007f)
{
utf8[j++] = 0x00 | ((c & 0x007f) >> 0);
}
else if (c < 0x07ff)
{
utf8[j++] = 0xc0 | ((c & 0x07c0) >> 6);
utf8[j++] = 0x80 | ((c & 0x003f) >> 0);
}
else
{
utf8[j++] = 0xe0 | ((c & 0xf000) >> 12);
utf8[j++] = 0x80 | ((c & 0x0fc0) >> 6);
utf8[j++] = 0x80 | ((c & 0x003f) >> 0);
}
}
}

utf8[j] = '\0';

return utf8;
}

0 comments on commit cfdd80f

Please sign in to comment.