Skip to content

Commit 152e218

Browse files
committed
Merge pull request #16 from gar1t/robustify
Couple asserts upgraded to handled errors
2 parents fda1273 + 628af0c commit 152e218

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

c_src/erlzmq_nif.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ NIF(erlzmq_nif_context)
138138
sizeof(erlzmq_context_t));
139139
assert(context);
140140
context->context_zmq = zmq_init(thread_count);
141-
assert(context->context_zmq);
141+
if (!context->context_zmq) {
142+
return return_zmq_errno(env, zmq_errno());
143+
}
142144

143145
char thread_socket_id[64];
144146
sprintf(thread_socket_id, "inproc://erlzmq-%ld", (long int) context);
@@ -197,7 +199,9 @@ NIF(erlzmq_nif_socket)
197199
socket->context = context;
198200
socket->socket_index = context->socket_index++;
199201
socket->socket_zmq = zmq_socket(context->context_zmq, socket_type);
200-
assert(socket->socket_zmq);
202+
if (!socket->socket_zmq) {
203+
return return_zmq_errno(env, zmq_errno());
204+
}
201205
socket->active = active;
202206
socket->mutex = enif_mutex_create("erlzmq_socket_t_mutex");
203207
assert(socket->mutex);

test/erlzmq_test.erl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ reqrep_tcp_test() ->
5555
basic_tests("tcp://127.0.0.1:5556", req, rep, active),
5656
basic_tests("tcp://127.0.0.1:5557", req, rep, passive).
5757

58+
bad_init_test() ->
59+
?assertEqual({error, einval}, erlzmq:context(-1)).
60+
5861
shutdown_stress_test() ->
5962
?assertMatch(ok, shutdown_stress_loop(10)).
6063

0 commit comments

Comments
 (0)