[erlang-patches] [PATCH] heart: remove garbage appended to heart command

Michael Santos <>
Wed Apr 6 02:45:17 CEST 2011


heart:get_cmd/0 is documented to return an empty string if the command is
cleared. get_cmd/0 returns 2 extra bytes: 1 byte for the trailing null,
1 byte from the op (the op is an unsigned char and 2 bytes are allocated
for it in the returned buffer).

1> heart:get_cmd().
{ok,[0,0]}
2> heart:set_cmd("echo hello").
ok
3> heart:get_cmd().
{ok,[101,99,104,111,32,104,101,108,108,111,0,0]}
4> heart:clear_cmd().
ok
5> heart:get_cmd().
{ok,[0,99]}
---
 erts/etc/common/heart.c         |    6 +++---
 lib/kernel/test/heart_SUITE.erl |   13 +++++++++++--
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/erts/etc/common/heart.c b/erts/etc/common/heart.c
index 778b356..7a5746e 100644
--- a/erts/etc/common/heart.c
+++ b/erts/etc/common/heart.c
@@ -727,16 +727,16 @@ static int
 heart_cmd_reply(int fd, char *s)
 {
   struct msg m;
-  int len = strlen(s) + 1;	/* Include \0 */
+  int len = strlen(s);
 
   /* if s >= MSG_BODY_SIZE, return a write
    * failure immediately.
    */
-  if (len > sizeof(m.fill))
+  if (len >= sizeof(m.fill))
       return -1;
 
   m.op = HEART_CMD;
-  m.len = htons(len + 2);	/* Include Op */
+  m.len = htons(len + 1);	/* Include Op */
   strcpy((char*)m.fill, s);
 
   return write_message(fd, &m);
diff --git a/lib/kernel/test/heart_SUITE.erl b/lib/kernel/test/heart_SUITE.erl
index 043c753..233e438 100644
--- a/lib/kernel/test/heart_SUITE.erl
+++ b/lib/kernel/test/heart_SUITE.erl
@@ -22,7 +22,7 @@
 
 -export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 
 	 init_per_group/2,end_per_group/2, start/1, restart/1, 
-	 reboot/1, set_cmd/1, clear_cmd/1,
+	 reboot/1, set_cmd/1, clear_cmd/1, get_cmd/1,
 	 dont_drop/1, kill_pid/1]).
 
 -export([init_per_testcase/2, end_per_testcase/2]).
@@ -58,7 +58,7 @@ end_per_testcase(_Func, Config) ->
 suite() -> [{ct_hooks,[ts_install_cth]}].
 
 all() -> 
-    [start, restart, reboot, set_cmd, clear_cmd, kill_pid].
+    [start, restart, reboot, set_cmd, clear_cmd, get_cmd, kill_pid].
 
 groups() -> 
     [].
@@ -246,6 +246,15 @@ clear_cmd(Config) when is_list(Config) ->
 	  end,
     ok.
 
+get_cmd(suite) -> [];
+get_cmd(Config) when is_list(Config) ->
+    ?line {ok, Node} = start_check(slave, heart_test),
+    Cmd = "test",
+    ?line ok = rpc:call(Node, heart, set_cmd, [Cmd]),
+    ?line {ok, Cmd} = rpc:call(Node, heart, get_cmd, []),
+    stop_node(Node),
+    ok.
+
 dont_drop(suite) -> 
 %%% Removed as it may crash epmd/distribution in colourful
 %%% ways. While we ARE finding out WHY, it would
-- 
1.7.0.4



More information about the erlang-patches mailing list