Skip to content
Myr

Fix segmentation fault when using jq in threads

Before adoption: this commit came in with the history the project adopted, so no landing records it.

In previous code nomem_handler in jv_alloc.c is called only once and therefore the structure is not initialized for second and following threads. This leads to segmentation fault in multi-threading environment. This patch moves initialization of nomem_handler out of the pthread_once call.

authored by Tomas Halman · committed by Nico Williams · parent 5300fbd · Browse files · 1 file+26 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+26 added−7 removed

  • other:+26 added−7 removed

1 file+26 added−7 removed

modified:src/jv_alloc.c+26 added-7 removed
43 unmodified lines
4445#ifdef HAVE_PTHREAD_KEY_CREATE46#include <pthread.h>4748pthread_key_t nomem_handler_key;49pthread_once_t mem_once = PTHREAD_ONCE_INIT;48static pthread_key_t nomem_handler_key;49static pthread_once_t mem_once = PTHREAD_ONCE_INIT;5051/* tsd_fini is called on application exit */52/* it clears the nomem_handler allocated in the main thread */53static void tsd_fini(void) {54 struct nomem_handler *nomem_handler;55 nomem_handler = pthread_getspecific(nomem_handler_key);56 if (nomem_handler) {
1 unmodified line
58 free(nomem_handler);59 }60}6162/* The tsd_fini_thread is a destructor set by calling */63/* pthread_key_create(&nomem_handler_key, tsd_fini_thread) */64/* It is called when thread ends */65static void tsd_fini_thread(void *nomem_handler) {66 free(nomem_handler);67}6869static void tsd_init(void) {61 if (pthread_key_create(&nomem_handler_key, NULL) != 0) {70 if (pthread_key_create(&nomem_handler_key, tsd_fini_thread) != 0) {71 fprintf(stderr, "jq: error: cannot create thread specific key");72 abort();73 }74 if (atexit(tsd_fini) != 0) {75 fprintf(stderr, "jq: error: cannot set an exit handler");76 abort();77 }78}7980static void tsd_init_nomem_handler(void)81{82 if (pthread_getspecific(nomem_handler_key) == NULL) {69 struct nomem_handler *nomem_handler = calloc(1, sizeof(struct nomem_handler));70 if (pthread_setspecific(nomem_handler_key, nomem_handler) != 0) {71 fprintf(stderr, "jq: error: cannot set thread specific data");72 abort();83 struct nomem_handler *nomem_handler = calloc(1, sizeof(struct nomem_handler));84 if (pthread_setspecific(nomem_handler_key, nomem_handler) != 0) {85 fprintf(stderr, "jq: error: cannot set thread specific data");86 abort();87 }88 }89}9091void jv_nomem_handler(jv_nomem_handler_f handler, void *data) {92 pthread_once(&mem_once, tsd_init); // cannot fail93 tsd_init_nomem_handler();9495 struct nomem_handler *nomem_handler;9697 nomem_handler = pthread_getspecific(nomem_handler_key);98 if (nomem_handler == NULL) {
8 unmodified lines
107static void memory_exhausted() {108 struct nomem_handler *nomem_handler;109110 pthread_once(&mem_once, tsd_init);111 tsd_init_nomem_handler();112113 nomem_handler = pthread_getspecific(nomem_handler_key);114 if (nomem_handler)115 nomem_handler->handler(nomem_handler->data); // Maybe handler() will longjmp() to safety116 // Or not
70 unmodified lines