diff mbox series

[03/23] chardev/baum: Use definitions to avoid dynamic stack allocation

Message ID 20210505211047.1496765-4-philmd@redhat.com (mailing list archive)
State New, archived
Headers show
Series misc: Remove variable-length arrays on the stack | expand

Commit Message

Philippe Mathieu-Daudé May 5, 2021, 9:10 p.m. UTC
We know 'x * y' will be at most 'X_MAX * Y_MAX' (which is not
a big value, it is actually 84). Instead of having the compiler
use variable-length array, declare an array able to hold the
maximum 'x * y'.

Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 chardev/baum.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Samuel Thibault May 5, 2021, 9:14 p.m. UTC | #1
Philippe Mathieu-Daudé, le mer. 05 mai 2021 23:10:27 +0200, a ecrit:
> We know 'x * y' will be at most 'X_MAX * Y_MAX' (which is not
> a big value, it is actually 84). Instead of having the compiler
> use variable-length array, declare an array able to hold the
> maximum 'x * y'.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>

Reviewed-by: Samuel Thibault <samuel.thibault@ens-lyon.org>

> ---
>  chardev/baum.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/chardev/baum.c b/chardev/baum.c
> index adc3d7b3b56..0822e9ed5f3 100644
> --- a/chardev/baum.c
> +++ b/chardev/baum.c
> @@ -383,9 +383,9 @@ static int baum_eat_packet(BaumChardev *baum, const uint8_t *buf, int len)
>      switch (req) {
>      case BAUM_REQ_DisplayData:
>      {
> -        uint8_t cells[baum->x * baum->y], c;
> -        uint8_t text[baum->x * baum->y];
> -        uint8_t zero[baum->x * baum->y];
> +        uint8_t cells[X_MAX * Y_MAX], c;
> +        uint8_t text[X_MAX * Y_MAX];
> +        uint8_t zero[X_MAX * Y_MAX];
>          int cursor = BRLAPI_CURSOR_OFF;
>          int i;
>  
> @@ -408,7 +408,7 @@ static int baum_eat_packet(BaumChardev *baum, const uint8_t *buf, int len)
>          }
>          timer_del(baum->cellCount_timer);
>  
> -        memset(zero, 0, sizeof(zero));
> +        memset(zero, 0, baum->x * baum->y);
>  
>          brlapi_writeArguments_t wa = {
>              .displayNumber = BRLAPI_DISPLAY_DEFAULT,
> -- 
> 2.26.3
>
Marc-André Lureau May 5, 2021, 9:27 p.m. UTC | #2
On Thu, May 6, 2021 at 1:14 AM Philippe Mathieu-Daudé <philmd@redhat.com>
wrote:

> We know 'x * y' will be at most 'X_MAX * Y_MAX' (which is not
> a big value, it is actually 84). Instead of having the compiler
> use variable-length array, declare an array able to hold the
> maximum 'x * y'.
>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>

---
>  chardev/baum.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/chardev/baum.c b/chardev/baum.c
> index adc3d7b3b56..0822e9ed5f3 100644
> --- a/chardev/baum.c
> +++ b/chardev/baum.c
> @@ -383,9 +383,9 @@ static int baum_eat_packet(BaumChardev *baum, const
> uint8_t *buf, int len)
>      switch (req) {
>      case BAUM_REQ_DisplayData:
>      {
> -        uint8_t cells[baum->x * baum->y], c;
> -        uint8_t text[baum->x * baum->y];
> -        uint8_t zero[baum->x * baum->y];
> +        uint8_t cells[X_MAX * Y_MAX], c;
> +        uint8_t text[X_MAX * Y_MAX];
> +        uint8_t zero[X_MAX * Y_MAX];
>          int cursor = BRLAPI_CURSOR_OFF;
>          int i;
>
> @@ -408,7 +408,7 @@ static int baum_eat_packet(BaumChardev *baum, const
> uint8_t *buf, int len)
>          }
>          timer_del(baum->cellCount_timer);
>
> -        memset(zero, 0, sizeof(zero));
> +        memset(zero, 0, baum->x * baum->y);
>

eh, I would have left the sizeof(zero)..


>          brlapi_writeArguments_t wa = {
>              .displayNumber = BRLAPI_DISPLAY_DEFAULT,
> --
> 2.26.3
>
>
>
Samuel Thibault May 5, 2021, 9:39 p.m. UTC | #3
Marc-André Lureau, le jeu. 06 mai 2021 01:27:25 +0400, a ecrit:
>     @@ -408,7 +408,7 @@ static int baum_eat_packet(BaumChardev *baum, const
>     uint8_t *buf, int len)
>              }
>              timer_del(baum->cellCount_timer);
> 
>     -        memset(zero, 0, sizeof(zero));
>     +        memset(zero, 0, baum->x * baum->y);
> 
> 
> eh, I would have left the sizeof(zero)..

I was wondering too, but below this it's clear that only
baum->x * baum->y is getting used.

Samuel
diff mbox series

Patch

diff --git a/chardev/baum.c b/chardev/baum.c
index adc3d7b3b56..0822e9ed5f3 100644
--- a/chardev/baum.c
+++ b/chardev/baum.c
@@ -383,9 +383,9 @@  static int baum_eat_packet(BaumChardev *baum, const uint8_t *buf, int len)
     switch (req) {
     case BAUM_REQ_DisplayData:
     {
-        uint8_t cells[baum->x * baum->y], c;
-        uint8_t text[baum->x * baum->y];
-        uint8_t zero[baum->x * baum->y];
+        uint8_t cells[X_MAX * Y_MAX], c;
+        uint8_t text[X_MAX * Y_MAX];
+        uint8_t zero[X_MAX * Y_MAX];
         int cursor = BRLAPI_CURSOR_OFF;
         int i;
 
@@ -408,7 +408,7 @@  static int baum_eat_packet(BaumChardev *baum, const uint8_t *buf, int len)
         }
         timer_del(baum->cellCount_timer);
 
-        memset(zero, 0, sizeof(zero));
+        memset(zero, 0, baum->x * baum->y);
 
         brlapi_writeArguments_t wa = {
             .displayNumber = BRLAPI_DISPLAY_DEFAULT,