linux-sctp.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH lksctp-tools] func_tests: fix malloc size in test_1_to_1_connectx.c
@ 2021-04-06 15:01 Marcelo Ricardo Leitner
  2021-04-07 21:13 ` Xin Long
  0 siblings, 1 reply; 2+ messages in thread
From: Marcelo Ricardo Leitner @ 2021-04-06 15:01 UTC (permalink / raw)
  To: linux-sctp; +Cc: Marcelo Ricardo Leitner

Recent gcc started tracking this and now it's warning:
test_1_to_1_connectx.c: In function ‘main’:
test_1_to_1_connectx.c:133:17: warning: array subscript ‘struct sockaddr[0]’ is partly outside array bounds of ‘unsigned char[15]’ [-Warray-bounds]
  133 |         tmp_addr->sa_family = AF_INET;
      |                 ^~
test_1_to_1_connectx.c:132:40: note: referencing an object of size 15 allocated by ‘malloc’
  132 |         tmp_addr = (struct sockaddr *) malloc(sizeof(struct sockaddr) - 1);
      |                                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It gets parsed by __connectx_addrsize using each family struct, but they
aligned to sockaddr size. So lets fix it here by allocating the right
size.

Signed-off-by: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
---
 src/func_tests/test_1_to_1_connectx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/func_tests/test_1_to_1_connectx.c b/src/func_tests/test_1_to_1_connectx.c
index 3cb18b3aad421720e8598e9a0bbab2de987d4467..9adab2352c09d0fb0087e4c4b43c6d7d7b60de0d 100644
--- a/src/func_tests/test_1_to_1_connectx.c
+++ b/src/func_tests/test_1_to_1_connectx.c
@@ -129,7 +129,7 @@ main(int argc, char *argv[])
 	tst_resm(TPASS, "sctp_connectx() with invalid socket - ENOTSOCK");
 
 	/*sctp_connectx () TEST3: Invalid address, EINVAL Expected error*/
-	tmp_addr = (struct sockaddr *) malloc(sizeof(struct sockaddr) - 1);
+	tmp_addr = (struct sockaddr *) malloc(sizeof(struct sockaddr));
 	tmp_addr->sa_family = AF_INET;
 	error = sctp_connectx(sk, tmp_addr, 1, NULL);
 	if (error != -1 || errno != EINVAL)
-- 
2.30.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH lksctp-tools] func_tests: fix malloc size in test_1_to_1_connectx.c
  2021-04-06 15:01 [PATCH lksctp-tools] func_tests: fix malloc size in test_1_to_1_connectx.c Marcelo Ricardo Leitner
@ 2021-04-07 21:13 ` Xin Long
  0 siblings, 0 replies; 2+ messages in thread
From: Xin Long @ 2021-04-07 21:13 UTC (permalink / raw)
  To: Marcelo Ricardo Leitner; +Cc: linux-sctp @ vger . kernel . org

On Wed, Apr 7, 2021 at 1:35 AM Marcelo Ricardo Leitner
<marcelo.leitner@gmail.com> wrote:
>
> Recent gcc started tracking this and now it's warning:
> test_1_to_1_connectx.c: In function ‘main’:
> test_1_to_1_connectx.c:133:17: warning: array subscript ‘struct sockaddr[0]’ is partly outside array bounds of ‘unsigned char[15]’ [-Warray-bounds]
>   133 |         tmp_addr->sa_family = AF_INET;
>       |                 ^~
> test_1_to_1_connectx.c:132:40: note: referencing an object of size 15 allocated by ‘malloc’
>   132 |         tmp_addr = (struct sockaddr *) malloc(sizeof(struct sockaddr) - 1);
>       |                                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> It gets parsed by __connectx_addrsize using each family struct, but they
> aligned to sockaddr size. So lets fix it here by allocating the right
> size.
>
> Signed-off-by: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
> ---
>  src/func_tests/test_1_to_1_connectx.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/src/func_tests/test_1_to_1_connectx.c b/src/func_tests/test_1_to_1_connectx.c
> index 3cb18b3aad421720e8598e9a0bbab2de987d4467..9adab2352c09d0fb0087e4c4b43c6d7d7b60de0d 100644
> --- a/src/func_tests/test_1_to_1_connectx.c
> +++ b/src/func_tests/test_1_to_1_connectx.c
> @@ -129,7 +129,7 @@ main(int argc, char *argv[])
>         tst_resm(TPASS, "sctp_connectx() with invalid socket - ENOTSOCK");
>
>         /*sctp_connectx () TEST3: Invalid address, EINVAL Expected error*/
> -       tmp_addr = (struct sockaddr *) malloc(sizeof(struct sockaddr) - 1);
> +       tmp_addr = (struct sockaddr *) malloc(sizeof(struct sockaddr));
>         tmp_addr->sa_family = AF_INET;
>         error = sctp_connectx(sk, tmp_addr, 1, NULL);
>         if (error != -1 || errno != EINVAL)
> --
> 2.30.2
>
Reviewed-by: Xin Long <lucien.xin@gmail.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-04-07 21:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-06 15:01 [PATCH lksctp-tools] func_tests: fix malloc size in test_1_to_1_connectx.c Marcelo Ricardo Leitner
2021-04-07 21:13 ` Xin Long

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).