[Date Prev][Date Next] [Chronological] [Thread] [Top]

(ITS#4900) command-line utils crash

Full_Name: Chris Ridd
Version: 2.3.34
OS: Windows
URL: ftp://ftp.openldap.org/incoming/
Submission from: (NULL) (

On Windows, the ldapadd tool often crashes while parsing the command-line

This is due to a defect in the way the optarg (et al) variables are declared in
ac/unistd.h. When tools/common.c includes that file, the 4 variables end up
being declared as being "imported" from a DLL. However they're *actually*
present in a static archive that's linked into the ldapadd executable, which is
apparently not the same sort of thing. (Visual Studio's compiler warns about
inconsistent linkage :-)

As a result, the code in getopt.c thinks that optarg is a variable at one
address, and the code calling getopt() think that optarg is a variable at a
*different* address (NULL in my test rig). Clearly that's not right, and it
causes a variety of issues:

-D <dn> copies optarg using strdup(NULL), which returns NULL.

-w <passwd> scribbles over the passwd string, which crashes doing a *optarg
because optarg is NULL.

A simple fix is to redeclare the 4 variables as just "extern" instead of
LDAP_LIBC_V. Diff follows...

--- openldap-2.3.34/include/ac/unistd.h.orig    Fri Mar 30 15:05:49 2007
+++ openldap-2.3.34/include/ac/unistd.h Fri Mar 30 15:06:10 2007
@@ -54,8 +54,8 @@

        /* assume we need to declare these externs */
-       LDAP_LIBC_V (char *) optarg;
-       LDAP_LIBC_V (int) optind, opterr, optopt;
+       extern char * optarg;
+       extern int optind, opterr, optopt;

 /* use lutil file locking */