Skip to content

Commit 4d5df0c

Browse files
committed
runtime/cgo: add error check for safe stack retrieval
1 parent f062d7b commit 4d5df0c

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/runtime/cgo/gcc_stack_unix.c

+12-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#define _GNU_SOURCE
99
#endif
1010

11+
#include <errno.h>
12+
#include <string.h>
1113
#include <pthread.h>
1214
#include "libcgo.h"
1315

@@ -20,16 +22,21 @@ x_cgo_getstackbound(uintptr bounds[2])
2022

2123
// Needed before pthread_getattr_np, too, since before glibc 2.32
2224
// it did not call pthread_attr_init in all cases (see #65625).
23-
pthread_attr_init(&attr);
25+
if (pthread_attr_init(&attr) != 0)
26+
fatalf("pthread_attr_init failed: %s", strerror(errno));
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+
if (pthread_getattr_np(pthread_self(), &attr) != 0) // GNU extension
32+
fatalf("pthread_getattr_np failed: %s", strerror(errno));
33+
if (pthread_attr_getstack(&attr, &addr, &size) != 0) // low address
34+
fatalf("pthread_attr_getstack failed: %s", strerror(errno));
3035
#elif defined(__illumos__)
31-
pthread_attr_get_np(pthread_self(), &attr);
32-
pthread_attr_getstack(&attr, &addr, &size); // low address
36+
if (pthread_attr_get_np(pthread_self(), &attr) != 0) // Solaris extension
37+
fatalf("pthread_attr_get_np failed: %s", strerror(errno));
38+
if (pthread_attr_getstack(&attr, &addr, &size) != 0) // low address
39+
fatalf("pthread_attr_getstack failed: %s", strerror(errno));
3340
#else
3441
// We don't know how to get the current stacks, leave it as
3542
// 0 and the caller will use an estimate based on the current

0 commit comments

Comments
 (0)