Skip to contentSkip to conversation

src/sae.c: Use realloc(3) instead of reallocarray(3)

PATCH In review Fabrice Fontaine 2 participants 3 messages Sep 19, 2021

lore
Share & tools
lore

Fabrice Fontaine

author
#0 lore raw

PATCH 1/1 src/sae.c: Use realloc(3) instead of reallocarray(3)

reallocarray(3) has been added to glibc relatively recently (version 2.26, from 2017) and apparently not all users run new enough glibc. Moreover, reallocarray is not available with uclibc-ng. Just use realloc(3) for now since in this case there's no real risk of overflow. This will avoid the following build failure since commit 891b78e9e892a3bcd800eb3a298e6380e9a15dd1:

/home/giuliobenetti/autobuild/run/instance-3/output-1/host/lib/gcc/xtensa-buildroot-linux-uclibc/10.3.0/../../../../xtensa-buildroot-linux-uclibc/bin/ld: src/sae.o: in function `sae_rx_authenticate':
sae.c:(.text+0xd74): undefined reference to `reallocarray'

Fixes:
- http://autobuild.buildroot.org/results/c6d3f86282c44645b4f1c61882dc63ccfc8eb35a

Signed-off-by Fabrice Fontaine <fontaine.fabrice(a)gmail.com>
---
 src/sae.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
src/sae.c +3 -3
index 7e6377f5..d04891e7 100644
@@ -104,7 +104,7 @@ static void sae_rejected_groups_append(struct sae_sm *sm, uint16_t group)
 	uint16_t i;
 
 	if (!sm->rejected_groups) {
-		sm->rejected_groups = reallocarray(NULL, 2, sizeof(uint16_t));
+		sm->rejected_groups = realloc(NULL, 2 * sizeof(uint16_t));
 		sm->rejected_groups[0] = 1;
 		sm->rejected_groups[1] = group;
 		return;
@@ -114,8 +114,8 @@ static void sae_rejected_groups_append(struct sae_sm *sm, uint16_t group)
 		if (sm->rejected_groups[i] == group)
 			return;
 
-	sm->rejected_groups = reallocarray(sm->rejected_groups,
-						i + 1, sizeof(uint16_t));
+	sm->rejected_groups = realloc(sm->rejected_groups,
+					(i + 1) * sizeof(uint16_t));
 	sm->rejected_groups[0] += 1;
 	sm->rejected_groups[i] = group;
 }
Signature · 1 line
2.33.0
#1 lore raw

Hi Fabrice,

Quoted 7 lines · Fabrice Fontaine

reallocarray(3) has been added to glibc relatively recently (version 2.26, from 2017) and apparently not all users run new enough glibc. Moreover, reallocarray is not available with uclibc-ng. Just use realloc(3) for now since in this case there's no real risk of overflow. This will avoid the following build failure since commit 891b78e9e892a3bcd800eb3a298e6380e9a15dd1:

Quoted 7 lines

/home/giuliobenetti/autobuild/run/instance-3/output-1/host/lib/gcc/xtensa-buildroot-linux-uclibc/10.3.0/../../../../xtensa-buildroot-linux-uclibc/bin/ld: src/sae.o: in function `sae_rx_authenticate':
sae.c:(.text+0xd74): undefined reference to `reallocarray'

Fixes:
- http://autobuild.buildroot.org/results/c6d3f86282c44645b4f1c61882dc63ccfc8eb35a

Signed-off-by Fabrice Fontaine <fontaine.fabrice(a)gmail.com>

No S-o-b tags please.

Quoted 18 lines
---
  src/sae.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
