xroche/coffeecatch logo

xroche/coffeecatch

Free

CoffeeCatch, a tiny native POSIX signal catcher (especially useful for JNI code on Android/Dalvik)

FreeFree tier
Type
Open Source

About xroche/coffeecatch

CoffeeCatch is a tiny native POSIX signal catcher designed to gracefully recover from signals such as SIGSEGV and SIGBUS by treating them as exceptions. It is particularly useful for JNI code on Android/Dalvik but can also be used in non-Java C projects. The library provides simple macros (COFFEE_TRY, COFFEE_CATCH, COFFEE_END) to protect code sections. In JNI environments, the COFFEE_TRY_JNI macro automatically throws a Java exception upon catching a signal. While it handles most gentle crashes (null pointer dereferencing, integer division, stack overflow), it does not recover from allocator or mutex corruption. The handler is thread-safe, but the client must have exclusive control over signal handlers. For best stack traces, libraries should be compiled with -funwind-tables.

Key Features

Graceful recovery from POSIX signals (SIGSEGV, SIGBUS, etc.) as exceptions
Simple macros: COFFEE_TRY, COFFEE_CATCH, COFFEE_END for protected code sections
COFFEE_TRY_JNI macro for automatic Java exception throwing in JNI context
Thread-safe signal handling
Supports null pointer dereferencing, integer division, stack overflow handling
Easily integrable: just add coffeecatch.c and coffeejni.c to your build

Pros & Cons

Pros
  • Lightweight and easy to integrate into existing projects
  • Provides exception-like handling for fatal signals
  • Well-suited for Android JNI development where crash recovery is needed
  • Open-source and free (MIT-like license implied by GitHub repository)
Cons
  • Cannot recover from allocator/mutex corruption or complex crashes
  • Requires exclusive control over signal handlers (conflicts with other signal handling libraries)
  • Best results require compilation with -funwind-tables for stack traces
  • Not a comprehensive error recovery solution; only handles 'gentle' crashes

Best For

Protecting JNI native code on Android/Dalvik from crashesRecovering from segmentation faults in C/C++ applicationsImplementing fault-tolerant code sections in POSIX environments