Skip to content

Commit

Permalink
Change output buffer size semantics for UTF functions.
Browse files Browse the repository at this point in the history
Make them consistent with other string functions: now output buffer size
includes potential null terminator, i.e. this is total size. This change
also means that if output buffer isn't large enough it can be left
unterminated (indicated by the -ENAMETOOLONG return value).
  • Loading branch information
relan committed Dec 26, 2016
1 parent e62b969 commit 758e96d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion libexfat/exfat.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ int exfat_find_used_sectors(const struct exfat* ef, off_t* a, off_t* b);
void exfat_stat(const struct exfat* ef, const struct exfat_node* node,
struct stat* stbuf);
void exfat_get_name(const struct exfat_node* node,
char buffer[UTF8_BYTES(EXFAT_NAME_MAX)]);
char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1]);
uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry);
uint16_t exfat_add_checksum(const void* entry, uint16_t sum);
le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1,
Expand Down
4 changes: 2 additions & 2 deletions libexfat/lookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static int lookup_name(struct exfat* ef, struct exfat_node* parent,

*node = NULL;

rc = utf8_to_utf16(buffer, name, EXFAT_NAME_MAX, n);
rc = utf8_to_utf16(buffer, name, EXFAT_NAME_MAX + 1, n);
if (rc != 0)
return rc;

Expand Down Expand Up @@ -194,7 +194,7 @@ int exfat_split(struct exfat* ef, struct exfat_node** parent,
exfat_put_node(ef, *parent);
return -ENOENT;
}
rc = utf8_to_utf16(name, p, EXFAT_NAME_MAX, n);
rc = utf8_to_utf16(name, p, EXFAT_NAME_MAX + 1, n);
if (rc != 0)
{
exfat_put_node(ef, *parent);
Expand Down
4 changes: 2 additions & 2 deletions libexfat/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ static int readdir(struct exfat* ef, const struct exfat_node* parent,
goto error;
}
if (utf16_to_utf8(ef->label, label->name,
sizeof(ef->label) - 1, EXFAT_ENAME_MAX) != 0)
sizeof(ef->label), EXFAT_ENAME_MAX) != 0)
goto error;
break;

Expand Down Expand Up @@ -1303,7 +1303,7 @@ int exfat_set_label(struct exfat* ef, const char* label)
struct exfat_entry_label entry;

memset(label_utf16, 0, sizeof(label_utf16));
rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX, strlen(label));
rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX + 1, strlen(label));
if (rc != 0)
return rc;

Expand Down
18 changes: 16 additions & 2 deletions libexfat/utf.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ int utf16_to_utf8(char* output, const le16_t* input, size_t outsize,
char* outp = output;
wchar_t wc;

while (inp - input < insize && le16_to_cpu(*inp))
while (inp - input < insize)
{
inp = utf16_to_wchar(inp, &wc, insize - (inp - input));
if (inp == NULL)
Expand All @@ -122,6 +122,13 @@ int utf16_to_utf8(char* output, const le16_t* input, size_t outsize,
exfat_error("name is too long");
return -ENAMETOOLONG;
}
if (wc == 0)
return 0;
}
if (outp - output >= outsize)
{
exfat_error("name is too long");
return -ENAMETOOLONG;
}
*outp = '\0';
return 0;
Expand Down Expand Up @@ -202,7 +209,7 @@ int utf8_to_utf16(le16_t* output, const char* input, size_t outsize,
le16_t* outp = output;
wchar_t wc;

while (inp - input < insize && *inp)
while (inp - input < insize)
{
inp = utf8_to_wchar(inp, &wc, insize - (inp - input));
if (inp == NULL)
Expand All @@ -216,6 +223,13 @@ int utf8_to_utf16(le16_t* output, const char* input, size_t outsize,
exfat_error("name is too long");
return -ENAMETOOLONG;
}
if (wc == 0)
break;
}
if (outp - output >= outsize)
{
exfat_error("name is too long");
return -ENAMETOOLONG;
}
*outp = cpu_to_le16(0);
return 0;
Expand Down
4 changes: 2 additions & 2 deletions libexfat/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ void exfat_stat(const struct exfat* ef, const struct exfat_node* node,
}

void exfat_get_name(const struct exfat_node* node,
char buffer[UTF8_BYTES(EXFAT_NAME_MAX)])
char buffer[UTF8_BYTES(EXFAT_NAME_MAX) + 1])
{
if (utf16_to_utf8(buffer, node->name, UTF8_BYTES(EXFAT_NAME_MAX),
if (utf16_to_utf8(buffer, node->name, UTF8_BYTES(EXFAT_NAME_MAX) + 1,
EXFAT_NAME_MAX) != 0)
exfat_bug("failed to convert name to UTF-8");
}
Expand Down
2 changes: 1 addition & 1 deletion mkfs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ static int setup_volume_label(le16_t label[EXFAT_ENAME_MAX + 1], const char* s)
memset(label, 0, (EXFAT_ENAME_MAX + 1) * sizeof(le16_t));
if (s == NULL)
return 0;
return utf8_to_utf16(label, s, EXFAT_ENAME_MAX, strlen(s));
return utf8_to_utf16(label, s, EXFAT_ENAME_MAX + 1, strlen(s));
}

static uint32_t setup_volume_serial(uint32_t user_defined)
Expand Down

0 comments on commit 758e96d

Please sign in to comment.