diff options
author | Rickard Green <rickard@erlang.org> | 2020-04-22 20:07:09 +0200 |
---|---|---|
committer | Rickard Green <rickard@erlang.org> | 2020-04-27 14:34:22 +0200 |
commit | f4b6405f3b645c9fdd29f119f241763dabe673ea (patch) | |
tree | 7fd4cd096fc8a61132939b6aedc692edb814164c /lib/erl_interface/src | |
parent | ec9151458d0d425011b3ee2cb8aed7796b6bdbc8 (diff) | |
download | erlang-f4b6405f3b645c9fdd29f119f241763dabe673ea.tar.gz |
Fix error checking in various ei functions
Diffstat (limited to 'lib/erl_interface/src')
-rw-r--r-- | lib/erl_interface/src/connect/ei_connect.c | 56 | ||||
-rw-r--r-- | lib/erl_interface/src/connect/eirecv.c | 48 | ||||
-rw-r--r-- | lib/erl_interface/src/connect/send.c | 37 | ||||
-rw-r--r-- | lib/erl_interface/src/connect/send_exit.c | 50 | ||||
-rw-r--r-- | lib/erl_interface/src/connect/send_reg.c | 43 | ||||
-rw-r--r-- | lib/erl_interface/src/encode/encode_trace.c | 18 | ||||
-rw-r--r-- | lib/erl_interface/src/global/global_names.c | 37 | ||||
-rw-r--r-- | lib/erl_interface/src/global/global_register.c | 60 | ||||
-rw-r--r-- | lib/erl_interface/src/global/global_unregister.c | 52 | ||||
-rw-r--r-- | lib/erl_interface/src/global/global_whereis.c | 40 | ||||
-rw-r--r-- | lib/erl_interface/src/misc/show_msg.c | 6 |
11 files changed, 288 insertions, 159 deletions
diff --git a/lib/erl_interface/src/connect/ei_connect.c b/lib/erl_interface/src/connect/ei_connect.c index 40efdd101a..0850d225a8 100644 --- a/lib/erl_interface/src/connect/ei_connect.c +++ b/lib/erl_interface/src/connect/ei_connect.c @@ -1588,27 +1588,46 @@ int ei_rpc_to(ei_cnode *ec, int fd, char *mod, char *fun, ei_x_buff x; erlang_pid *self = ei_self(ec); + int err = ERL_ERROR; self->num = fd; /* encode header */ - ei_x_new_with_version(&x); - ei_x_encode_tuple_header(&x, 2); /* A */ + if (ei_x_new_with_version(&x) < 0) + goto einval; + if (ei_x_encode_tuple_header(&x, 2) < 0) /* A */ + goto einval; - self->num = fd; - ei_x_encode_pid(&x, self); /* A 1 */ + if (ei_x_encode_pid(&x, self) < 0) /* A 1 */ + goto einval; - ei_x_encode_tuple_header(&x, 5); /* B A 2 */ - ei_x_encode_atom(&x, "call"); /* B 1 */ - ei_x_encode_atom(&x, mod); /* B 2 */ - ei_x_encode_atom(&x, fun); /* B 3 */ - ei_x_append_buf(&x, buf, len); /* B 4 */ - ei_x_encode_atom(&x, "user"); /* B 5 */ - - /* ei_x_encode_atom(&x,"user"); */ - ei_send_reg_encoded(fd, self, "rex", x.buff, x.index); - ei_x_free(&x); - + if (ei_x_encode_tuple_header(&x, 5) < 0) /* B A 2 */ + goto einval; + if (ei_x_encode_atom(&x, "call") < 0) /* B 1 */ + goto einval; + if (ei_x_encode_atom(&x, mod) < 0) /* B 2 */ + goto einval; + if (ei_x_encode_atom(&x, fun) < 0) /* B 3 */ + goto einval; + if (ei_x_append_buf(&x, buf, len) < 0) /* B 4 */ + goto einval; + if (ei_x_encode_atom(&x, "user") < 0) /* B 5 */ + goto einval; + + err = ei_send_reg_encoded(fd, self, "rex", x.buff, x.index); + if (err) + goto error; + + ei_x_free(&x); + return 0; + +einval: + EI_CONN_SAVE_ERRNO__(EINVAL); + +error: + if (x.buff != NULL) + ei_x_free(&x); + return err; } /* rpc_to */ /* @@ -1640,13 +1659,14 @@ int ei_rpc(ei_cnode* ec, int fd, char *mod, char *fun, char rex[MAXATOMLEN]; if (ei_rpc_to(ec, fd, mod, fun, inbuf, inbuflen) < 0) { - return -1; + return ERL_ERROR; } - /* FIXME are we not to reply to the tick? */ + + /* ei_rpc_from() responds with a tick if it gets one... */ while ((i = ei_rpc_from(ec, fd, ERL_NO_TIMEOUT, &msg, x)) == ERL_TICK) ; - if (i == ERL_ERROR) return -1; + if (i == ERL_ERROR) return i; /*ep = 'erl'_element(2,emsg.msg);*/ /* {RPC_Tag, RPC_Reply} */ index = 0; if (ei_decode_version(x->buff, &index, &i) < 0 diff --git a/lib/erl_interface/src/connect/eirecv.c b/lib/erl_interface/src/connect/eirecv.c index 0673d25d38..96732db5b6 100644 --- a/lib/erl_interface/src/connect/eirecv.c +++ b/lib/erl_interface/src/connect/eirecv.c @@ -70,7 +70,7 @@ ei_recv_internal (int fd, err = EI_GET_CBS_CTX__(&cbs, &ctx, fd); if (err) { EI_CONN_SAVE_ERRNO__(err); - return -1; + return ERL_ERROR; } /* get length field */ @@ -80,7 +80,7 @@ ei_recv_internal (int fd, err = EIO; if (err) { EI_CONN_SAVE_ERRNO__(err); - return -1; + return ERL_ERROR; } len = get32be(s); @@ -91,7 +91,7 @@ ei_recv_internal (int fd, ssize_t wlen = sizeof(tock); ei_write_fill_ctx_t__(cbs, ctx, tock, &wlen, tmo); /* Failure no problem */ *msglenp = 0; - return 0; /* maybe flag ERL_EAGAIN [sverkerw] */ + return ERL_TICK; } /* turn off tracing on each receive. it will be turned back on if @@ -106,7 +106,7 @@ ei_recv_internal (int fd, err = EIO; if (err) { EI_CONN_SAVE_ERRNO__(err); - return -1; + return ERL_ERROR; } /* now decode header */ @@ -119,8 +119,8 @@ ei_recv_internal (int fd, || ei_decode_tuple_header(header,&index,&arity) || ei_decode_long(header,&index,&msg->msgtype)) { - erl_errno = EIO; /* Maybe another code for decoding errors */ - return -1; + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; } switch (msg->msgtype) { @@ -129,8 +129,8 @@ ei_recv_internal (int fd, if (ei_decode_atom_as(header,&index,msg->cookie,sizeof(msg->cookie),ERLANG_UTF8,NULL,NULL) || ei_decode_pid(header,&index,&msg->to)) { - erl_errno = EIO; - return -1; + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; } break; @@ -141,8 +141,8 @@ ei_recv_internal (int fd, || ei_decode_atom_as(header,&index,msg->cookie,sizeof(msg->cookie),ERLANG_UTF8,NULL,NULL) || ei_decode_atom_as(header,&index,msg->toname,sizeof(msg->toname),ERLANG_UTF8,NULL,NULL)) { - erl_errno = EIO; - return -1; + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; } /* actual message is remaining part of headerbuf, plus any unread bytes */ @@ -155,8 +155,8 @@ ei_recv_internal (int fd, if (ei_decode_pid(header,&index,&msg->from) || ei_decode_pid(header,&index,&msg->to)) { - erl_errno = EIO; - return -1; + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; } break; @@ -167,8 +167,8 @@ ei_recv_internal (int fd, if (ei_decode_pid(header,&index,&msg->from) || ei_decode_pid(header,&index,&msg->to)) { - erl_errno = EIO; - return -1; + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; } break; @@ -179,8 +179,8 @@ ei_recv_internal (int fd, || ei_decode_pid(header,&index,&msg->to) || ei_decode_trace(header,&index,&msg->token)) { - erl_errno = EIO; - return -1; + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; } ei_trace(1,&msg->token); /* turn on tracing */ @@ -193,8 +193,8 @@ ei_recv_internal (int fd, || ei_decode_atom_as(header,&index,msg->toname,sizeof(msg->toname),ERLANG_UTF8,NULL,NULL) || ei_decode_trace(header,&index,&msg->token)) { - erl_errno = EIO; - return -1; + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; } ei_trace(1,&msg->token); /* turn on tracing */ @@ -207,8 +207,8 @@ ei_recv_internal (int fd, || ei_decode_pid(header,&index,&msg->to) || ei_decode_trace(header,&index,&msg->token)) { - erl_errno = EIO; - return -1; + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; } ei_trace(1,&msg->token); /* turn on tracing */ @@ -235,14 +235,14 @@ ei_recv_internal (int fd, err = ei_read_fill_ctx_t__(cbs, ctx, header, &rlen, tmo); if (err) { EI_CONN_SAVE_ERRNO__(err); - return -1; + return ERL_ERROR; } if (rlen == 0) break; remain -= rlen; } erl_errno = EMSGSIZE; - return -1; + return ERL_ERROR; } else { /* Dynamic buffer --- grow it. */ @@ -253,7 +253,7 @@ ei_recv_internal (int fd, if ((mbuf = realloc(*mbufp, msglen)) == NULL) { erl_errno = ENOMEM; - return -1; + return ERL_ERROR; } *mbufp = mbuf; @@ -276,7 +276,7 @@ ei_recv_internal (int fd, if (err) { *msglenp = bytesread-index+1; /* actual bytes in users buffer */ EI_CONN_SAVE_ERRNO__(err); - return -1; + return ERL_ERROR; } } diff --git a/lib/erl_interface/src/connect/send.c b/lib/erl_interface/src/connect/send.c index f9337187ab..df2e3e023d 100644 --- a/lib/erl_interface/src/connect/send.c +++ b/lib/erl_interface/src/connect/send.c @@ -67,19 +67,31 @@ int ei_send_encoded_tmo(int fd, const erlang_pid *to, /* check that he can receive trace tokens first */ if (ei_distversion(fd) > 0) token = ei_trace(0,NULL); + EI_CONN_SAVE_ERRNO__(EINVAL); + /* header = SEND, cookie, to max sizes: */ - ei_encode_version(header,&index); /* 1 */ + if (ei_encode_version(header,&index) < 0) /* 1 */ + return ERL_ERROR; if (token) { - ei_encode_tuple_header(header,&index,4); /* 2 */ - ei_encode_long(header,&index,ERL_SEND_TT); /* 2 */ + if (ei_encode_tuple_header(header,&index,4) < 0) /* 2 */ + return ERL_ERROR; + if (ei_encode_long(header,&index,ERL_SEND_TT) < 0) /* 2 */ + return ERL_ERROR; } else { - ei_encode_tuple_header(header,&index,3); - ei_encode_long(header,&index,ERL_SEND); + if (ei_encode_tuple_header(header,&index,3) < 0) + return ERL_ERROR; + if (ei_encode_long(header,&index,ERL_SEND) < 0) + return ERL_ERROR; } - ei_encode_atom(header,&index,ei_getfdcookie(fd)); /* 258 */ - ei_encode_pid(header,&index,to); /* 268 */ + if (ei_encode_atom(header,&index,ei_getfdcookie(fd)) < 0) /* 258 */ + return ERL_ERROR; + if (ei_encode_pid(header,&index,to) < 0) /* 268 */ + return ERL_ERROR; - if (token) ei_encode_trace(header,&index,token); /* 534 */ + if (token) { + if (ei_encode_trace(header,&index,token) < 0) /* 534 */ + return ERL_ERROR; + } /* control message (precedes header actually) */ /* length = 1 ('p') + header len + message len */ @@ -107,9 +119,11 @@ int ei_send_encoded_tmo(int fd, const erlang_pid *to, err = EIO; if (err) { EI_CONN_SAVE_ERRNO__(err); - return -1; + return ERL_ERROR; } + erl_errno = 0; + return 0; } #endif /* EI_HAVE_STRUCT_IOVEC__ */ @@ -121,7 +135,7 @@ int ei_send_encoded_tmo(int fd, const erlang_pid *to, err = EIO; if (err) { EI_CONN_SAVE_ERRNO__(err); - return -1; + return ERL_ERROR; } len = tot_len = (ssize_t) msglen; @@ -130,9 +144,10 @@ int ei_send_encoded_tmo(int fd, const erlang_pid *to, err = EIO; if (err) { EI_CONN_SAVE_ERRNO__(err); - return -1; + return ERL_ERROR; } + erl_errno = 0; return 0; } diff --git a/lib/erl_interface/src/connect/send_exit.c b/lib/erl_interface/src/connect/send_exit.c index af0fb2af88..231aad0ae0 100644 --- a/lib/erl_interface/src/connect/send_exit.c +++ b/lib/erl_interface/src/connect/send_exit.c @@ -67,9 +67,12 @@ int ei_send_exit_tmo(int fd, const erlang_pid *from, const erlang_pid *to, return ERL_ERROR; } - if (len > EISMALLBUF) - if (!(dbuf = malloc(len))) - return -1; + if (len > EISMALLBUF) { + if (!(dbuf = malloc(len))) { + EI_CONN_SAVE_ERRNO__(ENOMEM); + return ERL_ERROR; + } + } msgbuf = (dbuf ? dbuf : sbuf); @@ -77,31 +80,46 @@ int ei_send_exit_tmo(int fd, const erlang_pid *from, const erlang_pid *to, /* check that he can receive trace tokens first */ if (ei_distversion(fd) > 0) token = ei_trace(0,NULL); + EI_CONN_SAVE_ERRNO__(EINVAL); + index = 5; /* max sizes: */ - ei_encode_version(msgbuf,&index); /* 1 */ + if (ei_encode_version(msgbuf,&index) < 0) /* 1 */ + return ERL_ERROR; if (token) { - ei_encode_tuple_header(msgbuf,&index,5); /* 2 */ - ei_encode_long(msgbuf,&index,ERL_EXIT_TT); /* 2 */ + if (ei_encode_tuple_header(msgbuf,&index,5) < 0) /* 2 */ + return ERL_ERROR; + if (ei_encode_long(msgbuf,&index,ERL_EXIT_TT) < 0)/* 2 */ + return ERL_ERROR; } else { - ei_encode_tuple_header(msgbuf,&index,4); - ei_encode_long(msgbuf,&index,ERL_EXIT); + if (ei_encode_tuple_header(msgbuf,&index,4) < 0) + return ERL_ERROR; + if (ei_encode_long(msgbuf,&index,ERL_EXIT) < 0) + return ERL_ERROR; } - ei_encode_pid(msgbuf,&index,from); /* 268 */ - ei_encode_pid(msgbuf,&index,to); /* 268 */ + if (ei_encode_pid(msgbuf,&index,from) < 0) /* 268 */ + return ERL_ERROR; + if (ei_encode_pid(msgbuf,&index,to) < 0) /* 268 */ + return ERL_ERROR; - if (token) ei_encode_trace(msgbuf,&index,token); /* 534 */ + if (token) { + if (ei_encode_trace(msgbuf,&index,token) < 0) /* 534 */ + return ERL_ERROR; + } /* Reason */ - ei_encode_string(msgbuf,&index,reason); /* len */ + if (ei_encode_string(msgbuf,&index,reason) < 0) /* len */ + return ERL_ERROR; /* 5 byte header missing */ s = msgbuf; put32be(s, index - 4); /* 4 */ - put8(s, ERL_PASS_THROUGH); /* 1 */ + put8(s, ERL_PASS_THROUGH); /* 1 */ /*** sum: len + 1080 */ - if (ei_tracelevel >= 4) - ei_show_sendmsg(stderr,msgbuf,NULL); + if (ei_tracelevel >= 4) { + if (ei_show_sendmsg(stderr,msgbuf,NULL) < 0) + return ERL_ERROR; + } wlen = (ssize_t) index; err = ei_write_fill_ctx_t__(cbs, ctx, msgbuf, &wlen, tmo); @@ -113,6 +131,8 @@ int ei_send_exit_tmo(int fd, const erlang_pid *from, const erlang_pid *to, EI_CONN_SAVE_ERRNO__(err); return ERL_ERROR; } + + erl_errno = 0; return 0; } diff --git a/lib/erl_interface/src/connect/send_reg.c b/lib/erl_interface/src/connect/send_reg.c index b5c9e6a6f8..1d9c85f210 100644 --- a/lib/erl_interface/src/connect/send_reg.c +++ b/lib/erl_interface/src/connect/send_reg.c @@ -64,21 +64,33 @@ int ei_send_reg_encoded_tmo(int fd, const erlang_pid *from, if (ei_distversion(fd) > 0) token = ei_trace(0,NULL); + EI_CONN_SAVE_ERRNO__(EINVAL); + /* header = REG_SEND, from, cookie, toname max sizes: */ - ei_encode_version(header,&index); /* 1 */ + if (ei_encode_version(header,&index) < 0) /* 1 */ + return ERL_ERROR; if (token) { - ei_encode_tuple_header(header,&index,5); /* 2 */ - ei_encode_long(header,&index,ERL_REG_SEND_TT); /* 2 */ + if (ei_encode_tuple_header(header,&index,5) < 0) /* 2 */ + return ERL_ERROR; + if (ei_encode_long(header,&index,ERL_REG_SEND_TT) < 0) /* 2 */ + return ERL_ERROR; } else { - ei_encode_tuple_header(header,&index,4); - ei_encode_long(header,&index,ERL_REG_SEND); + if (ei_encode_tuple_header(header,&index,4) < 0) + return ERL_ERROR; + if (ei_encode_long(header,&index,ERL_REG_SEND) < 0) + return ERL_ERROR; } - ei_encode_pid(header, &index, from); /* 268 */ - ei_encode_atom(header, &index, ei_getfdcookie(fd)); /* 258 */ - ei_encode_atom(header, &index, to); /* 268 */ - - if (token) ei_encode_trace(header,&index,token); /* 534 */ + if (ei_encode_pid(header, &index, from) < 0) /* 268 */ + return ERL_ERROR; + if (ei_encode_atom(header, &index, ei_getfdcookie(fd)) < 0) /* 258 */ + return ERL_ERROR; + if (ei_encode_atom(header, &index, to) < 0) /* 268 */ + return ERL_ERROR; + if (token) { + if (ei_encode_trace(header,&index,token) < 0) /* 534 */ + return ERL_ERROR; + } /* control message (precedes header actually) */ /* length = 1 ('p') + header len + message len */ s = header; @@ -103,8 +115,11 @@ int ei_send_reg_encoded_tmo(int fd, const erlang_pid *from, err = EIO; if (err) { EI_CONN_SAVE_ERRNO__(err); - return -1; + return ERL_ERROR; } + + erl_errno = 0; + return 0; } #endif /* EI_HAVE_STRUCT_IOVEC__ */ @@ -116,7 +131,7 @@ int ei_send_reg_encoded_tmo(int fd, const erlang_pid *from, err = EIO; if (err) { EI_CONN_SAVE_ERRNO__(err); - return -1; + return ERL_ERROR; } len = tot_len = (ssize_t) msglen; @@ -125,8 +140,10 @@ int ei_send_reg_encoded_tmo(int fd, const erlang_pid *from, err = EIO; if (err) { EI_CONN_SAVE_ERRNO__(err); - return -1; + return ERL_ERROR; } + + erl_errno = 0; return 0; } diff --git a/lib/erl_interface/src/encode/encode_trace.c b/lib/erl_interface/src/encode/encode_trace.c index d0e6aec474..4c51e3d84b 100644 --- a/lib/erl_interface/src/encode/encode_trace.c +++ b/lib/erl_interface/src/encode/encode_trace.c @@ -23,12 +23,18 @@ int ei_encode_trace(char *buf, int *index, const erlang_trace *p) { /* { Flags, Label, Serial, FromPid, Prev } */ - ei_encode_tuple_header(buf,index,5); - ei_encode_long(buf,index,p->flags); - ei_encode_long(buf,index,p->label); - ei_encode_long(buf,index,p->serial); - ei_encode_pid(buf,index,&p->from); - ei_encode_long(buf,index,p->prev); + if (ei_encode_tuple_header(buf,index,5) < 0) + return -1; + if (ei_encode_long(buf,index,p->flags) < 0) + return -1; + if (ei_encode_long(buf,index,p->label) < 0) + return -1; + if (ei_encode_long(buf,index,p->serial) < 0) + return -1; + if (ei_encode_pid(buf,index,&p->from) < 0) + return -1; + if (ei_encode_long(buf,index,p->prev) < 0) + return -1; /* index is updated by the functions we called */ diff --git a/lib/erl_interface/src/global/global_names.c b/lib/erl_interface/src/global/global_names.c index bcbe740223..028e872916 100644 --- a/lib/erl_interface/src/global/global_names.c +++ b/lib/erl_interface/src/global/global_names.c @@ -26,6 +26,7 @@ #include "ei_connect_int.h" #include "ei.h" #include "ei_connect.h" +#include "ei_internal.h" #define GLOBALNAMEBUF (16*1024) /* not very small actually */ @@ -53,15 +54,18 @@ char **ei_global_names(ei_cnode *ec, int fd, int *count) char *s; self->num = fd; - ei_encode_version(buf,&index); - ei_encode_tuple_header(buf,&index,2); - ei_encode_pid(buf,&index,self); /* PidFrom */ - ei_encode_tuple_header(buf,&index,5); - ei_encode_atom(buf,&index,"call"); /* call */ - ei_encode_atom(buf,&index,"global"); /* Mod */ - ei_encode_atom(buf,&index,"registered_names"); /* Fun */ - ei_encode_list_header(buf,&index,0); /* Args: [ ] */ - ei_encode_atom(buf,&index,"user"); /* user */ + if (ei_encode_version(buf,&index) + || ei_encode_tuple_header(buf,&index,2) + || ei_encode_pid(buf,&index,self) /* PidFrom */ + || ei_encode_tuple_header(buf,&index,5) + || ei_encode_atom(buf,&index,"call") /* call */ + || ei_encode_atom(buf,&index,"global") /* Mod */ + || ei_encode_atom(buf,&index,"registered_names")/* Fun */ + || ei_encode_list_header(buf,&index,0) /* Args: [ ] */ + || ei_encode_atom(buf,&index,"user")) { /* user */ + EI_CONN_SAVE_ERRNO__(EINVAL); + return NULL; + } /* make the rpc call */ if (ei_send_reg_encoded(fd,self,"rex",buf,index)) return NULL; @@ -72,7 +76,10 @@ char **ei_global_names(ei_cnode *ec, int fd, int *count) else break; } - if (i != ERL_SEND) return NULL; + if (i != ERL_SEND) { + EI_CONN_SAVE_ERRNO__(EBADMSG); + return NULL; + } /* expecting { rex, [name1, name2, ...] } */ size = msglen; @@ -83,7 +90,10 @@ char **ei_global_names(ei_cnode *ec, int fd, int *count) || (arity != 2) || ei_decode_atom(buf,&index,tmpbuf) || strcmp(tmpbuf,"rex") - || ei_decode_list_header(buf,&index,&arity)) return NULL; + || ei_decode_list_header(buf,&index,&arity)) { + EI_CONN_SAVE_ERRNO__(EBADMSG); + return NULL; + } /* we use the size of the rest of the received message to estimate @@ -92,7 +102,10 @@ char **ei_global_names(ei_cnode *ec, int fd, int *count) * a little less than the atoms themselves needed in the reply. */ arity++; /* we will need a terminating NULL as well */ - if (!(names = malloc((arity * sizeof(char**)) + (size-index)))) return NULL; + if (!(names = malloc((arity * sizeof(char**)) + (size-index)))) { + EI_CONN_SAVE_ERRNO__(ENOMEM); + return NULL; + } /* arity pointers first, followed by s */ s = (char *)(names+arity); diff --git a/lib/erl_interface/src/global/global_register.c b/lib/erl_interface/src/global/global_register.c index c260ce091c..dbdab8dbc5 100644 --- a/lib/erl_interface/src/global/global_register.c +++ b/lib/erl_interface/src/global/global_register.c @@ -23,6 +23,7 @@ #include "eisend.h" #include "eirecv.h" #include "ei.h" +#include "ei_internal.h" int ei_global_register(int fd, const char *name, erlang_pid *self) { @@ -40,24 +41,27 @@ int ei_global_register(int fd, const char *name, erlang_pid *self) /* set up rpc arguments */ /* { PidFrom, { call, Mod, Fun, Args, user }} */ index = 0; - ei_encode_version(buf,&index); - ei_encode_tuple_header(buf,&index,2); - ei_encode_pid(buf,&index,self); /* PidFrom */ - ei_encode_tuple_header(buf,&index,5); - ei_encode_atom(buf,&index,"call"); /* call */ - ei_encode_atom(buf,&index,"global"); /* Mod */ - ei_encode_atom(buf,&index,"register_name_external"); /* Fun */ - ei_encode_list_header(buf,&index,3); /* Args: [ name, self(), cnode ] */ - ei_encode_atom(buf,&index,name); - ei_encode_pid(buf,&index,self); - ei_encode_tuple_header(buf,&index,2); - ei_encode_atom(buf,&index,"global"); /* special "resolve" treatment */ - ei_encode_atom(buf,&index,"cnode"); /* i.e. we get a SEND when conflict */ - ei_encode_empty_list(buf,&index); - ei_encode_atom(buf,&index,"user"); /* user */ + if (ei_encode_version(buf,&index) + || ei_encode_tuple_header(buf,&index,2) + || ei_encode_pid(buf,&index,self) /* PidFrom */ + || ei_encode_tuple_header(buf,&index,5) + || ei_encode_atom(buf,&index,"call") /* call */ + || ei_encode_atom(buf,&index,"global") /* Mod */ + || ei_encode_atom(buf,&index,"register_name_external")/* Fun */ + || ei_encode_list_header(buf,&index,3) /* Args: [ name, self(), cnode ] */ + || ei_encode_atom(buf,&index,name) + || ei_encode_pid(buf,&index,self) + || ei_encode_tuple_header(buf,&index,2) + || ei_encode_atom(buf,&index,"global") /* special "resolve" treatment */ + || ei_encode_atom(buf,&index,"cnode") /* i.e. we get a SEND when conflict */ + || ei_encode_empty_list(buf,&index) + || ei_encode_atom(buf,&index,"user")) { /* user */ + EI_CONN_SAVE_ERRNO__(EINVAL); + return ERL_ERROR; + } /* make the rpc call */ - if (ei_send_reg_encoded(fd,self,"rex",buf,index)) return -1; + if (ei_send_reg_encoded(fd,self,"rex",buf,index)) return ERL_ERROR; /* get the reply: expect link and an atom, or just an atom */ needlink = needatom = needmonitor = 1; @@ -72,19 +76,28 @@ int ei_global_register(int fd, const char *name, erlang_pid *self) switch (i) { case ERL_LINK: /* got link */ - if (!needlink) return -1; + if (!needlink) { + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; + } needlink = 0; break; case ERL_MONITOR_P-10: /* got monitor */ - if (!needmonitor) { return -1;} + if (!needmonitor) { + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; + } needmonitor = 0; break; case ERL_SEND: /* got message - does it contain our atom? */ - if (!needatom) return -1; + if (!needatom) { + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; + } else { /* expecting { rex, yes } */ index = 0; @@ -94,8 +107,10 @@ int ei_global_register(int fd, const char *name, erlang_pid *self) || ei_decode_atom(buf,&index,tmpbuf) || strcmp(tmpbuf,"rex") || ei_decode_atom(buf,&index,tmpbuf) - || strcmp(tmpbuf,"yes")) - return -1; /* bad response from other side */ + || strcmp(tmpbuf,"yes")) { + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; /* bad response from other side */ + } /* we're done */ return 0; @@ -103,7 +118,8 @@ int ei_global_register(int fd, const char *name, erlang_pid *self) break; default: - return -1; /* something else */ + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; /* something else */ } } return 0; diff --git a/lib/erl_interface/src/global/global_unregister.c b/lib/erl_interface/src/global/global_unregister.c index ee785a2abd..35e242b13f 100644 --- a/lib/erl_interface/src/global/global_unregister.c +++ b/lib/erl_interface/src/global/global_unregister.c @@ -25,6 +25,7 @@ #include "ei_connect_int.h" #include "ei_connect.h" #include "ei.h" +#include "ei_internal.h" /* remove the association between name and its pid */ /* global:unregister_name(name) -> ok */ @@ -42,20 +43,23 @@ int ei_global_unregister(ei_cnode *ec, int fd, const char *name) /* make a self pid */ self->num = fd; - ei_encode_version(buf,&index); - ei_encode_tuple_header(buf,&index,2); - ei_encode_pid(buf,&index,self); /* PidFrom */ - ei_encode_tuple_header(buf,&index,5); - ei_encode_atom(buf,&index,"call"); /* call */ - ei_encode_atom(buf,&index,"global"); /* Mod */ - ei_encode_atom(buf,&index,"unregister_name_external"); /* Fun */ - ei_encode_list_header(buf,&index,1); /* Args: [ name ] */ - ei_encode_atom(buf,&index,name); - ei_encode_empty_list(buf,&index); - ei_encode_atom(buf,&index,"user"); /* user */ + if (ei_encode_version(buf,&index) + || ei_encode_tuple_header(buf,&index,2) + || ei_encode_pid(buf,&index,self) /* PidFrom */ + || ei_encode_tuple_header(buf,&index,5) + || ei_encode_atom(buf,&index,"call") /* call */ + || ei_encode_atom(buf,&index,"global") /* Mod */ + || ei_encode_atom(buf,&index,"unregister_name_external") /* Fun */ + || ei_encode_list_header(buf,&index,1) /* Args: [ name ] */ + || ei_encode_atom(buf,&index,name) + || ei_encode_empty_list(buf,&index) + || ei_encode_atom(buf,&index,"user")) { /* user */ + EI_CONN_SAVE_ERRNO__(EINVAL); + return ERL_ERROR; + } /* make the rpc call */ - if (ei_send_reg_encoded(fd,self,"rex",buf,index)) return -1; + if (ei_send_reg_encoded(fd,self,"rex",buf,index)) return ERL_ERROR; /* get the reply: expect unlink and an atom, or just an atom */ needunlink = needatom = needdemonitor = 1; @@ -70,19 +74,28 @@ int ei_global_unregister(ei_cnode *ec, int fd, const char *name) switch (i) { case ERL_UNLINK: /* got unlink */ - if (!needunlink) return -1; + if (!needunlink) { + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; + } needunlink = 0; break; case ERL_DEMONITOR_P-10: /* got demonitor */ - if (!needdemonitor) return -1; + if (!needdemonitor) { + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; + } needdemonitor = 0; break; case ERL_SEND: /* got message - does it contain our atom? */ - if (!needatom) return -1; + if (!needatom) { + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; + } else { /* expecting { rex, ok } */ index = 0; @@ -92,8 +105,10 @@ int ei_global_unregister(ei_cnode *ec, int fd, const char *name) || ei_decode_atom(buf,&index,tmpbuf) || strcmp(tmpbuf,"rex") || ei_decode_atom(buf,&index,tmpbuf) - || strcmp(tmpbuf,"ok")) - return -1; /* bad response from other side */ + || strcmp(tmpbuf,"ok")) { + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; /* bad response from other side */ + } /* we're done here */ return 0; @@ -101,7 +116,8 @@ int ei_global_unregister(ei_cnode *ec, int fd, const char *name) break; default: - return -1; + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; } } diff --git a/lib/erl_interface/src/global/global_whereis.c b/lib/erl_interface/src/global/global_whereis.c index afedc98030..1af5626f4f 100644 --- a/lib/erl_interface/src/global/global_whereis.c +++ b/lib/erl_interface/src/global/global_whereis.c @@ -26,6 +26,7 @@ #include "ei_connect_int.h" #include "ei.h" #include "ei_connect.h" +#include "ei_internal.h" /* return the ETERM pid corresponding to name. If caller * provides non-NULL node, nodename will be returned there @@ -46,20 +47,22 @@ int ei_global_whereis(ei_cnode *ec, int fd, const char *name, erlang_pid* pid, c self->num = fd; /* FIXME looks strange to change something?! */ - ei_encode_version(buf,&index); - ei_encode_tuple_header(buf,&index,2); - ei_encode_pid(buf,&index,self); /* PidFrom */ - ei_encode_tuple_header(buf,&index,5); - ei_encode_atom(buf,&index,"call"); /* call */ - ei_encode_atom(buf,&index,"global"); /* Mod */ - ei_encode_atom(buf,&index,"whereis_name"); /* Fun */ - ei_encode_list_header(buf,&index,1); /* Args: [ name ] */ - ei_encode_atom(buf,&index,name); - ei_encode_empty_list(buf,&index); - ei_encode_atom(buf,&index,"user"); /* user */ - + if (ei_encode_version(buf,&index) + || ei_encode_tuple_header(buf,&index,2) + || ei_encode_pid(buf,&index,self) /* PidFrom */ + || ei_encode_tuple_header(buf,&index,5) + || ei_encode_atom(buf,&index,"call") /* call */ + || ei_encode_atom(buf,&index,"global") /* Mod */ + || ei_encode_atom(buf,&index,"whereis_name") /* Fun */ + || ei_encode_list_header(buf,&index,1) /* Args: [ name ] */ + || ei_encode_atom(buf,&index,name) + || ei_encode_empty_list(buf,&index) + || ei_encode_atom(buf,&index,"user")) { /* user */ + EI_CONN_SAVE_ERRNO__(EINVAL); + return ERL_ERROR; + } /* make the rpc call */ - if (ei_send_reg_encoded(fd,self,"rex",buf,index)) return -1; + if (ei_send_reg_encoded(fd,self,"rex",buf,index)) return ERL_ERROR; while (1) { index = EISMALLBUF; @@ -67,7 +70,7 @@ int ei_global_whereis(ei_cnode *ec, int fd, const char *name, erlang_pid* pid, c else break; } - if (i != ERL_SEND) return -1; + if (i != ERL_SEND) return ERL_ERROR; /* expecting { rex, pid } */ index = 0; @@ -76,8 +79,10 @@ int ei_global_whereis(ei_cnode *ec, int fd, const char *name, erlang_pid* pid, c || (arity != 2) || ei_decode_atom(buf,&index,tmpbuf) || strcmp(tmpbuf,"rex") - || ei_decode_pid(buf,&index,&epid)) - return -1; /* bad response from other side */ + || ei_decode_pid(buf,&index,&epid)) { + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; /* bad response from other side */ + } /* extract the nodename for the caller */ if (node) { @@ -86,7 +91,8 @@ int ei_global_whereis(ei_cnode *ec, int fd, const char *name, erlang_pid* pid, c strcpy(node, node_str); } else { - return -1; + EI_CONN_SAVE_ERRNO__(EBADMSG); + return ERL_ERROR; } } *pid = epid; diff --git a/lib/erl_interface/src/misc/show_msg.c b/lib/erl_interface/src/misc/show_msg.c index 518d9dd595..c87e73c545 100644 --- a/lib/erl_interface/src/misc/show_msg.c +++ b/lib/erl_interface/src/misc/show_msg.c @@ -129,9 +129,9 @@ int ei_show_sendmsg(FILE *stream, const char *header, const char *msgbuf) /* skip five bytes */ index = 5; - ei_decode_version(header,&index,&version); - ei_decode_tuple_header(header,&index,&arity); - ei_decode_long(header,&index,&msg.msgtype); + if (ei_decode_version(header,&index,&version) + || ei_decode_tuple_header(header,&index,&arity) + || ei_decode_long(header,&index,&msg.msgtype)) return -1; switch (msg.msgtype) { case ERL_SEND: |