Fix bug 7045 - Bad (non memory copying) interfaces in smbc_setXXXX calls.
authorJeremy Allison <jra@samba.org>
Sat, 16 Jan 2010 01:22:35 +0000 (17:22 -0800)
committerJeremy Allison <jra@samba.org>
Sat, 16 Jan 2010 01:22:35 +0000 (17:22 -0800)
In smbc_free_context libsmbclient just called free() on the string options
so it assumes the callers have malloced them before setting them via smbc_set
calls.

Change to corretly malloc/free string options to the library.

Jeremy

source3/libsmb/libsmb_context.c
source3/libsmb/libsmb_setget.c

index 78c9a551a688727764cadc9c31c2f230f7d91e21..336172ce6fba80e51898a9567ac5258d8f3254a9 100644 (file)
@@ -291,13 +291,8 @@ smbc_free_context(SMBCCTX *context,
         }
         
         /* Things we have to clean up */
-        free(smbc_getWorkgroup(context));
         smbc_setWorkgroup(context, NULL);
-
-        free(smbc_getNetbiosName(context));
         smbc_setNetbiosName(context, NULL);
-
-        free(smbc_getUser(context));
         smbc_setUser(context, NULL);
         
         DEBUG(3, ("Context %p successfully freed\n", context));
@@ -533,7 +528,6 @@ SMBCCTX *
 smbc_init_context(SMBCCTX *context)
 {
         int pid;
-        char *user = NULL;
         
         if (!context) {
                 errno = EBADF;
@@ -569,7 +563,7 @@ smbc_init_context(SMBCCTX *context)
                 /*
                  * FIXME: Is this the best way to get the user info?
                  */
-                user = getenv("USER");
+               char *user = getenv("USER");
                 /* walk around as "guest" if no username can be found */
                 if (!user) {
                         user = SMB_STRDUP("guest");
@@ -583,6 +577,12 @@ smbc_init_context(SMBCCTX *context)
                 }
 
                 smbc_setUser(context, user);
+               SAFE_FREE(user);
+
+               if (!smbc_getUser(context)) {
+                        errno = ENOMEM;
+                        return NULL;
+                }
         }
         
         if (!smbc_getNetbiosName(context)) {
@@ -615,6 +615,12 @@ smbc_init_context(SMBCCTX *context)
                 }
                 
                 smbc_setNetbiosName(context, netbios_name);
+               SAFE_FREE(netbios_name);
+
+                if (!smbc_getNetbiosName(context)) {
+                        errno = ENOMEM;
+                        return NULL;
+                }
         }
         
         DEBUG(1, ("Using netbios name %s.\n", smbc_getNetbiosName(context)));
@@ -636,6 +642,12 @@ smbc_init_context(SMBCCTX *context)
                 }
 
                 smbc_setWorkgroup(context, workgroup);
+               SAFE_FREE(workgroup);
+
+               if (!smbc_getWorkgroup(context)) {
+                       errno = ENOMEM;
+                       return NULL;
+               }
         }
         
         DEBUG(1, ("Using workgroup %s.\n", smbc_getWorkgroup(context)));
index fc3f321497901b7cd153defaf41811910f8c6969..3ef707863f13eea9fa4da330bcdc9a4c88ccf65a 100644 (file)
@@ -39,7 +39,8 @@ smbc_getNetbiosName(SMBCCTX *c)
 void
 smbc_setNetbiosName(SMBCCTX *c, char * netbios_name)
 {
-        c->netbios_name = netbios_name;
+       SAFE_FREE(c->netbios_name);
+       c->netbios_name = SMB_STRDUP(netbios_name);
 }
 
 /** Get the workgroup used for making connections */
@@ -53,7 +54,8 @@ smbc_getWorkgroup(SMBCCTX *c)
 void
 smbc_setWorkgroup(SMBCCTX *c, char * workgroup)
 {
-        c->workgroup = workgroup;
+       SAFE_FREE(c->workgroup);
+       c->workgroup = SMB_STRDUP(workgroup);
 }
 
 /** Get the username used for making connections */
@@ -67,7 +69,8 @@ smbc_getUser(SMBCCTX *c)
 void
 smbc_setUser(SMBCCTX *c, char * user)
 {
-        c->user = user;
+       SAFE_FREE(c->user);
+       c->user = SMB_STRDUP(user);
 }
 
 /** Get the debug level */