Bug 5003 - Xorg code misuses S_IF* macros
Summary: Xorg code misuses S_IF* macros
Status: RESOLVED FIXED
Alias: None
Product: xorg
Classification: Unclassified
Component: * Other (show other bugs)
Version: git
Hardware: All All
: high normal
Assignee: Alan Coopersmith
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-11-09 20:49 UTC by Alan Coopersmith
Modified: 2007-10-09 18:59 UTC (History)
1 user (show)

See Also:
i915 platform:
i915 features:


Attachments
Patches to correct use of S_IFMT macros in monolith (2.14 KB, patch)
2005-11-09 20:56 UTC, Alan Coopersmith
no flags Details | Splinter Review

Description Alan Coopersmith 2005-11-09 20:49:53 UTC
There's a few places in the Xorg code where the S_IF* macros are misused, such
as this check in xc/lib/X11/XKBCvt.c:
     if ( (stat(cf,&sbuf)==0) && (sbuf.st_mode&S_IFREG) &&

S_IF* are not pure bitmasks, but values to be compared with mode & S_IFMT.

For instance, on Solaris the values include:
#define S_IFREG         0x8000  /* regular */
#define S_IFLNK         0xA000  /* symbolic link */
#define S_IFSOCK        0xC000  /* socket */
#define S_IFDOOR        0xD000  /* door */
#define S_IFPORT        0xE000  /* event port */

which will all pass the above test as "regular files".

On FreeBSD, these values would pass that test:
#define S_IFREG  0100000                /* regular */
#define S_IFLNK  0120000                /* symbolic link */
#define S_IFSOCK 0140000                /* socket */
#define S_IFWHT  0160000                /* whiteout */

Fortunately, most of the places this is misused is in old code not used any
more since there are #ifdef's to use the OS-provided macros that do the right 
thing if they're available (such as this snippet from xc/lib/Xt/Intrinsic.c
that does the right thing on platforms without X_NOT_POSIX defined):

#ifndef X_NOT_POSIX
            S_ISDIR(status.st_mode) == 0);      /* not a directory */
#else
            (status.st_mode & S_IFDIR) == 0);   /* not a directory */
#endif /* X_NOT_POSIX else */

Offenders found by a find | xargs grep of the Xorg monolith were:
./config/util/checktree.c:    if ((fs->st_mode & S_IFLNK) == S_IFLNK) {
./config/util/checktree.c:    if ((fs->st_mode & S_IFDIR) == S_IFDIR) {
./config/util/checktree.c:    } else if ((fs->st_mode & S_IFREG) != S_IFREG)
./config/util/checktree.c:      if ((fs.st_mode & S_IFDIR) == S_IFDIR) {
./config/util/lndir.c:      if (sb.st_mode & S_IFDIR) 
./config/util/lndir.c:    if (!(ts.st_mode & S_IFDIR))
./config/util/lndir.c:    if (!(fs.st_mode & S_IFDIR))
./lib/X11/XKBCvt.c:     if ( (stat(cf,&sbuf)==0) && (sbuf.st_mode&S_IFREG) &&
./lib/Xt/Intrinsic.c:       (status.st_mode & S_IFDIR) == 0);   /* not a
directory */

checktree appears to be a no longer used distcheck tool for the old monolith
releases and can be ignored.

lndir uses the S_IS* macros instead if they are defined.

The XKBCvt.c is thus the only one we really need to fix, since most modern
platforms have S_IS*, but since it's trivial to fix lndir & Intrinsic.c as
well, a patch to fix all three will follow shortly.   (Will wait to commit
until after RC2 releases are tagged and released.)
Comment 1 Alan Coopersmith 2005-11-09 20:56:56 UTC
Created attachment 3763 [details] [review]
Patches to correct use of S_IFMT macros in monolith
Comment 2 Alan Coopersmith 2005-11-24 09:31:14 UTC
Checked into CVS head:

CVSROOT:	/cvs/xorg
Module name:	xc
Changes by:	alanc@gabe.freedesktop.org	05/11/23 14:33:07

Log message:
  2005-11-23  Alan Coopersmith  <alan.coopersmith@sun.com>
  
  	* lib/X11/XKBCvt.c:
  	* config/util/lndir.c:
  	* lib/Xt/Intrinsic.c:
  	Bug #5003 <https://bugs.freedesktop.org/show_bug.cgi?id=5003>
  	Patch #3763 <https://bugs.freedesktop.org/attachment.cgi?id=3763>
    	Xorg code misuses S_IF* macros

Modified files:
      xc/lib/X11/:
        XKBCvt.c 
      xc/config/util/:
        lndir.c 
      xc/lib/Xt/:
        Intrinsic.c 
      ./:
        ChangeLog 
  
  Revision      Changes    Path
  1.4           +6 -1      xc/lib/X11/XKBCvt.c
  http://cvs.freedesktop.org/xorg/xc/lib/X11/XKBCvt.c
  1.4           +3 -3      xc/config/util/lndir.c
  http://cvs.freedesktop.org/xorg/xc/config/util/lndir.c
  1.5           +1 -1      xc/lib/Xt/Intrinsic.c
  http://cvs.freedesktop.org/xorg/xc/lib/Xt/Intrinsic.c
  1.1535        +9 -0      xc/ChangeLog
  http://cvs.freedesktop.org/xorg/xc/ChangeLog



Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.