Fix using sizeof for array given as function argument

Cristian Greco <>
Sat Feb 26 00:23:00 CET 2011


Hi,

when using the sizeof() operator for an array given as function argument
it returns the size of the pointer instead of the size of the array.
In this case, the affected function hex(char digest[16], char buff[33])
will just print 4 or 8 byte instead of the full length of 16 bytes, on
32bit and 64bit systems respectively.

  git fetch :cristiangreco/otp.git cg/fix-sizeof-array-arg

The problem can be easily tested with the following pieces of code (this
is a 64bit system):

>>> :~$ cat test.c
#include <stdio.h>
#include "erl_interface.h"
#include "ei.h"

int main(int argc, char **argv) {
  erl_init(NULL, 0);
  if (erl_connect_init(1, "secretcookie", 0) == -1)
    erl_err_quit("error erl_connect_init");
  if (erl_connect("") < 0)
    erl_err_quit("error erl_connect");
  fprintf(stderr, "Connected to \n");
}
>>> :~$ gcc test.c -o test -lerl_interface -lei -lpthread
>>> :~$ erl -sname test -setcookie secretcookie -detached
>>> :~$ EI_TRACELEVEL=3 ./test
ei_xconnect: Sat Feb 26 00:16:57 2011: -> CONNECT attempt to connect to test
ei_epmd_r4_port: Sat Feb 26 00:16:57 2011: -> PORT2_REQ alive=test ip=127.0.1.1
ei_epmd_r4_port: Sat Feb 26 00:16:57 2011: <- PORT2_RESP result=0 (ok)
ei_epmd_r4_port: Sat Feb 26 00:16:57 2011:    port=50046 ntype=77 proto=0 dist-high=5 dist-low=5
ei_xconnect: Sat Feb 26 00:16:57 2011: -> CONNECT connected to remote
recv_status: Sat Feb 26 00:16:57 2011: <- RECV_STATUS (ok)
recv_challenge: Sat Feb 26 00:16:57 2011: <- RECV_CHALLENGE (ok) node = , version = 5, flags = 32764, challenge = 1549668388
send_challenge_reply: Sat Feb 26 00:16:57 2011: -> SEND_CHALLENGE_REPLY (ok) challenge = -1021121626, digest = 1a80a4b784b18a0e
recv_challenge_ack: Sat Feb 26 00:16:57 2011: <- RECV_CHALLENGE_ACK (ok) digest = 165837d811901950
ei_xconnect: Sat Feb 26 00:16:57 2011: -> CONNECT (ok) remote = test
Connected to 

Please compare the output provided by the JInterface (which has pretty
similar code, but not the same bug).

>>> :~$ cat test.java 
import java.io.IOException;
import java.net.UnknownHostException;
import com.ericsson.otp.erlang.*;

public class test {
  public static void main(String[] argv) throws UnknownHostException,
      IOException, OtpAuthException {
    OtpSelf self = new OtpSelf("client", "secretcookie");
    OtpPeer other = new OtpPeer("");
    OtpConnection connection = self.connect(other);
  }
}
>>> :~$ javac -classpath ".:/usr/lib/erlang/lib/jinterface-1.5.3/priv/OtpErlang.jar" test.java
>>> :~$ java -classpath ".:/usr/lib/erlang/lib/jinterface-1.5.3/priv/OtpErlang.jar" -DOtpConnection.trace=3 test
-> MD5 CONNECT TO regolo:50046
-> HANDSHAKE sendName flags=3476 dist=5 local=
<- HANDSHAKE recvStatus (ok) local=
<- HANDSHAKE recvChallenge from= challenge=199376417 local=
-> HANDSHAKE sendChallengeReply challenge=1255235538 digest=b5fde95e50a7cbbeeadf8b5d75cb8530 local=
<- HANDSHAKE recvChallengeAck from= digest=a1010198b6e75ae75d9a21a5cefcca2d local=


Thanks,
--
Cristian Greco
GPG key ID: 0xCF4D32E4
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-patches/attachments/20110226/56caf09b/attachment.bin>


More information about the erlang-patches mailing list