Skip to content

Commit 057771f

Browse files
authored
Prefer strerror_r over strerror for thread-safe errno (#14764)
Prefer the thread safe `strerror_r` C call over the thread unsafe `strerror`.
1 parent 70ed2d0 commit 057771f

File tree

17 files changed

+27
-2
lines changed

17 files changed

+27
-2
lines changed

src/errno.cr

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,17 @@ enum Errno
4545

4646
# :nodoc:
4747
def unsafe_message(&)
48-
pointer = LibC.strerror(value)
49-
yield Bytes.new(pointer, LibC.strlen(pointer))
48+
{% if LibC.has_method?(:strerror_r) %}
49+
buffer = uninitialized UInt8[256]
50+
if LibC.strerror_r(value, buffer, buffer.size) == 0
51+
yield Bytes.new(buffer.to_unsafe, LibC.strlen(buffer))
52+
else
53+
yield "(???)".to_slice
54+
end
55+
{% else %}
56+
pointer = LibC.strerror(value)
57+
yield Bytes.new(pointer, LibC.strlen(pointer))
58+
{% end %}
5059
end
5160

5261
# returns the value of libc's errno.

src/lib_c/aarch64-android/c/string.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ lib LibC
55
fun memcmp(__lhs : Void*, __rhs : Void*, __n : SizeT) : Int
66
fun strcmp(__lhs : Char*, __rhs : Char*) : Int
77
fun strerror(__errno_value : Int) : Char*
8+
fun strerror_r(Int, Char*, SizeT) : Int
89
fun strlen(__s : Char*) : SizeT
910
end

src/lib_c/aarch64-darwin/c/string.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ lib LibC
55
fun memcmp(x0 : Void*, x1 : Void*, x2 : SizeT) : Int
66
fun strcmp(x0 : Char*, x1 : Char*) : Int
77
fun strerror(x0 : Int) : Char*
8+
fun strerror_r(Int, Char*, SizeT) : Int
89
fun strlen(x0 : Char*) : SizeT
910
end

src/lib_c/aarch64-linux-gnu/c/string.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ lib LibC
55
fun memcmp(s1 : Void*, s2 : Void*, n : SizeT) : Int
66
fun strcmp(s1 : Char*, s2 : Char*) : Int
77
fun strerror(errnum : Int) : Char*
8+
fun strerror_r = __xpg_strerror_r(Int, Char*, SizeT) : Int
89
fun strlen(s : Char*) : SizeT
910
end

src/lib_c/aarch64-linux-musl/c/string.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ lib LibC
55
fun memcmp(x0 : Void*, x1 : Void*, x2 : SizeT) : Int
66
fun strcmp(x0 : Char*, x1 : Char*) : Int
77
fun strerror(x0 : Int) : Char*
8+
fun strerror_r(Int, Char*, SizeT) : Int
89
fun strlen(x0 : Char*) : SizeT
910
end

src/lib_c/arm-linux-gnueabihf/c/string.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ lib LibC
55
fun memcmp(s1 : Void*, s2 : Void*, n : SizeT) : Int
66
fun strcmp(s1 : Char*, s2 : Char*) : Int
77
fun strerror(errnum : Int) : Char*
8+
fun strerror_r = __xpg_strerror_r(Int, Char*, SizeT) : Int
89
fun strlen(s : Char*) : SizeT
910
end

src/lib_c/i386-linux-gnu/c/string.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ lib LibC
55
fun memcmp(s1 : Void*, s2 : Void*, n : SizeT) : Int
66
fun strcmp(s1 : Char*, s2 : Char*) : Int
77
fun strerror(errnum : Int) : Char*
8+
fun strerror_r = __xpg_strerror_r(Int, Char*, SizeT) : Int
89
fun strlen(s : Char*) : SizeT
910
end

src/lib_c/i386-linux-musl/c/string.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ lib LibC
55
fun memcmp(x0 : Void*, x1 : Void*, x2 : SizeT) : Int
66
fun strcmp(x0 : Char*, x1 : Char*) : Int
77
fun strerror(x0 : Int) : Char*
8+
fun strerror_r(Int, Char*, SizeT) : Int
89
fun strlen(x0 : Char*) : SizeT
910
end

src/lib_c/wasm32-wasi/c/string.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ lib LibC
55
fun memcmp(x0 : Void*, x1 : Void*, x2 : SizeT) : Int
66
fun strcmp(x0 : Char*, x1 : Char*) : Int
77
fun strerror(x0 : Int) : Char*
8+
fun strerror_r(Int, Char*, SizeT) : Int
89
fun strlen(x0 : Char*) : ULong
910
end

src/lib_c/x86_64-darwin/c/string.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ lib LibC
55
fun memcmp(x0 : Void*, x1 : Void*, x2 : SizeT) : Int
66
fun strcmp(x0 : Char*, x1 : Char*) : Int
77
fun strerror(x0 : Int) : Char*
8+
fun strerror_r(Int, Char*, SizeT) : Int
89
fun strlen(x0 : Char*) : SizeT
910
end

0 commit comments

Comments
 (0)