Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 332761 - =media-libs/libsdl-1.2.14-r3 fails to recognize the correct number of axis on a joystick
Summary: =media-libs/libsdl-1.2.14-r3 fails to recognize the correct number of axis on...
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: [OLD] Library (show other bugs)
Hardware: x86 Linux
: High normal (vote)
Assignee: Gentoo Games
URL: http://bugzilla.libsdl.org/show_bug.c...
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-08-14 18:02 UTC by Gabriel Marcano
Modified: 2011-01-26 08:26 UTC (History)
1 user (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
SDL-1.2.14/src/joystick/linux/SDL_sysjoystick.c patch correcting a possible memory overflow (libsdl-1.2.14-joystick.patch,717 bytes, patch)
2010-08-14 18:04 UTC, Gabriel Marcano
Details | Diff
Difference observed between 1.2.14 and the SDL repository (diff_between_SDL_version.diff,353 bytes, text/plain)
2010-08-14 18:07 UTC, Gabriel Marcano
Details
Source code for the program to test joystick (See comments inside as to how to compile). (sdl_js_test.cpp,462 bytes, text/plain)
2010-08-14 18:08 UTC, Gabriel Marcano
Details
SDL-1.2.14/src/joystick/linux/SDL_sysjoystick.c patch that is more memory efficient, with some changes (libsdl-1.2.14-joystick.patch,1.83 KB, patch)
2010-08-14 18:40 UTC, Gabriel Marcano
Details | Diff
SDL-1.2.14/src/joystick/linux/SDL_sysjoystick.c patch undoing the change to the includes of the file (libsdl-1.2.14-linux-joystick.patch,1.51 KB, patch)
2010-08-16 02:54 UTC, Gabriel Marcano
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Gabriel Marcano 2010-08-14 18:02:47 UTC
A joystick (SNES pad) with two axis and 8 buttons is identified correctly by 'jstest' with the correct number of buttons and axis. However, it
seems that any application using SDL (at least two separate ones on the system
this was encountered) to handle input identifies incorrectly that the joystick
has at least one less axis than it really has. 


Reproducible: Always

Steps to Reproduce:
1. emerge =media-libs/libsdl-1.2.14-r3
2. Run any SDL application that can use a joystick (like zsnes)
3. Test joystick in application

Actual Results:  
One of the axis of the joystick is not recognized by the application.

Expected Results:  
All axis were recognized by the application.

I'm using ~x86 (unstable). I first encountered this bug while trying to I've reported this bug upstream as well, and I might have hit a possible solution... I first ran into it by trying to use the SNES pad with zsnes recently. Zsnes reported that it found a joystick with only one axis, which was strange. After some digging through SDL code, I found what could be the cause. The code in SDL's Mercurial repository seems to work, and by diff-ing the source code from their repository with the one for release 1.2.14, I found one change in SDL_sysjoystick.c. This change fixed what appears to be a memory overflow-- that variable was an array with a fixed size, and an ioctl call (due to a recent change) returned more data than the array could handle. Fixing this array problem made the axis problem to go away. I will submit a patch for libsdl I developed that appears address that problem, and I'll also upload a small c++ program that I used to test the number of axis SDL found. 

In case anyone wants to see the original bug report, it is found here:
http://bugzilla.libsdl.org/show_bug.cgi?id=1044
There might be a little more information there about the bug itself. I can supply more information if needed.

Here is my emerge --info output:
Portage 2.2_rc67 (default/linux/x86/10.0/desktop, gcc-4.4.4, glibc-2.11.2-r0, 2.6.35-gentoo i686)
=================================================================
System uname: Linux-2.6.35-gentoo-i686-Intel-R-_Pentium-R-_4_CPU_2.93GHz-with-gentoo-2.0.1
Timestamp of tree: Fri, 13 Aug 2010 15:45:01 +0000
app-shells/bash:     4.1_p7
dev-java/java-config: 2.1.11
dev-lang/python:     2.5.4-r4, 2.6.5-r3, 3.1.2-r4
dev-util/cmake:      2.8.1-r2
sys-apps/baselayout: 2.0.1
sys-apps/openrc:     0.6.1-r1
sys-apps/sandbox:    2.2
sys-devel/autoconf:  2.13, 2.65-r1
sys-devel/automake:  1.9.6-r3, 1.10.3, 1.11.1
sys-devel/binutils:  2.20.1-r1
sys-devel/gcc:       4.3.5, 4.4.4-r1
sys-devel/gcc-config: 1.4.1
sys-devel/libtool:   2.2.10
virtual/os-headers:  2.6.34
ACCEPT_KEYWORDS="x86 ~x86"
ACCEPT_LICENSE="* -@EULA PUEL"
CBUILD="i686-pc-linux-gnu"
CFLAGS="-O2 -march=native -mtune=native -pipe"
CHOST="i686-pc-linux-gnu"
CONFIG_PROTECT="/etc /usr/share/X11/xkb /var/lib/hsqldb"
CONFIG_PROTECT_MASK="/etc/ca-certificates.conf /etc/env.d /etc/env.d/java/ /etc/fonts/fonts.conf /etc/gconf /etc/gentoo-release /etc/php/apache2-php5/ext-active/ /etc/php/cgi-php5/ext-active/ /etc/php/cli-php5/ext-active/ /etc/revdep-rebuild /etc/sandbox.d /etc/splash /etc/terminfo /etc/texmf/language.dat.d /etc/texmf/language.def.d /etc/texmf/updmap.d /etc/texmf/web2c"
CXXFLAGS="-O2 -march=native -mtune=native -pipe"
DISTDIR="/usr/portage/distfiles"
FEATURES="assume-digests distlocks fixpackages news parallel-fetch preserve-libs protect-owned sandbox sfperms strict unmerge-logs unmerge-orphans userfetch"
GENTOO_MIRRORS="http://gentoo.osuosl.org/ "
LANG="en_US.UTF-8"
LDFLAGS="-Wl,-O1 -Wl,--as-needed -Wl,--as-needed"
LINGUAS="en es"
MAKEOPTS="-j3"
PKGDIR="/usr/portage/packages"
PORTAGE_CONFIGROOT="/"
PORTAGE_RSYNC_OPTS="--recursive --links --safe-links --perms --times --compress --force --whole-file --delete --stats --timeout=180 --exclude=/distfiles --exclude=/local --exclude=/packages"
PORTAGE_TMPDIR="/var/tmp"
PORTDIR="/usr/portage"
PORTDIR_OVERLAY="/var/lib/layman/sunrise /usr/local/portage"
SYNC="rsync://rsync.namerica.gentoo.org/gentoo-portage"
USE="X a52 aac acl acpi alsa ao apache2 asf avahi avi berkdb branding bzip2 bzlib cairo cdr cli consolekit cracklib crypt cups cxx dbus device-mapper devil directfb dri dts dvd dvdr emboss encode esd exif fam ffmpeg firefox flac fortran gdbm gdu gif glib gnome gnutls gpm gstreamer gtk gtk2 hal iconv ipv6 jack java javascript joystick jpeg kdrive kerberos lcms ldap libnotify lua mad mdnsresponder-compat midi mikmod mmx mng modules mp3 mp4 mpeg mudflap nautilus ncurses nls nptl nptlonly nsplugin odbc ogg oggvorbis openal opengl openmp pam pango pcre pdf perl pm-utils png portaudio posix ppds pppd pulseaudio python qt qt3 qt3support qt4 quicktime readline reflection samba sdl session sox spell spl sqlite sse sse2 ssl startup-notification subversion svg symlink sysfs tcpd theora tiff timidity truetype udev unicode usb videos vorbis wav win32codecs wmf x264 x86 xcb xml xmms xorg xulrunner xv xvid zlib" ALSA_CARDS="ali5451 als4000 atiixp atiixp-modem bt87x ca0106 cmipci emu10k1 emu10k1x ens1370 ens1371 es1938 es1968 fm801 hda-intel intel8x0 intel8x0m maestro3 trident usb-audio via82xx via82xx-modem ymfpci" ALSA_PCM_PLUGINS="adpcm alaw asym copy dmix dshare dsnoop empty extplug file hooks iec958 ioplug ladspa lfloat linear meter mmap_emul mulaw multi null plug rate route share shm softvol" APACHE2_MODULES="authn_file vhost_alias auth_basic authz_user dav dav_fs dav_lock dir mime authz_host alias auth_digest info mime_magic status userdir" ELIBC="glibc" INPUT_DEVICES="keyboard mouse joystick evdev" KERNEL="linux" LCD_DEVICES="bayrad cfontz cfontz633 glk hd44780 lb216 lcdm001 mtxorb ncurses text" LINGUAS="en es" RUBY_TARGETS="ruby18" USERLAND="GNU" VIDEO_CARDS="intel i810 vesa fbdev vga" XTABLES_ADDONS="quota2 psd pknock lscan length2 ipv4options ipset ipp2p iface geoip fuzzy condition tee tarpit sysrq steal rawnat logmark ipmark dhcpmac delude chaos account" 
Unset:  CPPFLAGS, CTARGET, EMERGE_DEFAULT_OPTS, FFLAGS, INSTALL_MASK, LC_ALL, PORTAGE_COMPRESS, PORTAGE_COMPRESS_FLAGS, PORTAGE_RSYNC_EXTRA_OPTS
Comment 1 Gabriel Marcano 2010-08-14 18:04:04 UTC
Created attachment 242975 [details, diff]
SDL-1.2.14/src/joystick/linux/SDL_sysjoystick.c patch correcting a possible memory overflow
Comment 2 Gabriel Marcano 2010-08-14 18:07:48 UTC
Created attachment 242977 [details]
Difference observed between 1.2.14 and the SDL repository

To be honest, I don't like the fix the SDL developers applied, since it's a magic number (I can't count the number of times my programming professors have bashed on magic numbers). The patch supplied previously (libsdl-1.2.14-joystick.patch) has (what I think) to be a better way to handle that issue.
Comment 3 Gabriel Marcano 2010-08-14 18:08:54 UTC
Created attachment 242979 [details]
Source code for the program to test joystick (See comments inside as to how to compile).
Comment 4 Gabriel Marcano 2010-08-14 18:40:53 UTC
Created attachment 242981 [details, diff]
SDL-1.2.14/src/joystick/linux/SDL_sysjoystick.c patch  that is more memory efficient, with some changes

Silly mistake on my end-- I briefly forgot that when declaring arrays in C, the memory used by the array is equal to the number of elements times the size of the type of array. I decided to utilize the struct to its fullest, and instead of using an array to access the data, I used the actual struct members. That hopefully makes the code more readable, and prevents mistakes based on assumptions that other code won't change (which it will, with time). If I find any other stupid mistakes with my patch, I'll re-upload a fix.
Comment 5 Mr. Bones. (RETIRED) gentoo-dev 2010-08-15 06:22:34 UTC
does fbsd have linux/input.h?  seems like the patch need more work to be generally useful.

Comment 6 Gabriel Marcano 2010-08-15 15:57:55 UTC
(In reply to comment #5)
> does fbsd have linux/input.h?  seems like the patch need more work to be
> generally useful.
> 

Hm. I hadn't considered other Posix compliant systems... Due to lack of experience. I'm not an expert programmer, by any definition of that term, so I'm not perfectly sure how to make the patch more general. The problem here is this:

If you see the diff output between the 1.2.14 release and the one from their mercurial repository, they use an int array to house the struct input_absinfo, which is used later like this to fill it with information:
ioctl(fd, EVIOCGABS(i), values)

Since I didn't change any other includes, I'm assuming that the EVIOCGABS macro exists in fbsd, somewhere? I'll try looking for it later (don't have the time right now). Any suggestions would be gladly accepted, as well!
Comment 7 Gabriel Marcano 2010-08-16 02:54:16 UTC
Created attachment 243157 [details, diff]
SDL-1.2.14/src/joystick/linux/SDL_sysjoystick.c patch undoing the change to the includes of the file

I seemingly didn't pay enough attention to the code... a preprocessor directive surrounding <linux/input.h> is also shared by the block of code that is being addressed by the patch, so the change of the conditional around it was unnecessary. Also, this code only appears to affect Linux systems, and not FreeBSD systems (the incorrect patch might have messed with compilation, but I don't know since I don't have a fbsd box to tinker with). This code, based on its directory, pertains ONLY to the Linux implementation of joystick setup for SDL. At least I'm fairly confident this is the case. Since the problem was tied to that ictl call ("ioctl(fd, EVIOCGABS(i), values" on line 705), and that line is missing from the fbsd implementation (the macro EVIOCGABS doesn't exist in FreeBSD), I think it would be safe to assume this problem does not exist in that system (please, please correct me if I am mistaken!). Thus, I think, this problem pertains only to anyone with SDL in a Linux box. Hopefully this new version of the patch fixes that issue.
Comment 8 Markus Rathgeb 2011-01-25 20:36:02 UTC
After I solved the problem for myself and would like also to send a bug report here, I realized there is an existing one.

At first I reported the bug upstream and after I solved the bug (a similar patch as here) the solution was accepted upstream and merged to their repository.

Have you reported the bug upstream ever?

http://bugzilla.libsdl.org/show_bug.cgi?id=1080
Comment 9 Mr. Bones. (RETIRED) gentoo-dev 2011-01-26 08:26:52 UTC
in portage.  thanks for the bug report and patch.