failing to mmap() causes segfault due to wrong cast
A Python module to control the GPIO on a Raspberry Pi
Brought to you by:
croston
Hi,
In the setup() function, you use:
gpio_map = (uint32_t *)mmap(NULL, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, mem_fd, 0); if ((uint32_t)gpio_map < 0) { return SETUP_MMAP_FAIL; } else { return SETUP_OK; }
The if condition can never be true because of the cast. You want (int32_t), not (uint32_t).
There's the same problem later on, when opening /dev/mem.
I ran into this when trying to run RPi.GPIO on a stock Debian kernel, that ships with CONFIG_DEVMEM_STRICT=y, and thus requires iomem=relaxed to mmap() like that.
I've changed the if condition in the code. Does it still need iomem=relaxed?
You can get the development version to test it:
$ sudo apt install mercurial
$ sudo pip3 install hg+http://hg.code.sf.net/p/raspberry-gpio-python/code#egg=RPi.GPIO
Hi,
I confirm that the fix works. Thanks!
Yes, it still needs iomem=relaxed. Without it, it fails with:
But that's expected: the Debian image doesn't have /dev/gpiomem, so the restrictions set by CONFIG_DEVMEM_STRICT=y apply.
Maybe a note about that should be added to the README.
In release 0.7.0