From d59c7c8b35fedc2b247afee56d91c2a44101253d Mon Sep 17 00:00:00 2001 From: Nebuleon Fumika Date: Sun, 19 Jun 2016 19:48:48 +0000 Subject: [PATCH 1/3] More correctly handle the case where plain char is signed In _FAT_directory_lfnLength, the check 'ch < 0x20' incorrectly catches any extended UTF-8 bytes if the plain 'char' type is signed: 0x80 < 0x20 ... 0xEF < 0x20 are interpreted as (negative value < 32). The check for 0xF0 and above also fails to work properly due to 0xF0 being negative as well. --- source/directory.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/directory.c b/source/directory.c index ff849e2..18641f9 100644 --- a/source/directory.c +++ b/source/directory.c @@ -98,7 +98,8 @@ static int _FAT_directory_lfnLength (const char* name) { } // Make sure the name doesn't contain any control codes or codes not representable in UCS-2 for (i = 0; i < nameLength; i++) { - if (name[i] < 0x20 || name[i] >= ABOVE_UCS_RANGE) { + unsigned char ch = (unsigned char) name[i]; + if (ch < 0x20 || ch >= ABOVE_UCS_RANGE) { return -1; } } From 3fde6ce161db5293a302ef5b839483065526c3b5 Mon Sep 17 00:00:00 2001 From: Nebuleon Fumika Date: Sun, 19 Jun 2016 19:58:02 +0000 Subject: [PATCH 2/3] Set errno to ENOTEMPTY when removing a non-empty directory The value that was previously set, EPERM, implies that the user didn't have the (write) permission to remove the directory. --- source/fatdir.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/fatdir.c b/source/fatdir.c index fe0e781..435d85b 100644 --- a/source/fatdir.c +++ b/source/fatdir.c @@ -136,7 +136,7 @@ int _FAT_unlink_r (struct _reent *r, const char *path) { if (!_FAT_directory_isDot (&dirContents)) { // The directory had something in it that isn't a reference to itself or it's parent _FAT_unlock(&partition->lock); - r->_errno = EPERM; + r->_errno = ENOTEMPTY; return -1; } nextEntry = _FAT_directory_getNextEntry (partition, &dirContents); From b7c4d38b320b8da0c9e318dbbdc2cc0ad966a93b Mon Sep 17 00:00:00 2001 From: Nebuleon Fumika Date: Mon, 20 Jun 2016 07:19:37 +0000 Subject: [PATCH 3/3] Don't set errno at the end of a directory if no error occurred In _FAT_dirnext, which implements a function similar to readdir, don't set errno to ENOENT, because reaching the end of a directory is not an error. This conforms to POSIX readdir and is consistent with _FAT_read not setting errno when reaching EOF. --- source/fatdir.c | 1 - 1 file changed, 1 deletion(-) diff --git a/source/fatdir.c b/source/fatdir.c index 435d85b..1ff2677 100644 --- a/source/fatdir.c +++ b/source/fatdir.c @@ -588,7 +588,6 @@ int _FAT_dirnext_r (struct _reent *r, DIR_ITER *dirState, char *filename, struct // Make sure there is another file to report on if (! state->validEntry) { _FAT_unlock(&state->partition->lock); - r->_errno = ENOENT; return -1; }