[OpenWrt-Devel] [PATCH] [make_ext4fs] Explicitly call setlocale() before sorting the source directory

Wojtek Porczyk woju at invisiblethingslab.com
Wed Dec 2 10:59:48 EST 2015


I'd like to submit a patch for make_ext4fs. Currently make_ext4fs does
not call setlocale() before scandir(..., alphasort). This means that
the "C" locale is used implicitely. The attached patch makes an explicit
call to setlocale(LC_COLLATE, "C") before doing the actual sort.

See the attached patch.

regards,                        _.-._
Wojtek Porczyk               .-^'   '^-.
Invisible Things Lab         |'-.-^-.-'|
                             |  |   |  |
 I do not fear computers,    |  '-.-'  |
 I fear lack of them.        '-._ :  ,-'
    -- Isaac Asimov             `^-^-_>
-------------- next part --------------
From abb8df8b0e6459abe5d8e74aef9fe95a379e21f5 Mon Sep 17 00:00:00 2001
From: Wojtek Porczyk <woju at invisiblethingslab.com>
Date: Wed, 2 Dec 2015 16:08:01 +0100
Subject: [PATCH] make_ext4fs: explicitly call setlocale() before creating

The alphasort(3) function, which is used as comparison function for
scandir(3) is locale dependent (at least in glibc, in which it defers to
strcoll(3)). This patch explicitly calls setlocale(3), setting the
locale to "C".

Currently, there are no other calls to setlocale(), so this does not
change the behaviour. However, we'd like to be sure that in the future
no-one will break that.

Signed-off-by: Wojtek Porczyk <woju at invisiblethingslab.com>
 make_ext4fs.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/make_ext4fs.c b/make_ext4fs.c
index cb18161..dd4d0d3 100644
--- a/make_ext4fs.c
+++ b/make_ext4fs.c
@@ -33,6 +33,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <locale.h>
 /* TODO: Not implemented:
    Allocating blocks in the same block group as the file inode
@@ -66,6 +67,9 @@ static u32 build_directory_structure(const char *full_path, const char *dir_path
 	u32 dirs = 0;
 	bool needs_lost_and_found = false;
+	/* alphasort is locale-dependent; let's fix the locale to some sane value */
+	setlocale(LC_COLLATE, "C");
 	if (full_path) {
 		entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
 		if (entries < 0) {

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/openwrt-devel/attachments/20151202/aac2e1a8/attachment.sig>
-------------- next part --------------
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org

More information about the openwrt-devel mailing list