Skip to content

Commit

Permalink
Fixes for buf_append_utf16le()
Browse files Browse the repository at this point in the history
Make it set buf_error() when it returns -EINVAL, to ensure callers will
catch the problem. Previously, ntlm_set_string_binary() and  sspi_setup()
would not have noticed.

Also, NUL-terminate the buffer. Not accounting for it, but just putting
two zero bytes after the string so that it can be used as a
NUL-terminated (wchar_t *). Not entirely sure how sspi_setup() was
working before this, except perhaps pure luck.

Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
  • Loading branch information
David Woodhouse authored and David Woodhouse committed Jul 29, 2014
1 parent 7d7e2c1 commit 62ecb0e
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions http.c
Expand Up @@ -205,13 +205,16 @@ int buf_append_utf16le(struct oc_text_buf *buf, const char *utf8)
nr_extra = 3;
min = 0x10000;
} else {
buf->error = -EINVAL;
return -EINVAL;
}

while (nr_extra--) {
c = *(utf8++);
if ((c & 0xc0) != 0x80)
if ((c & 0xc0) != 0x80) {
buf->error = -EINVAL;
return -EINVAL;
}
utfchar <<= 6;
utfchar |= (c & 0x3f);
}
Expand All @@ -221,20 +224,26 @@ int buf_append_utf16le(struct oc_text_buf *buf, const char *utf8)
if (utfchar >= 0x10000) {
utfchar -= 0x10000;
if (buf_ensure_space(buf, 4))
return -ENOMEM;
return buf_error(buf);
buf->data[buf->pos++] = (utfchar >> 10) & 0xff;
buf->data[buf->pos++] = 0xd8 | ((utfchar >> 18) & 3);
buf->data[buf->pos++] = utfchar & 0xff;
buf->data[buf->pos++] = 0xdc | ((utfchar >> 8) & 3);
len += 4;
} else {
if (buf_ensure_space(buf, 2))
return -ENOMEM;
return buf_error(buf);
buf->data[buf->pos++] = utfchar & 0xff;
buf->data[buf->pos++] = utfchar >> 8;
len += 2;
}
}

/* Ensure UTF16 is NUL-terminated */
if (buf_ensure_space(buf, 2))
buf_error(buf);
buf->data[buf->pos] = buf->data[buf->pos + 1] = 0;

return len;
}

Expand Down

0 comments on commit 62ecb0e

Please sign in to comment.