diff options
author | John Högberg <[email protected]> | 2018-04-24 13:30:00 +0200 |
---|---|---|
committer | John Högberg <[email protected]> | 2018-04-24 14:20:06 +0200 |
commit | 05b1c13e36ec49891e89593b25e668e983a4fc41 (patch) | |
tree | c5c8474cfbf9516fe1818bbfb66757057bdfcb9b | |
parent | 74a95b3d511177a9b35c2b0272b9ca5511b6f750 (diff) | |
download | otp-05b1c13e36ec49891e89593b25e668e983a4fc41.tar.gz otp-05b1c13e36ec49891e89593b25e668e983a4fc41.tar.bz2 otp-05b1c13e36ec49891e89593b25e668e983a4fc41.zip |
heart: Use ntohs instead of manual conversion
Multiplying a signed char by 256 is undefined behavior and caused
problems on some platforms when the length was long enough. We
could cast it to an unsigned int to make it work, but it's best not
to reinvent the wheel.
Fixes OTP-15034
-rw-r--r-- | erts/etc/common/heart.c | 5 | ||||
-rw-r--r-- | lib/kernel/test/heart_SUITE.erl | 13 |
2 files changed, 11 insertions, 7 deletions
diff --git a/erts/etc/common/heart.c b/erts/etc/common/heart.c index 01ef840b5d..aaaffbca79 100644 --- a/erts/etc/common/heart.c +++ b/erts/etc/common/heart.c @@ -847,11 +847,8 @@ write_message(fd, mp) int fd; struct msg *mp; { - int len; - char* tmp; + int len = ntohs(mp->len); - tmp = (char*) &(mp->len); - len = (*tmp * 256) + *(tmp+1); if ((len == 0) || (len > MSG_BODY_SIZE)) { return MSG_HDR_SIZE; } /* cc68k wants (char *) */ diff --git a/lib/kernel/test/heart_SUITE.erl b/lib/kernel/test/heart_SUITE.erl index 83efbb4c35..02954aa54c 100644 --- a/lib/kernel/test/heart_SUITE.erl +++ b/lib/kernel/test/heart_SUITE.erl @@ -352,9 +352,16 @@ clear_cmd(Config) when is_list(Config) -> get_cmd(suite) -> []; get_cmd(Config) when is_list(Config) -> {ok, Node} = start_check(slave, heart_test), - Cmd = "test", - ok = rpc:call(Node, heart, set_cmd, [Cmd]), - {ok, Cmd} = rpc:call(Node, heart, get_cmd, []), + + ShortCmd = "test", + ok = rpc:call(Node, heart, set_cmd, [ShortCmd]), + {ok, ShortCmd} = rpc:call(Node, heart, get_cmd, []), + + %% This would hang prior to OTP-15024 being fixed. + LongCmd = [$a || _ <- lists:seq(1, 160)], + ok = rpc:call(Node, heart, set_cmd, [LongCmd]), + {ok, LongCmd} = rpc:call(Node, heart, get_cmd, []), + stop_node(Node), ok. |