Skip to contentSkip to conversation

multipathd: fix memory allocation logic error for polls in uxsock_listen()

PATCH In review [email protected] 3 participants 3 messages Aug 10, 2016 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 multipathd: fix memory allocation logic error for polls in uxsock_listen()

From: "tang.junhui" <[email protected]>

logic error exists in memory allocation for polls in uxsock_listen(), even if the allocated memory size meet the needs, it is still to realloc memory, which is not up to expectations.

---
 multipathd/uxlsnr.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
multipathd/uxlsnr.c +5 -3
index f114e59..fa29b2a 100644
@@ -145,7 +145,7 @@ void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)
 	pthread_cleanup_push(uxsock_cleanup, NULL);
 
 	condlog(3, "uxsock: startup listener");
-	polls = (struct pollfd *)MALLOC(MIN_POLLS + 1);
+	polls = (struct pollfd *)MALLOC((MIN_POLLS + 1) * sizeof(struct pollfd));
 	if (!polls) {
 		condlog(0, "uxsock: failed to allocate poll fds");
 		return NULL;
@@ -167,9 +167,11 @@ void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)
 		}
 		if (num_clients != old_clients) {
 			struct pollfd *new;
-			if (num_clients < MIN_POLLS) {
+			if (num_clients <= MIN_POLLS && old_clients > MIN_POLLS) {
 				new = REALLOC(polls, (1 + MIN_POLLS) *
 						sizeof(struct pollfd));
+			} else if (num_clients <= MIN_POLLS && old_clients <= MIN_POLLS) {
+				new = polls;
 			} else {
 				new = REALLOC(polls, (1+num_clients) *
 						sizeof(struct pollfd));
@@ -181,7 +183,7 @@ void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)
 				pthread_yield();
 				continue;
 			}
-			num_clients = old_clients;
+			old_clients = num_clients;
 			polls = new;
 		}
 		polls[0].fd = ux_sock;
Signature · 1 line
2.8.1.windows.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 37 lines · On 08/10/2016 11:32 AM, [email protected] wrote:

From: "tang.junhui" <[email protected]>

logic error exists in memory allocation for polls in uxsock_listen(), even if the allocated memory size meet the needs, it is still to realloc memory, which is not up to expectations.

---
 multipathd/uxlsnr.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
multipathd/uxlsnr.c +4 -2
index f114e59..fa29b2a 100644
@@ -145,7 +145,7 @@ void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)
 	pthread_cleanup_push(uxsock_cleanup, NULL);
 
 	condlog(3, "uxsock: startup listener");
-	polls = (struct pollfd *)MALLOC(MIN_POLLS + 1);
+	polls = (struct pollfd *)MALLOC((MIN_POLLS + 1) * sizeof(struct pollfd));
 	if (!polls) {
 		condlog(0, "uxsock: failed to allocate poll fds");
 		return NULL;
@@ -167,9 +167,11 @@ void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)
 		}
 		if (num_clients != old_clients) {
 			struct pollfd *new;
-			if (num_clients < MIN_POLLS) {
+			if (num_clients <= MIN_POLLS && old_clients > MIN_POLLS) {
 				new = REALLOC(polls, (1 + MIN_POLLS) *
 						sizeof(struct pollfd));
+			} else if (num_clients <= MIN_POLLS && old_clients <= MIN_POLLS) {
+				new = polls;
 			} else {
 				new = REALLOC(polls, (1+num_clients) *
 						sizeof(struct pollfd));

multipathd/uxlsnr.c void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data) · line 189

Quoted 8 lines
multipathd/uxlsnr.c +1 -1
@@ -181,7 +183,7 @@ void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)
 				pthread_yield();
 				continue;
 			}
-			num_clients = old_clients;
+			old_clients = num_clients;
 			polls = new;
 		}
 		polls[0].fd = ux_sock;

Thanks for catching this.

Cheers,

Hannes

Signature · 5 lines
Dr. Hannes Reinecke		   Teamlead Storage & Networking
[email protected]			               +49 911 74053 688
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: F. Imendörffer, J. Smithard, J. Guild, D. Upmanyu, G. Norton
HRB 21284 (AG Nürnberg)
#2 lore raw

Merged.
Thanks.

Quoted 73 lines · Hannes Reinecke
Quoted 3 lines · On 08/10/2016 11:32 AM, [email protected] wrote:

From: "tang.junhui" <[email protected]>

logic error exists in memory allocation for polls in uxsock_listen(),

even if the allocated memory size meet the needs, it is still to realloc memory, which is not up to expectations.

multipathd/uxlsnr.c void * uxsock_listen(uxsock_trigger_fn

Quoted 11 lines
---
 multipathd/uxlsnr.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
multipathd/uxlsnr.c +0 -0
index f114e59..fa29b2a 100644
@@ -145,7 +145,7 @@ void * uxsock_listen(uxsock_trigger_fn

uxsock_trigger, void * trigger_data)

multipathd/uxlsnr.c void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)

Quoted 5 lines
multipathd/uxlsnr.c +1 -1
      pthread_cleanup_push(uxsock_cleanup, NULL);
 
      condlog(3, "uxsock: startup listener");
-     polls = (struct pollfd *)MALLOC(MIN_POLLS + 1);
+     polls = (struct pollfd *)MALLOC((MIN_POLLS + 1) * sizeof(struct

pollfd));

multipathd/uxlsnr.c void * uxsock_listen(uxsock_trigger_fn

Quoted 4 lines
multipathd/uxlsnr.c +0 -0
      if (!polls) {
              condlog(0, "uxsock: failed to allocate poll fds");
              return NULL;
@@ -167,9 +167,11 @@ void * uxsock_listen(uxsock_trigger_fn

uxsock_trigger, void * trigger_data)

multipathd/uxlsnr.c void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)

Quoted 5 lines
multipathd/uxlsnr.c +1 -1
              }
              if (num_clients != old_clients) {
                      struct pollfd *new;
-                     if (num_clients < MIN_POLLS) {
+                     if (num_clients <= MIN_POLLS && old_clients >

MIN_POLLS) {

multipathd/uxlsnr.c void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)

Quoted 3 lines
multipathd/uxlsnr.c +1 -0
                              new = REALLOC(polls, (1 + MIN_POLLS) *
                                              sizeof(struct pollfd));
+                     } else if (num_clients <= MIN_POLLS && old_clients

<= MIN_POLLS) {

multipathd/uxlsnr.c void * uxsock_listen(uxsock_trigger_fn

Quoted 5 lines
multipathd/uxlsnr.c +1 -0
+                             new = polls;
                      } else {
                              new = REALLOC(polls, (1+num_clients) *
                                              sizeof(struct pollfd));
@@ -181,7 +183,7 @@ void * uxsock_listen(uxsock_trigger_fn

uxsock_trigger, void * trigger_data)

multipathd/uxlsnr.c void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data) · line 189

Quoted 9 lines
multipathd/uxlsnr.c +1 -1
                              pthread_yield();
                              continue;
                      }
-                     num_clients = old_clients;
+                     old_clients = num_clients;
                      polls = new;
              }
              polls[0].fd = ux_sock;

Thanks for catching this.

Cheers,

Hannes
--
Dr. Hannes Reinecke Teamlead Storage & Networking
[email protected] +49 911 74053 688
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: F. Imendörffer, J. Smithard, J. Guild, D. Upmanyu, G. Norton HRB 21284 (AG Nürnberg)

--
dm-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/dm-devel