[comp.lang.c] QuickC bug

walt_waldo_novinger@cup.portal.com (01/02/88)

Note: this may be a double posting -- out gateway was down for
a couple of days.
Walt


I have found and reported to MicroSoft the following bugs in the
QuickC v1.0 `_splitpath` function and its documentation. The
code fragment below illustrates the code which will reproduce
the bug. The following describes the problems:

    1) The documentation in the "Run-Time Library Reference",
       pp 559-60 contains several errors. The Summary indicates
       that `stdlib.h` must be included, while the example shows
       `dos.h`; `stdlib.h` is correct.

    2) The example program shows the returned variables declared
       as "char * var [n]". The example program below shows the
       *correct* declaration syntax.

    3) The bug, which can be observed under the debugger, is
       that the first character of the `prog` variable is left
       as a null. For instance, if the string "d:\\tmp\\bazfaz.exe"
       is fed to the function, `prog` will contain "\0azfaz\0\0\0"
       on return. The rest of the variables are correctly set.

Items 1) and 2) above apply to the documentation for `_makepath`
as well (pp 407-08); `_makepath` *does* work as documented, however.

I am awaiting a response from MicroSoft vis-a-vis a patch
(though the tech with whom I spoke indicated that they probably
won't release one). If I receive one or any hard info on a fix,
I'll post it here.

========================= Example Program =================================
#include <stdio.h>
#include <stdlib.h>

/* get program name from DOS, extract base name and convert to lowercase */

char *progname(pathname)
char pathname[40];
{
        char drive[3];  /* note that `*` is *incorrect* */
        char dir[60];
        char prog[9];
        char ext[4];
        char *result;

        _splitpath(pathname, drive, dir, prog, ext);
        result = strlwr(prog);                      /* convert to lowercase */
        return (result);
}


main(argc, argv)
int argc;
char *argv[];
{
        char *pro;

        pro = progname(argv[0]);        /* extract program name w/o extension */

        [...]

        exit (0);
}
========================================================================
Walt Novinger            sun!cup.portal.com!waldo    "The real world is
680 Sylvan Ave. #17      waldo@cup.portal.com         NOT user-friendly"
Mountain View, CA 94041  (415) 964-9377                 Kelvin Throop
========================================================================
       

mik@hpopda.HP.COM (Mik Butler) (01/11/88)

Walt Novinger writes : 
> 
> I have found and reported to MicroSoft the following bugs in the
> QuickC v1.0 `_splitpath` function and its documentation. The
> code fragment below illustrates the code which will reproduce
> the bug. The following describes the problems:
	.
	.
	.
>     3) The bug, which can be observed under the debugger, is
>        that the first character of the `prog` variable is left
>        as a null. For instance, if the string "d:\\tmp\\bazfaz.exe"
>        is fed to the function, `prog` will contain "\0azfaz\0\0\0"
>        on return. The rest of the variables are correctly set.
> 
> ========================= Example Program =================================
> #include <stdio.h>
> #include <stdlib.h>
> 
> /* get program name from DOS, extract base name and convert to lowercase */
> 
> char *progname(pathname)
> char pathname[40];
> {
>         char drive[3];  /* note that `*` is *incorrect* */
>         char dir[60];
>         char prog[9];
>         char ext[4];
>         char *result;
> 
>         _splitpath(pathname, drive, dir, prog, ext);
>         result = strlwr(prog);                      /* convert to lowercase */
>         return (result);
> }
> 
> 
> main(argc, argv)
> int argc;
> char *argv[];
> {
>         char *pro;
> 
>         pro = progname(argv[0]);        /* extract program name w/o extension */
	.
	.

The reason that this fails isn't because _splitpath is broken, its because
progname is returning a pointer to something that is no longer in scope.
The space for prog is allocated on the stack for the lifetime of progname,
so pro will point to the space where prog was (and now isn't).

If the declaration is changed to 'static char prog[9];', then
the example program will work.
Alternately, make the declaration 'char prog[9];' global.

----------------------------------------
Mik Butler