bindings: python: fix a use-after-free bug
authorIker Pedrosa <ikerpedrosam@gmail.com>
Mon, 29 Jul 2024 10:57:16 +0000 (12:57 +0200)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Wed, 31 Jul 2024 08:46:36 +0000 (10:46 +0200)
`req_cfg` variable is freed and then used, which would generate an
error. Avoid this problem by freeing when the variable will no longer be
used.

Signed-off-by: Iker Pedrosa <ikerpedrosam@gmail.com>
Reviewed-by: Kent Gibson <warthog618@gmail.com>
Link: https://lore.kernel.org/r/3d8b12dd60eec59d4184c0bcc7d575b4eccbc22c.1722250385.git.ikerpedrosam@gmail.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
bindings/python/gpiod/ext/chip.c

index 28cf504f6c5c3f47e954a1fdb714d019a497abd6..e8eaad8a3e4a8046ba6689e7c40f1b1ec515a930 100644 (file)
@@ -274,14 +274,16 @@ static PyObject *chip_request_lines(chip_object *self, PyObject *args)
        Py_BEGIN_ALLOW_THREADS;
        request = gpiod_chip_request_lines(self->chip, req_cfg, line_cfg);
        Py_END_ALLOW_THREADS;
-       gpiod_request_config_free(req_cfg);
-       if (!request)
+       if (!request) {
+               gpiod_request_config_free(req_cfg);
                return Py_gpiod_SetErrFromErrno();
+       }
 
        req_obj = Py_gpiod_MakeRequestObject(request,
                        gpiod_request_config_get_event_buffer_size(req_cfg));
        if (!req_obj)
                gpiod_line_request_release(request);
+       gpiod_request_config_free(req_cfg);
 
        return req_obj;
 }