From 9a15f2492b1bccfa085a19c252ed5224c1fde3b2 Mon Sep 17 00:00:00 2001 From: Tor Didriksen Date: Wed, 30 Nov 2011 15:39:29 +0100 Subject: Bug#11761576 54082: HANDLE_SEGFAULT MAKES USE OF UNSAFE FUNCTIONS handle_segfault is the signal handler code of mysqld. however, it makes calls to potentially unsafe functions localtime_r, fprintf, fflush. include/my_stacktrace.h: Add safe versions of itoa() write() and snprintf(). libmysqld/CMakeLists.txt: Move signal handler to separate file. mysys/stacktrace.c: Remove unsafe function calls. sql/CMakeLists.txt: Move signal handler to separate file. sql/Makefile.am: Move signal handler to separate file. sql/mysqld.cc: Move signal handler to separate file. sql/signal_handler.cc: Remove unsafe function calls. --- include/my_stacktrace.h | 69 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/my_stacktrace.h b/include/my_stacktrace.h index e7713f46fc3..30cac3871c5 100644 --- a/include/my_stacktrace.h +++ b/include/my_stacktrace.h @@ -1,5 +1,4 @@ -/* - Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. +/* Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -12,8 +11,7 @@ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef _my_stacktrace_h_ #define _my_stacktrace_h_ @@ -63,6 +61,69 @@ void my_set_exception_pointers(EXCEPTION_POINTERS *ep); void my_write_core(int sig); #endif + + +/** + Async-signal-safe utility functions used by signal handler routines. + Declared here in order to unit-test them. + These are not general-purpose, but tailored to the signal handling routines. +*/ +/** + Converts a longlong value to string. + @param base 10 for decimal, 16 for hex values (0..9a..f) + @param val The value to convert + @param buf Assumed to point to the *end* of the buffer. + @returns Pointer to the first character of the converted string. + Negative values: + for base-10 the return string will be prepended with '-' + for base-16 the return string will contain 16 characters + Implemented with simplicity, and async-signal-safety in mind. +*/ +char *my_safe_itoa(int base, longlong val, char *buf); + +/** + Converts a ulonglong value to string. + @param base 10 for decimal, 16 for hex values (0..9a..f) + @param val The value to convert + @param buf Assumed to point to the *end* of the buffer. + @returns Pointer to the first character of the converted string. + Implemented with simplicity, and async-signal-safety in mind. +*/ +char *my_safe_utoa(int base, ulonglong val, char *buf); + +/** + A (very) limited version of snprintf. + @param to Destination buffer. + @param n Size of destination buffer. + @param fmt printf() style format string. + @returns Number of bytes written, including terminating '\0' + Supports 'd' 'i' 'u' 'x' 'p' 's' conversion. + Supports 'l' and 'll' modifiers for integral types. + Does not support any width/precision. + Implemented with simplicity, and async-signal-safety in mind. +*/ +size_t my_safe_snprintf(char* to, size_t n, const char* fmt, ...) + ATTRIBUTE_FORMAT(printf, 3, 4); + +/** + A (very) limited version of snprintf, which writes the result to STDERR. + @sa my_safe_snprintf + Implemented with simplicity, and async-signal-safety in mind. + @note Has an internal buffer capacity of 512 bytes, + which should suffice for our signal handling routines. +*/ +size_t my_safe_printf_stderr(const char* fmt, ...) + ATTRIBUTE_FORMAT(printf, 1, 2); + +/** + Writes up to count bytes from buffer to STDERR. + Implemented with simplicity, and async-signal-safety in mind. + @param buf Buffer containing data to be written. + @param count Number of bytes to write. + @returns Number of bytes written. +*/ +size_t my_write_stderr(const void *buf, size_t count); + C_MODE_END #endif /* _my_stacktrace_h_ */ -- cgit v1.2.1