git » libfiu » commit 78f9899

libfiu: Fail fiu_enable_stack() if we know we can't do it

author Alberto Bertogli
2012-08-26 22:20:01 UTC
committer Alberto Bertogli
2012-09-02 23:02:29 UTC
parent d7130e41d4f64695cf2928c6704140512fc8f3c0

libfiu: Fail fiu_enable_stack() if we know we can't do it

When the backtrace related functions are not available, fail
fiu_enable_stack(); otherwise the caller may think it's going to work, when
it's not.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

libfiu/backtrace.c +3 -0
libfiu/fiu.c +6 -0
libfiu/internal.h +2 -0
tests/test-enable_stack.c +1 -1
tests/test-enable_stack_by_name.c +1 -1

diff --git a/libfiu/backtrace.c b/libfiu/backtrace.c
index 60c2e27..1ac8d39 100644
--- a/libfiu/backtrace.c
+++ b/libfiu/backtrace.c
@@ -13,6 +13,7 @@
 #include <sys/procfs.h>
 #include <link.h>
 
+const int have_backtrace = 1;
 
 int get_backtrace(void *buffer, int size)
 {
@@ -60,6 +61,8 @@ void *get_func_addr(const char *func_name)
 
 #include <stddef.h>	/* for NULL */
 
+const int have_backtrace = 0;
+
 int get_backtrace(void *buffer, int size)
 {
 	return 0;
diff --git a/libfiu/fiu.c b/libfiu/fiu.c
index cff61bc..ff47cf3 100644
--- a/libfiu/fiu.c
+++ b/libfiu/fiu.c
@@ -517,11 +517,17 @@ int fiu_enable_stack(const char *name, int failnum, void *failinfo,
 	if (func_pos_in_stack != -1)
 		return -1;
 
+	if (have_backtrace == 0)
+		return -1;
+
 	pf = insert_new_fail(name, failnum, failinfo, flags, PF_STACK);
 	if (pf == NULL)
 		return -1;
 
 	pf->minfo.stack.func_start = func;
+
+	/* Note get_func_end(func) can return NULL and we would still be able
+	 * to make it work, see pc_in_func() above. */
 	pf->minfo.stack.func_end = get_func_end(func);
 	pf->minfo.stack.func_pos_in_stack = func_pos_in_stack;
 	return 0;
diff --git a/libfiu/internal.h b/libfiu/internal.h
index 1cb95ff..f804735 100644
--- a/libfiu/internal.h
+++ b/libfiu/internal.h
@@ -7,6 +7,8 @@
 /* Recursion count, used both in fiu.c and fiu-rc.c */
 extern __thread int rec_count;
 
+/* Are these backtrace-related functions available, or dummies? */
+extern const int have_backtrace;
 
 /* Gets a stack trace. The pointers are stored in the given buffer, which must
  * be of the given size. The number of entries is returned.
diff --git a/tests/test-enable_stack.c b/tests/test-enable_stack.c
index 7c41405..e3e94f5 100644
--- a/tests/test-enable_stack.c
+++ b/tests/test-enable_stack.c
@@ -34,7 +34,7 @@ int main(void)
 	fiu_init(0);
 	r = fiu_enable_stack("fp-1", 1, NULL, 0, (void *) &func2, -1);
 	if (r != 0) {
-		printf("note: fiu_enable_stack() failed, skipping test\n");
+		printf("NOTE: fiu_enable_stack() failed, skipping test\n");
 		return 0;
 	}
 
diff --git a/tests/test-enable_stack_by_name.c b/tests/test-enable_stack_by_name.c
index b901333..0968aba 100644
--- a/tests/test-enable_stack_by_name.c
+++ b/tests/test-enable_stack_by_name.c
@@ -34,7 +34,7 @@ int main(void)
 	fiu_init(0);
 	r = fiu_enable_stack_by_name("fp-1", 1, NULL, 0, "func2", -1);
 	if (r != 0) {
-		printf("note: fiu_enable_stack_by_name() failed, "
+		printf("NOTE: fiu_enable_stack_by_name() failed, "
 				"skipping test\n");
 		return 0;
 	}