1
0
cuberite-2a/src/OSSupport/StackTrace.cpp
linnemannr ee34e7131a Fix FreeBSD/clang errors caused by -Werror
With FreeBSD/clang, -Werror combined with the configured warning flags yields
some fatal errors, specifically related to signed conversion, 64 to 32 bit
conversion, and tautological compares.

CONTRIBUTORS

	Add myself to the contributor list

src/Generating/FinishGen.cpp

	In cFinishGenPassiveMobs::GetRandomMob(), change the type of RandMob
	from size_t to the difference_type of the ListOfSpawnables iterator
	MobIter. Using size_t triggers a 64 bit to 32 bit conversion if the
	difference_type of the iterator class is 64 bit

	Also explicitly cast the noise expression to unsigned long so we don't
	get a signed conversion warning from the modulo against
	ListOfSpawnables.size()

src/OSSupport/StackTrace.cpp

	FreeBSD 10 and above includes a non glibc implementation of benchmark()
	for which size_t, not int, is the return type. To account for this and
	prevent a signed conversion warning, abstract the type for numItems with
	a macro btsize

src/StringUtils.h

	In StringToInteger(), correct a tautological compare warning for
	unsigned types with the template. If T is unsigned, comparing
	std::numeric_limits<T>::min() to the unsigned result is always
	false. That control can enter this branch in an evaluated template with
	an unsigned type T may also permit a signed number to be parsed and
	erroneously stripped of its signedness at runtime. To guard against this
	and avoid the warning in the case that the number parsed from the string
	is non-positive, return false and don't try to parse if T is unsigned
	and control enters the non-positive branch
2015-05-30 02:23:57 -06:00

52 lines
1.1 KiB
C++

// StackTrace.cpp
// Implements the functions to print current stack traces
#include "Globals.h"
#include "StackTrace.h"
#ifdef _WIN32
#include "../StackWalker.h"
#else
#include <execinfo.h>
#include <unistd.h>
#endif
// FreeBSD uses size_t for the return type of backtrace()
#if defined(__FreeBSD__) && (__FreeBSD__ >= 10)
#define btsize size_t
#else
#define btsize int
#endif
void PrintStackTrace(void)
{
#ifdef _WIN32
// Reuse the StackWalker from the LeakFinder project already bound to MCS
// Define a subclass of the StackWalker that outputs everything to stdout
class PrintingStackWalker :
public StackWalker
{
virtual void OnOutput(LPCSTR szText) override
{
puts(szText);
}
} sw;
sw.ShowCallstack();
#else
// Use the backtrace() function to get and output the stackTrace:
// Code adapted from http://stackoverflow.com/questions/77005/how-to-generate-a-stacktrace-when-my-gcc-c-app-crashes
void * stackTrace[30];
btsize numItems = backtrace(stackTrace, ARRAYCOUNT(stackTrace));
backtrace_symbols_fd(stackTrace, numItems, STDERR_FILENO);
#endif
}