diff --git a/src/libAtomVM/otp_socket.c b/src/libAtomVM/otp_socket.c index ee7774a56..21cb304f6 100644 --- a/src/libAtomVM/otp_socket.c +++ b/src/libAtomVM/otp_socket.c @@ -1765,7 +1765,7 @@ static term nif_socket_recv_without_peek(Context *ctx, struct SocketResource *rs GlobalContext *global = ctx->global; size_t buffer_size = len == 0 ? rsrc_obj->buf_size : len; - char *buffer = malloc(buffer_size); + uint8_t *buffer = (uint8_t *) malloc(buffer_size); term payload = term_invalid_term(); if (IS_NULL_PTR(buffer)) { diff --git a/tests/libs/estdlib/test_tcp_socket.erl b/tests/libs/estdlib/test_tcp_socket.erl index 906bbbbc3..606fc5214 100644 --- a/tests/libs/estdlib/test_tcp_socket.erl +++ b/tests/libs/estdlib/test_tcp_socket.erl @@ -27,6 +27,7 @@ test() -> ok = test_shutdown(), ok = test_close_by_another_process(), ok = test_buf_size(), + ok = test_override_buf_size(), case get_otp_version() of atomvm -> ok = test_abandon_select(); @@ -135,16 +136,31 @@ test_buf_size() -> %% we should only be able to receive ok = check_receive(Socket, Packet, 0, <<"0123456789">>), + ok = check_receive(Socket, Packet, 0, <<"0123456789">>), + ok = check_receive(Socket, Packet, 0, <<"0123456789">>), + + ok = close_client_socket(Socket), + + close_listen_socket(ListenSocket). + +test_override_buf_size() -> + etest:flush_msg_queue(), + + Port = 44404, + ListenSocket = start_echo_server(Port), + + {ok, Socket} = socket:open(inet, stream, tcp), + ok = try_connect(Socket, Port, 10), + + %% limit the recv buffer size to 10 bytes + ok = socket:setopt(Socket, {otp, rcvbuf}, 10), - %% read the remaining 20 bytes - _ = socket:recv(Socket, 20), + Packet = "012345678901234567890123456789", %% verify that the socket:recv length parameter takes %% precedence over the default ok = check_receive(Socket, Packet, 15, <<"012345678901234">>), - - %% read the remaining 15 bytes - _ = socket:recv(Socket, 20), + ok = check_receive(Socket, Packet, 15, <<"012345678901234">>), ok = close_client_socket(Socket),