Skip to content

Commit abce42b

Browse files
committed
runtime/cgo: add error checks for safe stack retrieval on Unix
1 parent f062d7b commit abce42b

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/runtime/cgo/gcc_stack_unix.c

+16-5
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,30 @@ x_cgo_getstackbound(uintptr bounds[2])
1717
pthread_attr_t attr;
1818
void *addr;
1919
size_t size;
20+
int err;
2021

2122
// Needed before pthread_getattr_np, too, since before glibc 2.32
2223
// it did not call pthread_attr_init in all cases (see #65625).
23-
pthread_attr_init(&attr);
24+
err = pthread_attr_init(&attr);
25+
if (err != 0)
26+
fatalf("pthread_attr_init failed: %d", err);
2427
#if defined(__GLIBC__) || defined(__BIONIC__) || (defined(__sun) && !defined(__illumos__))
2528
// pthread_getattr_np is a GNU extension supported in glibc.
2629
// Solaris is not glibc but does support pthread_getattr_np
2730
// (and the fallback doesn't work...). Illumos does not.
28-
pthread_getattr_np(pthread_self(), &attr); // GNU extension
29-
pthread_attr_getstack(&attr, &addr, &size); // low address
31+
err = pthread_getattr_np(pthread_self(), &attr);
32+
if (err != 0)
33+
fatalf("pthread_getattr_np failed: %d", err);
34+
err = pthread_attr_getstack(&attr, &addr, &size);
35+
if (err != 0)
36+
fatalf("pthread_attr_getstack failed: %d", err);
3037
#elif defined(__illumos__)
31-
pthread_attr_get_np(pthread_self(), &attr);
32-
pthread_attr_getstack(&attr, &addr, &size); // low address
38+
err = pthread_attr_get_np(pthread_self(), &attr);
39+
if (err != 0)
40+
fatalf("pthread_attr_get_np failed: %d", err);
41+
err = pthread_attr_getstack(&attr, &addr, &size);
42+
if (err != 0)
43+
fatalf("pthread_attr_getstack failed: %d", err);
3344
#else
3445
// We don't know how to get the current stacks, leave it as
3546
// 0 and the caller will use an estimate based on the current

0 commit comments

Comments
 (0)