xinggangw / leptonica

Automatically exported from code.google.com/p/leptonica
1 stars 0 forks source link

genPathname() in utils.c doesn't correctly handle dir with wrong directory sepchar under Windows #16

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Try running psioseg_reg using Windows. It calls
convertSegmentedPagesToPS("/tmp/junkimagedir", "/tmp/junkmaskdir", 2.0,
0.15, 190, 0, 0, "junkfile.ps") which will eventually fail.

What is the expected output? What do you see instead?
I expect to be able to build concatenated paths.
Instead paths with / and \ are created.

What version of the product are you using? On what operating system?
leptonlib-1.63.
Microsoft Visual Studio 2008 SP1 with latest updates also applied.
Windows XP Pro SP3.

Please provide any additional information below.
I'm not sure the following is the best solution but it works. Perhaps more
thought needs to be done on the whole annoying Windows pathsep issue rather
than piecemeal fixing issues after the fact?

genPathname() should be changed to:

genPathname(const char  *dir,
            const char  *fname)
{
char    *charbuf;
l_int32  dirlen, namelen;
char    *tmpDirname;

    PROCNAME("genPathname");

    if (!dir)
        return (char *)ERROR_PTR("dir not defined", procName, NULL);
    if (!fname)
        return (char *)ERROR_PTR("fname not defined", procName, NULL);

    dirlen = strlen(dir);
    namelen = strlen(fname);
    if ((charbuf = (char *)CALLOC(dirlen + namelen + 10, sizeof(char)))
         == NULL)
        return (char *)ERROR_PTR("charbuf not made", procName, NULL);

#if COMPILER_MSVC
    if (stringFindSubstr(dir, "/", NULL) > 0) {
        tmpDirname = stringReplaceEachSubstr(dir, "/", "\\", NULL);
        strcpy(charbuf, tmpDirname);
        FREE(tmpDirname);
    } else {
        strcpy(charbuf, dir);
    }
#else
    strcpy(charbuf, dir);
#endif

    if (charbuf[dirlen - 1] != sepchar) {
        charbuf[dirlen] = sepchar;
        charbuf[dirlen+1] = '\0';
    }
    strcat(charbuf, fname);

    return charbuf;
}

Original issue reported on code.google.com by tomp2...@gmail.com on 1 Dec 2009 at 8:13

GoogleCodeExporter commented 8 years ago
Thanks.  I've made the changes to utils.c, and tested it.
The file is attached.

Original comment by dan.bloo...@gmail.com on 5 Dec 2009 at 5:59

Attachments:

GoogleCodeExporter commented 8 years ago
This fix will be in 1.64.

Original comment by dan.bloo...@gmail.com on 5 Dec 2009 at 6:00

GoogleCodeExporter commented 8 years ago
Just took a look at your change to genPathname() in utils.c

You do:

    dirlen = strlen(charbuf);
    if (charbuf[dirlen - 1] != sepchar)  /* append sepchar */
        charbuf[dirlen] = sepchar;
    strncat(charbuf, fname, namelen);
    return charbuf;

Offhand, that looks to me like you clobber charbuf's trailing null with 
sepchar? In
which case, strncat is going to be very confused. I think you still need my:

        charbuf[dirlen+1] = '\0';

Original comment by tomp2...@gmail.com on 8 Dec 2009 at 8:47

GoogleCodeExporter commented 8 years ago
I don't think so.  The calloc initialized charbuf to 0.
After copying the (possibly munged) directory name, and appending
the sepchar, all the remaining chars in charbuf are still 0.
That's why I left it off.

Original comment by dan.bloo...@gmail.com on 8 Dec 2009 at 10:19

GoogleCodeExporter commented 8 years ago
Fixed in 1.64

Original comment by dan.bloo...@gmail.com on 3 Jan 2010 at 11:31