src/sae.c +1 -1
index 7e6377f5..d04891e7 100644
@@ -104,7 +104,7 @@ static void sae_rejected_groups_append(struct sae_sm *sm, uint16_t group)
  	uint16_t i;
 
  	if (!sm->rejected_groups) {
-		sm->rejected_groups = reallocarray(NULL, 2, sizeof(uint16_t));
+		sm->rejected_groups = realloc(NULL, 2 * sizeof(uint16_t));
  		sm->rejected_groups[0] = 1;
  		sm->rejected_groups[1] = group;
  		return;

src/sae.c static void sae_rejected_groups_append(struct sae_sm *sm, uint16_t group) · line 118

Quoted 7 lines
src/sae.c +2 -2
@@ -114,8 +114,8 @@ static void sae_rejected_groups_append(struct sae_sm *sm, uint16_t group)
  		if (sm->rejected_groups[i] == group)
  			return;
 
-	sm->rejected_groups = reallocarray(sm->rejected_groups,
-						i + 1, sizeof(uint16_t));
+	sm->rejected_groups = realloc(sm->rejected_groups,
+					(i + 1) * sizeof(uint16_t));

Can we add this to missing.h instead? The reallocarray version is more clear / preferable to open-coded version.

src/sae.c static void sae_rejected_groups_append(struct sae_sm *sm, uint16_t group) · line 121

Quoted 4 lines
src/sae.c +0 -0
  	sm->rejected_groups[0] += 1;
  	sm->rejected_groups[i] = group;
  }

Regards,
-Denis

author
#2 lore raw

Hi Denis,

Quoted 14 lines · Denis Kenzior

Hi Fabrice,

Quoted 10 lines · Fabrice Fontaine

reallocarray(3) has been added to glibc relatively recently (version 2.26, from 2017) and apparently not all users run new enough glibc. Moreover, reallocarray is not available with uclibc-ng. Just use realloc(3) for now since in this case there's no real risk of overflow. This will avoid the following build failure since commit 891b78e9e892a3bcd800eb3a298e6380e9a15dd1:

/home/giuliobenetti/autobuild/run/instance-3/output-1/host/lib/gcc/xtensa-buildroot-linux-uclibc/10.3.0/../../../../xtensa-buildroot-linux-uclibc/bin/ld: src/sae.o: in function `sae_rx_authenticate':
sae.c:(.text+0xd74): undefined reference to `reallocarray'

Quoted 6 lines
Quoted 4 lines

Fixes:
- http://autobuild.buildroot.org/results/c6d3f86282c44645b4f1c61882dc63ccfc8eb35a

Signed-off-by Fabrice Fontaine <fontaine.fabrice(a)gmail.com>

No S-o-b tags please.

OK, I'll remove it from v2.

Quoted 22 lines
Quoted 22 lines
---
  src/sae.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
src/sae.c +1 -1
index 7e6377f5..d04891e7 100644
@@ -104,7 +104,7 @@ static void sae_rejected_groups_append(struct sae_sm *sm, uint16_t group)
      uint16_t i;
 
      if (!sm->rejected_groups) {
-             sm->rejected_groups = reallocarray(NULL, 2, sizeof(uint16_t));
+             sm->rejected_groups = realloc(NULL, 2 * sizeof(uint16_t));
              sm->rejected_groups[0] = 1;
              sm->rejected_groups[1] = group;
              return;
@@ -114,8 +114,8 @@ static void sae_rejected_groups_append(struct sae_sm *sm, uint16_t group)
              if (sm->rejected_groups[i] == group)
                      return;
 
Quoted 7 lines

src/sae.c static void sae_rejected_groups_append(struct sae_sm *sm, uint16_t group) · line 118

Quoted 4 lines
src/sae.c +2 -2
-     sm->rejected_groups = reallocarray(sm->rejected_groups,
-                                             i + 1, sizeof(uint16_t));
+     sm->rejected_groups = realloc(sm->rejected_groups,
+                                     (i + 1) * sizeof(uint16_t));

Can we add this to missing.h instead? The reallocarray version is more clear / preferable to open-coded version.

Sure, I'll send a v2.

Quoted 8 lines

src/sae.c static void sae_rejected_groups_append(struct sae_sm *sm, uint16_t group) · line 121

Quoted 5 lines
src/sae.c +0 -0
      sm->rejected_groups[0] += 1;
      sm->rejected_groups[i] = group;
  }

Regards,
-Denis

Best Regards,

Fabrice