Skip to content

Commit

Permalink
syscall, runtime: always call XSI strerror_r
Browse files Browse the repository at this point in the history
This does the right thing for either glibc or musl on GNU/Linux.

Based on patch by Sören Tempel.

Change-Id: If2969e131f0fae456d58b35d839d8abe191fcc59
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/454176
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Bypass: Ian Lance Taylor <iant@google.com>
  • Loading branch information
ianlancetaylor authored and Ian Lance Taylor committed Nov 30, 2022
1 parent 5e658f4 commit fef6aa3
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 48 deletions.
1 change: 1 addition & 0 deletions libgo/Makefile.am
Expand Up @@ -465,6 +465,7 @@ runtime_files = \
runtime/go-nanotime.c \
runtime/go-now.c \
runtime/go-nosys.c \
runtime/go-strerror.c \
runtime/go-reflect-call.c \
runtime/go-setenv.c \
runtime/go-signal.c \
Expand Down
6 changes: 5 additions & 1 deletion libgo/Makefile.in
Expand Up @@ -247,7 +247,7 @@ am__objects_4 = runtime/aeshash.lo runtime/go-assert.lo \
runtime/go-fieldtrack.lo runtime/go-matherr.lo \
runtime/go-memclr.lo runtime/go-memmove.lo \
runtime/go-memequal.lo runtime/go-nanotime.lo \
runtime/go-now.lo runtime/go-nosys.lo \
runtime/go-now.lo runtime/go-nosys.lo runtime/go-strerror.lo \
runtime/go-reflect-call.lo runtime/go-setenv.lo \
runtime/go-signal.lo runtime/go-unsafe-pointer.lo \
runtime/go-unsetenv.lo runtime/go-unwind.lo \
Expand Down Expand Up @@ -917,6 +917,7 @@ runtime_files = \
runtime/go-nanotime.c \
runtime/go-now.c \
runtime/go-nosys.c \
runtime/go-strerror.c \
runtime/go-reflect-call.c \
runtime/go-setenv.c \
runtime/go-signal.c \
Expand Down Expand Up @@ -1390,6 +1391,8 @@ runtime/go-now.lo: runtime/$(am__dirstamp) \
runtime/$(DEPDIR)/$(am__dirstamp)
runtime/go-nosys.lo: runtime/$(am__dirstamp) \
runtime/$(DEPDIR)/$(am__dirstamp)
runtime/go-strerror.lo: runtime/$(am__dirstamp) \
runtime/$(DEPDIR)/$(am__dirstamp)
runtime/go-reflect-call.lo: runtime/$(am__dirstamp) \
runtime/$(DEPDIR)/$(am__dirstamp)
runtime/go-setenv.lo: runtime/$(am__dirstamp) \
Expand Down Expand Up @@ -1457,6 +1460,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-reflect-call.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-setenv.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-signal.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-strerror.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-unsafe-pointer.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-unsetenv.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@runtime/$(DEPDIR)/go-unwind.Plo@am__quote@
Expand Down
22 changes: 9 additions & 13 deletions libgo/go/syscall/errstr.go
Expand Up @@ -4,23 +4,19 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build !hurd && !linux
// +build !hurd,!linux

package syscall

//sysnb strerror_r(errnum int, buf []byte) (err Errno)
//strerror_r(errnum _C_int, buf *byte, buflen Size_t) _C_int
import "internal/bytealg"

//extern go_strerror
func go_strerror(_C_int, *byte, Size_t) _C_int

func Errstr(errnum int) string {
for len := 128; ; len *= 2 {
b := make([]byte, len)
errno := strerror_r(errnum, b)
for size := 128; ; size *= 2 {
b := make([]byte, size)
errno := go_strerror(_C_int(errnum), &b[0], Size_t(len(b)))
if errno == 0 {
i := 0
for b[i] != 0 {
i++
}
i := bytealg.IndexByte(b, 0)
// Lowercase first letter: Bad -> bad, but
// STREAM -> STREAM.
if i > 1 && 'A' <= b[0] && b[0] <= 'Z' && 'a' <= b[1] && b[1] <= 'z' {
Expand All @@ -29,7 +25,7 @@ func Errstr(errnum int) string {
return string(b[:i])
}
if errno != ERANGE {
return "errstr failure"
return "strerror_r failure"
}
}
}
34 changes: 0 additions & 34 deletions libgo/go/syscall/errstr_glibc.go

This file was deleted.

37 changes: 37 additions & 0 deletions libgo/runtime/go-strerror.c
@@ -0,0 +1,37 @@
/* go-strerror.c -- wrapper around XSI-compliant strerror_r.
Copyright 2022 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file. */

/* There are two version of strerror_r on GNU/Linux: a GNU-specific
and an XSI-compliant version. The former version is only available
on glibc. Since glibc 2.13, the XSI-compliant version is also
provided by glibc if _GNU_SOURCE is not defined. Since the
entirety of gofrontend is compiled with _GNU_SOURCE, this file
exists to selectively undefine it and provides an alias to the
XSI-compliant version of strerror_r(3). */

#ifdef __linux__

This comment has been minimized.

Copy link
@nmeum

nmeum Nov 30, 2022

Contributor

@ianlancetaylor I haven't tested this but I believe you also need the #undef / #define dance on GNU Hurd since they also use glibc (Hurd also used errstr_glibc.go previously via //go:build tags). Maybe #ifdef __GLIBC__ instead of #ifdef __linux__ is more appropriate here?

Thanks for merging the patch!

This comment has been minimized.

Copy link
@ianlancetaylor

ianlancetaylor Nov 30, 2022

Author Contributor

Not sure that the compiler will reliably define GLIBC, but I'll add a check for hurd. Thanks.


/* Force selection of XSI-compliant strerror_r by glibc. */
#undef XOPEN_SOURCE
#define XOPEN_SOURCE 600
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200112L
#undef _GNU_SOURCE

#endif /* __linux__ */

#include <string.h>

#ifndef HAVE_STRERROR_R
// Provided by go-nosys.c if not provided by libc itself.
extern int strerror_r (int, char *, size_t);
#endif

int
go_strerror (int errnum, char *buf, size_t buflen)
{
return strerror_r (errnum, buf, buflen);
}

0 comments on commit fef6aa3

Please sign in to comment.