diff -u x/libpri-1.4.0/prisched.c libpri-1.4.0/prisched.c
--- x/libpri-1.4.0/prisched.c	2006-02-14 20:22:26.000000000 +0100
+++ libpri-1.4.0/prisched.c	2007-06-07 18:16:05.000000000 +0200
@@ -35,6 +35,9 @@
 {
 	int x;
 	struct timeval tv;
+
+	if (!pri)
+		return -1;
 	for (x=1;x<MAX_SCHED;x++)
 		if (!pri->pri_sched[x].callback)
 			break;
@@ -61,6 +64,9 @@
 {
 	struct timeval *closest = NULL;
 	int x;
+
+	if (!pri)
+		return NULL;
 	/* Check subchannels */
 	if (pri->subchannel)
 		closest = pri_schedule_next(pri->subchannel);
@@ -80,6 +86,10 @@
 	void (*callback)(void *);
 	void *data;
 	pri_event *e;
+
+	if (!pri)
+		return NULL;
+
 	if (pri->subchannel) {
 		if ((e = __pri_schedule_run(pri->subchannel, tv))) {
 			return e;
@@ -105,6 +115,9 @@
 
 pri_event *pri_schedule_run(struct pri *pri)
 {
+	if (!pri)
+		return NULL;
+
 	struct timeval tv;
 	gettimeofday(&tv, NULL);
 	return __pri_schedule_run(pri, &tv);
@@ -113,6 +126,8 @@
 
 void pri_schedule_del(struct pri *pri,int id)
 {
+	if (!pri)
+		return;
 	if ((id >= MAX_SCHED) || (id < 0)) 
 		pri_error(pri, "Asked to delete sched id %d???\n", id);
 	pri->pri_sched[id].callback = NULL;
diff -u x/libpri-1.4.0/q931.c libpri-1.4.0/q931.c
--- x/libpri-1.4.0/q931.c	2006-07-28 16:41:57.000000000 +0200
+++ libpri-1.4.0/q931.c	2007-06-07 18:02:30.000000000 +0200
@@ -219,7 +219,7 @@
 #if 1
 /* Update call state with transition trace. */
 #define UPDATE_OURCALLSTATE(pri,c,newstate) do {\
-	if (pri->debug & (PRI_DEBUG_Q931_STATE) && c->ourcallstate != newstate) \
+	if (pri && (pri->debug & (PRI_DEBUG_Q931_STATE) && c->ourcallstate != newstate)) \
 		pri_message(pri, DBGHEAD "call %d on channel %d enters state %d (%s)\n", DBGINFO, \
 		            c->cr, c->channelno, newstate, callstate2str(newstate)); \
 	c->ourcallstate = newstate; \
@@ -1172,7 +1172,7 @@
 						break; \
 					} \
 					state = my_state; \
-					if (pri->debug) \
+					if (pri && (pri->debug)) \
 						pri_message(pri, "Handle Q.932 %s component\n", name); \
 					(handler)(pri, call, comp->data, comp->len); \
 					break;
@@ -1183,7 +1183,7 @@
 						break; \
 					} \
 					state = my_state; \
-					if (pri->debug & PRI_DEBUG_APDU) \
+					if (pri && (pri->debug & PRI_DEBUG_APDU)) \
 						pri_message(pri, "Q.932 %s component is not handled\n", name); \
 					break;
 
@@ -1193,7 +1193,7 @@
 	switch(next_protocol = protocol = (ie->data[i] & 0x1f)) {
 	case Q932_PROTOCOL_CMIP:
 	case Q932_PROTOCOL_ACSE:
-		if (pri->debug & PRI_DEBUG_APDU)
+		if (pri && (pri->debug & PRI_DEBUG_APDU))
 			pri_message(pri, "!! Don't know how to handle Q.932 Protocol Profile of type 0x%X\n", protocol);
 		return -1;
 	case Q932_PROTOCOL_EXTENSIONS:
@@ -1235,7 +1235,7 @@
 				Q932_HANDLE_PROC(COMP_TYPE_RETURN_ERROR, Q932_STATE_SERVICE, "ROSE return error", rose_return_error_decode);
 				Q932_HANDLE_PROC(COMP_TYPE_REJECT, Q932_STATE_SERVICE, "ROSE reject", rose_reject_decode);
 				default:
-					if (pri->debug & PRI_DEBUG_APDU)
+					if (pri && (pri->debug & PRI_DEBUG_APDU))
 						pri_message(pri, "Don't know how to handle ROSE component of type 0x%X\n", comp->type);
 					break;
 				}
@@ -1243,7 +1243,7 @@
 			case Q932_PROTOCOL_CMIP:
 				switch (comp->type) {
 				default:
-					if (pri->debug & PRI_DEBUG_APDU)
+					if (pri && (pri->debug & PRI_DEBUG_APDU))
 						pri_message(pri, "Don't know how to handle CMIP component of type 0x%X\n", comp->type);
 					break;
 				}
@@ -1251,7 +1251,7 @@
 			case Q932_PROTOCOL_ACSE:
 				switch (comp->type) {
 				default:
-					if (pri->debug & PRI_DEBUG_APDU)
+					if (pri && (pri->debug & PRI_DEBUG_APDU))
 						pri_message(pri, "Don't know how to handle ACSE component of type 0x%X\n", comp->type);
 					break;
 				}
@@ -2173,7 +2173,7 @@
 		cur = cur->next;
 	}
 	/* No call exists, make a new one */
-	if (pri->debug & PRI_DEBUG_Q931_STATE)
+	if (pri && (pri->debug & PRI_DEBUG_Q931_STATE))
 		pri_message(pri, "-- Making new call for cr %d\n", cr);
 	cur = malloc(sizeof(struct q931_call));
 	if (cur) {
@@ -2218,7 +2218,7 @@
 				prev->next = cur->next;
 			else
 				*pri->callpool = cur->next;
-			if (pri->debug & PRI_DEBUG_Q931_STATE)
+			if (pri && (pri->debug & PRI_DEBUG_Q931_STATE))
 				pri_message(pri, "NEW_HANGUP DEBUG: Destroying the call, ourstate %s, peerstate %s\n",callstate2str(cur->ourcallstate),callstate2str(cur->peercallstate));
 			if (cur->retranstimer)
 				pri_schedule_del(pri, cur->retranstimer);
@@ -2353,14 +2353,14 @@
 {
 	unsigned int x;
 	int full_ie = Q931_FULL_IE(codeset, ie->ie);
-	if (pri->debug & PRI_DEBUG_Q931_STATE)
+	if (pri && (pri->debug & PRI_DEBUG_Q931_STATE))
 		pri_message(pri, "-- Processing IE %d (cs%d, %s)\n", ie->ie, codeset, ie2str(full_ie));
 	for (x=0;x<sizeof(ies) / sizeof(ies[0]);x++) {
 		if (full_ie == ies[x].ie) {
 			if (ies[x].receive)
 				return ies[x].receive(full_ie, pri, c, msg, ie, ielen(ie));
 			else {
-				if (pri->debug & PRI_DEBUG_Q931_ANOMALY)
+				if (pri && (pri->debug & PRI_DEBUG_Q931_ANOMALY))
 					pri_error(pri, "!! No handler for IE %d (cs%d, %s)\n", ie->ie, codeset, ie2str(full_ie));
 				return -1;
 			}
@@ -2403,7 +2403,7 @@
 	/* The transmit operation might dump the q921 header, so logging the q931
 	   message body after the transmit puts the sections of the message in the
 	   right order in the log */
-	if (pri->debug & PRI_DEBUG_Q931_DUMP)
+	if (pri && (pri->debug & PRI_DEBUG_Q931_DUMP))
 		q931_dump(pri, h, len, 1);
 #ifdef LIBPRI_COUNTERS
 	pri->q931_txcount++;
@@ -2629,7 +2629,7 @@
 {
 	struct q931_call *c = data;
 	struct pri *pri = c->pri;
-	if (pri->debug & PRI_DEBUG_Q931_STATE)
+	if (pri && (pri->debug & PRI_DEBUG_Q931_STATE))
 		pri_message(pri, "Timed out looking for connect acknowledge\n");
 	q931_disconnect(pri, c, PRI_CAUSE_NORMAL_CLEARING);
 	
@@ -2639,7 +2639,7 @@
 {
 	struct q931_call *c = data;
 	struct pri *pri = c->pri;
-	if (pri->debug & PRI_DEBUG_Q931_STATE)
+	if (pri && (pri->debug & PRI_DEBUG_Q931_STATE))
 		pri_message(pri, "Timed out looking for release complete\n");
 	c->t308_timedout++;
 	c->alive = 1;
@@ -2651,7 +2651,7 @@
 	struct q931_call *c = data;
 	struct pri *pri = c->pri;
 	c->alive = 1;
-	if (pri->debug & PRI_DEBUG_Q931_STATE)
+	if (pri && (pri->debug & PRI_DEBUG_Q931_STATE))
 		pri_message(pri, "Final time-out looking for release complete\n");
 	c->t308_timedout++;
 	c->ourcallstate = Q931_CALL_STATE_NULL;
@@ -2671,7 +2671,7 @@
 {
 	struct q931_call *c = data;
 	struct pri *pri = c->pri;
-	if (pri->debug & PRI_DEBUG_Q931_STATE)
+	if (pri && (pri->debug & PRI_DEBUG_Q931_STATE))
 		pri_message(pri, "Timed out looking for release\n");
 	c->alive = 1;
 	q931_release(pri, c, PRI_CAUSE_NORMAL_CLEARING);
@@ -2922,7 +2922,7 @@
 {
 	int disconnect = 1;
 	int release_compl = 0;
-	if (pri->debug & PRI_DEBUG_Q931_STATE)
+	if (pri && (pri->debug & PRI_DEBUG_Q931_STATE))
 		pri_message(pri, "NEW_HANGUP DEBUG: Calling q931_hangup, ourstate %s, peerstate %s\n",callstate2str(c->ourcallstate),callstate2str(c->peercallstate));
 	if (!pri || !c)
 		return -1;
@@ -3025,7 +3025,7 @@
 	struct apdu_event *cur = NULL;
 
 	memset(last_ie, 0, sizeof(last_ie));
-	if (pri->debug & PRI_DEBUG_Q931_DUMP)
+	if (pri && (pri->debug & PRI_DEBUG_Q931_DUMP))
 		q931_dump(pri, h, len, 0);
 #ifdef LIBPRI_COUNTERS
 	pri->q931_rxcount++;
@@ -3050,7 +3050,7 @@
 	/* Preliminary handling */
 	switch(mh->msg) {
 	case Q931_RESTART:
-		if (pri->debug & PRI_DEBUG_Q931_STATE)
+		if (pri && (pri->debug & PRI_DEBUG_Q931_STATE))
 			pri_message(pri, "-- Processing Q.931 Restart\n");
 		/* Reset information */
 		c->channelno = -1;
@@ -3063,7 +3063,7 @@
 		c->callername[0] = '\0';
 		break;
 	case Q931_SETUP:
-		if (pri->debug & PRI_DEBUG_Q931_STATE)
+		if (pri && (pri->debug & PRI_DEBUG_Q931_STATE))
 			pri_message(pri, "-- Processing Q.931 Call Setup\n");
 		c->channelno = -1;
 		c->slotmap = -1;
@@ -3204,10 +3204,10 @@
 		case Q931_LOCKING_SHIFT:
 			y = ie->ie & 7;	/* Requested codeset */
 			/* Locking shifts couldn't go to lower codeset, and couldn't follows non-locking shifts - verify this */
-			if ((cur_codeset != codeset) && (pri->debug & PRI_DEBUG_Q931_ANOMALY))
+			if ((cur_codeset != codeset) && (pri && (pri->debug & PRI_DEBUG_Q931_ANOMALY)))
 				pri_message(pri, "XXX Locking shift immediately follows non-locking shift (from %d through %d to %d) XXX\n", codeset, cur_codeset, y);
 			if (y > 0) {
-				if ((y < codeset) && (pri->debug & PRI_DEBUG_Q931_ANOMALY))
+				if ((y < codeset) && (pri && (pri->debug & PRI_DEBUG_Q931_ANOMALY)))
 					pri_error(pri, "!! Trying to locked downshift codeset from %d to %d !!\n", codeset, y);
 				codeset = cur_codeset = y;
 			}
@@ -3223,7 +3223,7 @@
 			/* Sanity check for IE code order */
 			if (!(ie->ie & 0x80)) {
 				if (last_ie[cur_codeset] > ie->ie) {
-					if ((pri->debug & PRI_DEBUG_Q931_ANOMALY))
+					if ((pri && (pri->debug & PRI_DEBUG_Q931_ANOMALY)))
 						pri_message(pri, "XXX Out-of-order IE %d at codeset %d (last was %d)\n", ie->ie, cur_codeset, last_ie[cur_codeset]);
 				}
 				else
@@ -3234,7 +3234,7 @@
 			case PRI_SWITCH_LUCENT5E:
 			case PRI_SWITCH_ATT4ESS:
 				if (cur_codeset != codeset) {
-					if ((pri->debug & PRI_DEBUG_Q931_DUMP))
+					if ((pri && (pri->debug & PRI_DEBUG_Q931_DUMP)))
 						pri_message(pri, "XXX Ignoring IE %d for temporary codeset %d XXX\n", ie->ie, cur_codeset);
 					break;
 				}
@@ -3455,8 +3455,8 @@
 		}
 		/* Do nothing */
 		/* Also when the STATUS asks for the call of an unexisting reference send RELEASE_COMPL */
-		if ((pri->debug & PRI_DEBUG_Q931_ANOMALY) &&
-		    (c->cause != PRI_CAUSE_INTERWORKING)) 
+		if ((pri && (pri->debug & PRI_DEBUG_Q931_ANOMALY) &&
+		    (c->cause != PRI_CAUSE_INTERWORKING)))
 			pri_error(pri, "Received unsolicited status: %s\n", pri_cause2str(c->cause));
 		/* Workaround for S-12 ver 7.3 - it responds for invalid/non-implemented IEs at SETUP with null call state */
 		if (!c->sugcallstate && (c->ourcallstate != Q931_CALL_STATE_CALL_INITIATED)) {
@@ -3702,7 +3702,7 @@
 {
 	struct q931_call *c = data;
 	struct pri *pri = c->pri;
-	if (pri->debug & PRI_DEBUG_Q931_STATE)
+	if (pri && (pri->debug & PRI_DEBUG_Q931_STATE))
 		pri_message(pri, DBGHEAD "Timed out waiting for data link re-establishment\n", DBGINFO);
 
 	c->cause = PRI_CAUSE_DESTINATION_OUT_OF_ORDER;
@@ -3715,7 +3715,7 @@
 {
 	struct q931_call *c = data;
 	struct pri *pri = c->pri;
-	if (pri->debug & PRI_DEBUG_Q931_STATE)
+	if (pri && (pri->debug & PRI_DEBUG_Q931_STATE))
 		pri_message(pri, DBGHEAD "Cancel non active call after data link failure\n", DBGINFO);
 
 	c->cause = PRI_CAUSE_DESTINATION_OUT_OF_ORDER;
