]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: avoid undefined behaviour in libkmod-builtin.c:get_string
authorEugene Syromiatnikov <esyr@redhat.com>
Tue, 13 Aug 2024 14:17:27 +0000 (16:17 +0200)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Tue, 13 Aug 2024 17:05:52 +0000 (12:05 -0500)
Static analysis has reported a potential UB:

    kmod-31/libkmod/libkmod-builtin.c:125: use_invalid: Using "nullp", which points to an out-of-scope variable "buf".
    #  123|    size_t linesz = 0;
    #  124|
    #  125|->  while (!nullp) {
    #  126|    char buf[BUFSIZ];
    #  127|    ssize_t sz;

It seems to be indeed an UB, as nullp is getting assined an address
inside object buf, which has a lifetime of the while loop body,
and is not available outside of it (specifically, in the while
condition, where nullp is checked for NULL).  Fix it by putting
buf definition in the outer block.

libkmod/libkmod-builtin.c

index fd0f54923a487de365a6ba5c0bdc6ef6ba44fe5c..40a7d6142d03b1f303e223876cf4ea1fe5d25397 100644 (file)
@@ -105,11 +105,11 @@ static off_t get_string(struct kmod_builtin_iter *iter, off_t offset,
                        char **line, size_t *size)
 {
        int sv_errno;
+       char buf[BUFSIZ];
        char *nullp = NULL;
        size_t linesz = 0;
 
        while (!nullp) {
-               char buf[BUFSIZ];
                ssize_t sz;
                size_t partsz;