]> sigrok.org Git - libserialport.git/commitdiff
windows: Avoid leak of write buffer on realloc failure.
authorMartin Ling <redacted>
Fri, 24 Jan 2020 04:44:47 +0000 (04:44 +0000)
committerMartin Ling <redacted>
Fri, 24 Jan 2020 05:39:16 +0000 (05:39 +0000)
VS2019 IntelliSense reported:

Warning C6308: 'realloc' might return null pointer: assigning null
               pointer to 'port->write_buf', which is passed as an
       argument to 'realloc', will cause the original memory
       block to be leaked.

This is correct, we would leak the buffer on a realloc failure.

Put the realloc result in a separate variable and handle the error
path before assigning the result to port->write_buf.

serialport.c

index eee453a276155d667a1a32894c62788891bc42aa..422b767a50366aeae3682b5a6124ecc5c06bcdcc 100644 (file)
@@ -1838,6 +1838,7 @@ static enum sp_return set_config(struct sp_port *port, struct port_data *data,
        DEBUG_FMT("Setting configuration for port %s", port->name);
 
 #ifdef _WIN32
+       BYTE* new_buf;
 
        TRY(await_write_completion(port));
 
@@ -1854,11 +1855,10 @@ static enum sp_return set_config(struct sp_port *port, struct port_data *data,
 
                /* Allocate write buffer for 50ms of data at baud rate. */
                port->write_buf_size = max(config->baudrate / (8 * 20), 1);
-               port->write_buf = realloc(port->write_buf,
-                                         port->write_buf_size);
-
-               if (!port->write_buf)
+               new_buf = realloc(port->write_buf, port->write_buf_size);
+               if (!new_buf)
                        RETURN_ERROR(SP_ERR_MEM, "Allocating write buffer failed");
+               port->write_buf = new_buf;
        }
 
        if (config->bits >= 0)