Skip to contentSkip to conversation

erofs-utils: fix endiannes issue

PATCH In review Alexander Egorenkov 4 participants 5 messages Apr 29, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: fix endiannes issue

From: Super User <[email protected]>

Macros __BYTE_ORDER, __LITTLE_ENDIAN and __BIG_ENDIAN are defined in user space header 'endian.h'. Not including this header results in the condition #if __BYTE_ORDER == __LITTLE_ENDIAN being always true, even on BE architectures (e.g. s390x). Due to this bug the compressor library was built for LE byte-order on BE arch s390x.

Fixes bc99c763e3fe ("erofs-utils: switch to effective unaligned access")
---
 include/erofs/defs.h | 1 +
 1 file changed, 1 insertion(+)
include/erofs/defs.h +1 -0
index 051a270531ca..196dfa8191a8 100644
@@ -19,6 +19,7 @@ extern "C"
 #include <inttypes.h>
 #include <limits.h>
 #include <stdbool.h>
+#include <endian.h>
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
Signature · 1 line
2.49.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · Alexander Egorenkov

From: Super User <[email protected]>

Macros __BYTE_ORDER, __LITTLE_ENDIAN and __BIG_ENDIAN are defined in user space header 'endian.h'. Not including this header results in the condition #if __BYTE_ORDER == __LITTLE_ENDIAN being always true, even on BE architectures (e.g. s390x). Due to this bug the compressor library was built for LE byte-order on BE arch s390x.

Fixes bc99c763e3fe ("erofs-utils: switch to effective unaligned access")
---
  include/erofs/defs.h | 1 +
  1 file changed, 1 insertion(+)
include/erofs/defs.h +0 -0
index 051a270531ca..196dfa8191a8 100644

include/erofs/defs.h extern "C" · line 23

Quoted 5 lines
include/erofs/defs.h +1 -0
@@ -19,6 +19,7 @@ extern "C"
  #include <inttypes.h>
  #include <limits.h>
  #include <stdbool.h>
+#include <endian.h>
 
Quoted 2 lines
include/erofs/defs.h +0 -0
  #ifdef HAVE_CONFIG_H
  #include <config.h>
Reviewed-by
#2 lore raw
Quoted 17 lines · Hongbo Li
Quoted 14 lines · Alexander Egorenkov

From: Super User <[email protected]>

Macros __BYTE_ORDER, __LITTLE_ENDIAN and __BIG_ENDIAN are defined in user space header 'endian.h'. Not including this header results in the condition #if __BYTE_ORDER == __LITTLE_ENDIAN being always true, even on BE architectures (e.g. s390x). Due to this bug the compressor library was built for LE byte-order on BE arch s390x.

Fixes bc99c763e3fe ("erofs-utils: switch to effective unaligned access")

---
  include/erofs/defs.h | 1 +

  1 file changed, 1 insertion(+)
Quoted 11 lines

include/erofs/defs.h extern "C" · line 22

Quoted 9 lines
include/erofs/defs.h +1 -0
index 051a270531ca..196dfa8191a8 100644
@@ -19,6 +19,7 @@ extern "C"
   #include <inttypes.h>
   #include <limits.h>
   #include <stdbool.h>
+#include <endian.h>

Yes, it makes sense.

Reviewed-by Ian Kent Reviewed-by Hongbo Li on v1 1/1
#3 lore raw
In reply to Alexander Egorenkov

Hi Alexander,

Quoted 1 line · Alexander Egorenkov

From: Super User <[email protected]>

Thanks for catching this, the "From:" line seems invalid, so I change it as "From: Alexander Egorenkov <[email protected]>"

Quoted 18 lines

Macros __BYTE_ORDER, __LITTLE_ENDIAN and __BIG_ENDIAN are defined in user space header 'endian.h'. Not including this header results in the condition #if __BYTE_ORDER == __LITTLE_ENDIAN being always true, even on BE architectures (e.g. s390x). Due to this bug the compressor library was built for LE byte-order on BE arch s390x.

Fixes bc99c763e3fe ("erofs-utils: switch to effective unaligned access")
---
 include/erofs/defs.h | 1 +
 1 file changed, 1 insertion(+)
include/erofs/defs.h +0 -0
index 051a270531ca..196dfa8191a8 100644

include/erofs/defs.h extern "C" · line 22

Quoted 4 lines
include/erofs/defs.h +1 -0
@@ -19,6 +19,7 @@ extern "C"
 #include <inttypes.h>
 #include <limits.h>
 #include <stdbool.h>
+#include <endian.h>

I guess it could break MacOS compilation, so I update as below:

