zeromq/jzmq — Issue #87: Socket option value type mismatch
Author: agexq
Created: 2011-11-07 09:35:24+00:00
State: open

Hi,

I recently noticed that the binding I grabbed from the repo on friday (2011-11-04) was triggering EINVAL error, not matter what value I used, with native lib 3.0.2.
After some investigation, I found out that in the 3.0.2 native version, there is a guard to ensure that most of the option's size match an int's size. (see https://github.com/zeromq/libzmq/blob/master/src/options.cpp for more details).
However, most options in the binding are handled as uint64_t which seems to cause the error as far as I can tell.

I propose the following patch, which solve the error on my machine (OpenSuse 11.4, Linux 2.6.37.6-0.7-desktop #1 SMP PREEMPT 2011-07-21 02:17:24 +0200 x86_64 x86_64 x86_64 GNU/Linux).

I basically used int32_t instead of uint64_t, except for AFFINITY, which is documented as a uint64_t in the native lib.

123c123
< case ZMQ_AFFINITY:
---
>
135c135
< uint64_t optval = 0;
---
> int32_t optval = 0;
146a147,165
>	case ZMQ_AFFINITY:
>	{
>	void *s = get_socket (env, obj, 1);
> jlong ret = 0;
> int rc = 0;
> int err = 0;
>
> uint64_t optval = 0;
> size_t optvallen = sizeof(optval);
> rc = zmq_getsockopt (s, option, &optval, &optvallen);
> err = zmq_errno();
> ret = (jlong) optval;
>
> if (rc != 0) {
> raise_exception (env, err);
> return 0L;
> }
> return ret;
>	}
219c238
< case ZMQ_AFFINITY:
---
>
228c247
< uint64_t optval = (uint64_t) value;
---
> int32_t optval = (int32_t) value;
247a267,282
> case ZMQ_AFFINITY:
>	{
>	void *s = get_socket (env, obj, 1);
> int rc = 0;
> int err = 0;
> uint64_t ival = (uint64_t) optval;
>	size_t optvallen = sizeof(ival);
>	rc = zmq_setsockopt (s, option, &ival, optvallen);
>	err = zmq_errno();
>
> if (rc != 0) {
> raise_exception (env, err);
> }
> return;
>
>	}

What is your opinion about it ? Am I missing something ?
Cheers
Aurélien