Skip to contentSkip to conversation

xfs_scrub: Use POSIX-conformant strerror_r

PATCH In review A. Wilcox 4 participants 6 messages Sep 6, 2025 – Sep 19, 2025

lore
Share & tools
lore

A. Wilcox

author
#0 lore raw

PATCH v2 1/1 xfs_scrub: Use POSIX-conformant strerror_r

When building xfsprogs with musl libc, strerror_r returns int as specified in POSIX. This differs from the glibc extension that returns char*. Successful calls will return 0, which will be dereferenced as a NULL pointer by (v)fprintf.

---
 scrub/common.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
scrub/common.c +2 -1
index 14cd677b..9437d0ab 100644
@@ -126,7 +126,8 @@ __str_out(
 	fprintf(stream, "%s%s: %s: ", stream_start(stream),
 			_(err_levels[level].string), descr);
 	if (error) {
-		fprintf(stream, _("%s."), strerror_r(error, buf, DESCR_BUFSZ));
+		strerror_r(error, buf, DESCR_BUFSZ);
+		fprintf(stream, _("%s."), buf);
 	} else {
 		va_start(args, format);
 		vfprintf(stream, format, args);
Signature · 1 line
2.49.0
Reviewed-by on v2 1/1
#2 lore raw
In reply to A. Wilcox
Quoted 6 lines · A. Wilcox

When building xfsprogs with musl libc, strerror_r returns int as specified in POSIX. This differs from the glibc extension that returns char*. Successful calls will return 0, which will be dereferenced as a NULL pointer by (v)fprintf.

Isn't C fun?

--D

Quoted 22 lines
---
 scrub/common.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
scrub/common.c +2 -1
index 14cd677b..9437d0ab 100644
@@ -126,7 +126,8 @@ __str_out(
 	fprintf(stream, "%s%s: %s: ", stream_start(stream),
 			_(err_levels[level].string), descr);
 	if (error) {
-		fprintf(stream, _("%s."), strerror_r(error, buf, DESCR_BUFSZ));
+		strerror_r(error, buf, DESCR_BUFSZ);
+		fprintf(stream, _("%s."), buf);
 	} else {
 		va_start(args, format);
 		vfprintf(stream, format, args);
Signature · 1 line
2.49.0
#3 lore raw
Quoted 10 lines · Darrick J. Wong
Quoted 6 lines · A. Wilcox

When building xfsprogs with musl libc, strerror_r returns int as specified in POSIX. This differs from the glibc extension that returns char*. Successful calls will return 0, which will be dereferenced as a NULL pointer by (v)fprintf.

Isn't C fun?

Ohh yes it is, this patch broke the build for me:

common.c: In function ‘__str_out’:
common.c:129:17: error: ignoring return value of ‘strerror_r’ declared with attribute ‘warn_unused_result’ [-Werror=unused-result]

  129 |                 strerror_r(error, buf, DESCR_BUFSZ);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<sigh> xfsprogs can't get the XSI version because it defines GNU_SOURCE, and you can't shut up gcc by casting the whole expression to void.

Do you folks remove the -D_GNU_SOURCE from builddefs.in when building against musl? Or do you leave the definition alone, taking advantage of the fact that #define'ing a symbol is not a guarantee of functionality?

--D

Quoted 25 lines

--D

Quoted 22 lines
---
 scrub/common.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
scrub/common.c +2 -1
index 14cd677b..9437d0ab 100644
@@ -126,7 +126,8 @@ __str_out(
 	fprintf(stream, "%s%s: %s: ", stream_start(stream),
 			_(err_levels[level].string), descr);
 	if (error) {
-		fprintf(stream, _("%s."), strerror_r(error, buf, DESCR_BUFSZ));
+		strerror_r(error, buf, DESCR_BUFSZ);
+		fprintf(stream, _("%s."), buf);
 	} else {
 		va_start(args, format);
 		vfprintf(stream, format, args);
Signature · 1 line
2.49.0
#4 lore raw
Quoted 7 lines · Darrick J. Wong
Quoted 6 lines · Darrick J. Wong
Quoted 5 lines · A. Wilcox

When building xfsprogs with musl libc, strerror_r returns int as specified in POSIX. This differs from the glibc extension that returns char*. Successful calls will return 0, which will be dereferenced as a NULL pointer by (v)fprintf.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Isn't C fun?

Ohh yes it is, this patch broke the build for me:

do you build gcc + musl?

- Andrey

Quoted 41 lines

common.c: In function ‘__str_out’:
common.c:129:17: error: ignoring return value of ‘strerror_r’ declared with attribute ‘warn_unused_result’ [-Werror=unused-result]

  129 |                 strerror_r(error, buf, DESCR_BUFSZ);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<sigh> xfsprogs can't get the XSI version because it defines GNU_SOURCE, and you can't shut up gcc by casting the whole expression to void.

Do you folks remove the -D_GNU_SOURCE from builddefs.in when building against musl? Or do you leave the definition alone, taking advantage of the fact that #define'ing a symbol is not a guarantee of functionality?

--D

Quoted 25 lines

--D

Quoted 22 lines
---
 scrub/common.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
scrub/common.c +2 -1
index 14cd677b..9437d0ab 100644
@@ -126,7 +126,8 @@ __str_out(
 	fprintf(stream, "%s%s: %s: ", stream_start(stream),
 			_(err_levels[level].string), descr);
 	if (error) {
-		fprintf(stream, _("%s."), strerror_r(error, buf, DESCR_BUFSZ));
+		strerror_r(error, buf, DESCR_BUFSZ);
+		fprintf(stream, _("%s."), buf);
 	} else {
 		va_start(args, format);
 		vfprintf(stream, format, args);
Signature · 1 line
2.49.0
#5 lore raw
Quoted 8 lines · Andrey Albershteyn
Quoted 7 lines · Darrick J. Wong
Quoted 6 lines · Darrick J. Wong
Quoted 5 lines · A. Wilcox

When building xfsprogs with musl libc, strerror_r returns int as specified in POSIX. This differs from the glibc extension that returns char*. Successful calls will return 0, which will be dereferenced as a NULL pointer by (v)fprintf.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Isn't C fun?

Ohh yes it is, this patch broke the build for me:

do you build gcc + musl?

I don't even know /how/ to do that, even on Debian.

--D

Quoted 45 lines

- Andrey

Quoted 41 lines

common.c: In function ‘__str_out’:
common.c:129:17: error: ignoring return value of ‘strerror_r’ declared with attribute ‘warn_unused_result’ [-Werror=unused-result]

  129 |                 strerror_r(error, buf, DESCR_BUFSZ);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<sigh> xfsprogs can't get the XSI version because it defines GNU_SOURCE, and you can't shut up gcc by casting the whole expression to void.

Do you folks remove the -D_GNU_SOURCE from builddefs.in when building against musl? Or do you leave the definition alone, taking advantage of the fact that #define'ing a symbol is not a guarantee of functionality?

--D

Quoted 25 lines

--D

Quoted 22 lines
---
 scrub/common.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
scrub/common.c +2 -1
index 14cd677b..9437d0ab 100644
@@ -126,7 +126,8 @@ __str_out(
 	fprintf(stream, "%s%s: %s: ", stream_start(stream),
 			_(err_levels[level].string), descr);
 	if (error) {
-		fprintf(stream, _("%s."), strerror_r(error, buf, DESCR_BUFSZ));
+		strerror_r(error, buf, DESCR_BUFSZ);
+		fprintf(stream, _("%s."), buf);
 	} else {
 		va_start(args, format);
 		vfprintf(stream, format, args);
Signature · 1 line
2.49.0