From d55344291092b69a2ba6f11dbcda52fa534ac124 Mon Sep 17 00:00:00 2001
From: Alexander Egorenkov <[email protected]>
Date: Tue, 29 Apr 2025 09:30:52 +0200
Subject: [PATCH] erofs-utils: fix endiannes issue

Macros __BYTE_ORDER, __LITTLE_ENDIAN and __BIG_ENDIAN are defined in user space header 'endian.h'. Not including this header results in the condition #if __BYTE_ORDER == __LITTLE_ENDIAN being always true, even on BE architectures (e.g. s390x). Due to this bug the compressor library was built for LE byte-order on BE arch s390x.

Fixes bc99c763e3fe ("erofs-utils: switch to effective unaligned access")
---
 configure.ac         |  1 +
 include/erofs/defs.h | 15 +++++++++++++++
 2 files changed, 16 insertions(+)
configure.ac +1 -0
index 6e1e7a1..88f1cbe 100644
@@ -194,6 +194,7 @@ AC_ARG_WITH(selinux,
 AC_CHECK_HEADERS(m4_flatten([
 	dirent.h
 	execinfo.h
+	endian.h
 	fcntl.h
 	getopt.h
 	inttypes.h
include/erofs/defs.h +15 -0
index 051a270..21e0f09 100644
@@ -24,6 +24,21 @@ extern "C"
 #include <config.h>
 #endif
 
+#ifdef HAVE_ENDIAN_H
+#include <endian.h>
+#else
+/* Use GNU C predefined macros as a fallback */
+#ifndef __BYTE_ORDER
+#define __BYTE_ORDER	__BYTE_ORDER__
+#endif
+#ifndef __LITTLE_ENDIAN
+#define __LITTLE_ENDIAN	__ORDER_LITTLE_ENDIAN__
+#endif
+#ifndef __BIG_ENDIAN
+#define __BIG_ENDIAN	__ORDER_BIG_ENDIAN__
+#endif
+#endif
+
 #ifdef HAVE_LINUX_TYPES_H
 #include <linux/types.h>
 #endif
Signature · 1 line
2.30.2
#4 lore raw

Hi,

Quoted 8 lines · Gao Xiang

Hi Alexander,

Quoted 1 line · Alexander Egorenkov

From: Super User <[email protected]>

Thanks for catching this, the "From:" line seems invalid, so I change it as "From: Alexander Egorenkov <[email protected]>"

Thanks!
Argh, sorry for the wrong From field.

Quoted 64 lines

I guess it could break MacOS compilation, so I update as below:

From d55344291092b69a2ba6f11dbcda52fa534ac124 Mon Sep 17 00:00:00 2001
From: Alexander Egorenkov <[email protected]>
Date: Tue, 29 Apr 2025 09:30:52 +0200
Subject: [PATCH] erofs-utils: fix endiannes issue

Macros __BYTE_ORDER, __LITTLE_ENDIAN and __BIG_ENDIAN are defined in user space header 'endian.h'. Not including this header results in the condition #if __BYTE_ORDER == __LITTLE_ENDIAN being always true, even on BE architectures (e.g. s390x). Due to this bug the compressor library was built for LE byte-order on BE arch s390x.

Fixes bc99c763e3fe ("erofs-utils: switch to effective unaligned access")
---
 configure.ac         |  1 +
 include/erofs/defs.h | 15 +++++++++++++++
 2 files changed, 16 insertions(+)
configure.ac +1 -0
index 6e1e7a1..88f1cbe 100644
@@ -194,6 +194,7 @@ AC_ARG_WITH(selinux,
 AC_CHECK_HEADERS(m4_flatten([
 	dirent.h
 	execinfo.h
+	endian.h
 	fcntl.h
 	getopt.h
 	inttypes.h
include/erofs/defs.h +15 -0
index 051a270..21e0f09 100644
@@ -24,6 +24,21 @@ extern "C"
 #include <config.h>
 #endif
 
+#ifdef HAVE_ENDIAN_H
+#include <endian.h>
+#else
+/* Use GNU C predefined macros as a fallback */
+#ifndef __BYTE_ORDER
+#define __BYTE_ORDER	__BYTE_ORDER__
+#endif
+#ifndef __LITTLE_ENDIAN
+#define __LITTLE_ENDIAN	__ORDER_LITTLE_ENDIAN__
+#endif
+#ifndef __BIG_ENDIAN
+#define __BIG_ENDIAN	__ORDER_BIG_ENDIAN__
+#endif
+#endif
+
 #ifdef HAVE_LINUX_TYPES_H
 #include <linux/types.h>
 #endif
Signature · 1 line
2.30.2

Thanks!
Regards
Alex