Can't compile "iwlwifi" module in Linux

I’m trying to build iwlwifi module manually and for my needs.

git.kernel.org/pub/scm/linux/kernel/…/wireless/

When I run Makefile as make, I get:


<span style="color:#323232;">subcmd-util.h: In function ‘xrealloc’:
</span><span style="color:#323232;">subcmd-util.h:58:31: error: pointer ‘ptr’ may be used after ‘realloc’ [-Werror=use-after-free]
</span><span style="color:#323232;">   58 |                         ret = realloc(ptr, 1);
</span><span style="color:#323232;">      |                               ^~~~~~~~~~~~~~~
</span><span style="color:#323232;">subcmd-util.h:52:21: note: call to ‘realloc’ here
</span><span style="color:#323232;">   52 |         void *ret = realloc(ptr, size);
</span><span style="color:#323232;">      |                     ^~~~~~~~~~~~~~~~~~
</span><span style="color:#323232;">subcmd-util.h:56:23: error: pointer ‘ptr’ may be used after ‘realloc’ [-Werror=use-after-free]
</span><span style="color:#323232;">   56 |                 ret = realloc(ptr, size);
</span><span style="color:#323232;">      |                       ^~~~~~~~~~~~~~~~~~
</span><span style="color:#323232;">subcmd-util.h:52:21: note: call to ‘realloc’ here
</span><span style="color:#323232;">   52 |         void *ret = realloc(ptr, size);
</span><span style="color:#323232;">      |                     ^~~~~~~~~~~~~~~~~~
</span><span style="color:#323232;">cc1: all warnings being treated as errors
</span><span style="color:#323232;">make[4]: *** [/data/iwlwifi-fixes/tools/build/Makefile.build:97: /data/iwlwifi-fixes/tools/objtool/help.o] Error 1
</span><span style="color:#323232;">make[3]: *** [Makefile:59: /data/iwlwifi-fixes/tools/objtool/libsubcmd-in.o] Error 2
</span><span style="color:#323232;">make[2]: *** [Makefile:63: /data/iwlwifi-fixes/tools/objtool/libsubcmd.a] Error 2
</span><span style="color:#323232;">make[1]: *** [Makefile:69: objtool] Error 2
</span><span style="color:#323232;">make: *** [Makefile:1349: tools/objtool] Error 2
</span><span style="color:#323232;">
</span>

Why is it? How to fix it?

cbarrick, (edited )

Why is it?

The code hitting that error is here:

git.kernel.org/pub/scm/linux/…/subcmd-util.h

It looks fine to me.

What you are seeing is a warning that your compiler may have found a use-after-free bug, but I think this is a false positive. Your build is configured to turn this warning into a hard error.

How to fix it?

I think it will be difficult to know how to fix this without knowing more about your build setup. Are you passing any custom CFLAGS? What compiler and version are you using?

Also, here is someone asking about the same issue (in the same code) on Stack Exchange using GGC 12.1:

unix.stackexchange.com/…/linux-kernel-5-15-54-com…

This was the top result when Googling linux “-Werror=use-after-free”.

I believe you can disable this warning in this file by adding a pragma after the includes (line 8):


<span style="color:#323232;">#pragma GCC diagnostic ignored "-Wuse-after-free"
</span>

See stackoverflow.com/…/selectively-remove-a-warning-…

Edit: If you don’t want to change the code, try disabling the use-after-free warning from the make call:


<span style="color:#323232;">make CFLAGS="-Wno-use-after-free"
</span>
nothingness,

I think it will be difficult to know how to fix this without knowing more about your build setup. Are you passing any custom CFLAGS? What compiler and version are you using?

No.


<span style="color:#323232;">gcc --version
</span><span style="color:#323232;">gcc (GCC) 13.2.1 20230801
</span>

The goal - simply compile it for now.

cbarrick,

If you don’t want to change the code, try disabling the use-after-free warning from the make call:


<span style="color:#323232;">make CFLAGS="-Wno-use-after-free"
</span>
litchralee,

No objections to your answer to the OP’s question, but as a curiosity, I’m trying to figure out what the original xrealloc() function is trying to do.

So far as I can tell, it tries a normal realloc() with the requested size, but if that fails, tries again with size=1. But strangely, it that fails, tries using the requested size a second time. And if that still fails, tries once more with size=1.

The POSIX man page isn’t giving me any hints as to why size=1 might be special, or if this is some sort of Linux-specific behavior or workaround. I wondered if you might have some insight why this function is the way it is.

Note: I’m on mobile, so haven’t checked the Git Blame history yet.

AlmightySnoo, (edited )
@AlmightySnoo@lemmy.world avatar

What revision are you using? Because I don’t see the ret = realloc(ptr, 1); line in github.com/torvalds/linux/blob/…/subcmd-util.h . (EDIT: my bad, it’s in the subcmd-util.h file linked in the other comment)

Also, make sure you have the latest version of GCC, because I remember it yielding many false-positives when I was implementing some reference counting (which obviously is prone to false positives in static analysis) and those warnings disappeared after updating the compiler.

EDIT: from the looks of it GCC’s “static analysis” basically assumed the worst case (ie that the first realloc is freeing memory and the second one is allocating new memory elsewhere and is copying from the old pointer, thus accessing it and hence the use-after-free warning). You can probably just remove the warning as the others have suggested, or you could try to downgrade GCC to version 11 as many of the search results regarding those use-after-free warnings involve later versions.

yardy_sardley,

This would normally be a compiler warning, but someone has enabled the -Werror compiler option (probably in the makefile) which causes the compiler to treat all warnings as errors. You can just remove any -Werror flags from the makefile and it should compile properly.

nothingness,

will try

  • All
  • Subscribed
  • Moderated
  • Favorites
  • programming@programming.dev
  • cubers
  • magazineikmin
  • InstantRegret
  • everett
  • osvaldo12
  • Youngstown
  • mdbf
  • slotface
  • rosin
  • kavyap
  • Durango
  • ngwrru68w68
  • thenastyranch
  • DreamBathrooms
  • provamag3
  • khanakhh
  • anitta
  • modclub
  • ethstaker
  • tester
  • GTA5RPClips
  • cisconetworking
  • tacticalgear
  • megavids
  • Leos
  • normalnudes
  • JUstTest
  • lostlight
  • All magazines