Browse Source

Remove 3 leaks

Julien 'Lta' BALLET 11 years ago
parent
commit
84bbfefcf8
8 changed files with 59 additions and 7 deletions
  1. 1 1
      Makefile.am
  2. 2 0
      bin/dsm.c
  3. 5 4
      include/bdsm/smb_fd.h
  4. 12 2
      include/bdsm/smb_stat.h
  5. 26 0
      src/smb_fd.c
  6. 4 0
      src/smb_file.c
  7. 3 0
      src/smb_session.c
  8. 6 0
      src/smb_stat.c

+ 1 - 1
Makefile.am

@@ -6,7 +6,7 @@ EXTRA_DIST = \
 	contrib/spnego/spnego.asn1
 	contrib/spnego/spnego_asn1.c
 
-CFLAGS = @CFLAGS@ -I$(top_srcdir)/contrib -I$(top_srcdir)/include @TASN1_CFLAGS@
+CFLAGS = -I$(top_srcdir)/contrib -I$(top_srcdir)/include @TASN1_CFLAGS@ @CFLAGS@
 
 if DEBUG
 AM_CFLAGS = -O0 -g3 -Wall -Wextra #-Werror

+ 2 - 0
bin/dsm.c

@@ -215,9 +215,11 @@ int main(int ac, char **av)
     }
   else
     fprintf(stderr, "Unable to list files\n");
+  smb_stat_list_destroy(files);
 
   fprintf(stderr, "Query file info for path: %s\n", fname);
   files = smb_fstat(session, test, fname);
+  smb_stat_destroy(files);
 
   if (files != NULL)
     printf("File '%s' is %"PRIu64" bytes long\n", fname, files->size);

+ 5 - 4
include/bdsm/smb_fd.h

@@ -22,11 +22,12 @@
 #include "bdsm/smb_session.h"
 
 void            smb_session_share_add(smb_session *s, smb_share *share);
-smb_share     *smb_session_share_get(smb_session *s, smb_tid tid);
-smb_share     *smb_session_share_remove(smb_session *s, smb_tid tid);
+smb_share       *smb_session_share_get(smb_session *s, smb_tid tid);
+smb_share       *smb_session_share_remove(smb_session *s, smb_tid tid);
+void            smb_session_share_clear(smb_session *s);
 
 int             smb_session_file_add(smb_session *s, smb_tid tid, smb_file *f);
-smb_file      *smb_session_file_get(smb_session *s, smb_fd fd);
-smb_file      *smb_session_file_remove(smb_session *s, smb_fd fd);
+smb_file        *smb_session_file_get(smb_session *s, smb_fd fd);
+smb_file        *smb_session_file_remove(smb_session *s, smb_fd fd);
 
 #endif

+ 12 - 2
include/bdsm/smb_stat.h

@@ -64,7 +64,9 @@ smb_stat_list   smb_find(smb_session *s, smb_tid tid, const char *pattern);
  * @param tid The tree id of a share obtained by smb_tree_connect()
  * @param path The full path of the file relative to the root of the share
  * (e.g. '\\folder\\file.ext')
- * @return An opaque smb_stat or NULL in case of error
+ *
+ * @return An opaque smb_stat or NULL in case of error. You need to
+ * destory this object with smb_stat_destroy after usage.
  */
 smb_stat        smb_fstat(smb_session *s, smb_tid tid, const char *path);
 
@@ -75,11 +77,19 @@ smb_stat        smb_fstat(smb_session *s, smb_tid tid, const char *path);
  * @param s The session object
  * @param fd The smb_fd from which you want infos/status
  *
- * @return  An opaque smb_stat or NULL in case of error
+ * @return An opaque smb_stat or NULL in case of error. You don't own
+ * this object memory, and then don't have to destory it
  */
 smb_stat        smb_stat_fd(smb_session *s, smb_fd fd);
 
 /**
+ * @brief Clear a smb_stat object, reclaiming its memory
+ *
+ * @param stat A smb_stat object returned by smb_fstat. 
+ */
+void            smb_stat_destroy(smb_stat stat);
+
+/**
  * @brief Get the number of item in a smb_stat_list file info
  *
  * @param list The list you want the length of

+ 26 - 0
src/smb_fd.c

@@ -78,6 +78,32 @@ smb_share *smb_session_share_remove(smb_session *s, smb_tid tid)
     return (NULL);
 }
 
+void            smb_session_share_clear(smb_session *s)
+{
+    smb_share   *iter, *tmp;
+    smb_file    *fiter, *ftmp;
+
+    assert(s != NULL);
+
+    iter = s->shares;
+    while(iter != NULL)
+    {        
+        fiter = iter->files;
+        while(fiter != NULL)
+        {
+            ftmp = fiter;
+            fiter = fiter->next;
+
+            free(ftmp->name);
+            free(ftmp);
+        }   
+
+        tmp = iter;
+        iter = iter->next;
+        free(tmp);
+    }
+}
+
 int         smb_session_file_add(smb_session *s, smb_tid tid, smb_file *f)
 {
     smb_share *share;

+ 4 - 0
src/smb_file.c

@@ -129,6 +129,10 @@ void        smb_fclose(smb_session *s, smb_fd fd)
     // care about creating a potentiel leak server side.
     smb_session_send_msg(s, msg);
     smb_session_recv_msg(s, 0);
+    smb_message_destroy(msg);
+
+    free(file->name);
+    free(file);
 }
 
 ssize_t   smb_fread(smb_session *s, smb_fd fd, void *buf, size_t buf_size)

+ 3 - 0
src/smb_session.c

@@ -24,6 +24,7 @@
 #include "bdsm/debug.h"
 #include "bdsm/smb_session.h"
 #include "bdsm/smb_session_msg.h"
+#include "bdsm/smb_fd.h"
 #include "bdsm/smb_ntlm.h"
 #include "bdsm/smb_spnego.h"
 #include "bdsm/smb_transport.h"
@@ -61,6 +62,8 @@ void            smb_session_destroy(smb_session *s)
 {
     if (s != NULL)
     {
+        smb_session_share_clear(s);
+
         // FIXME Free smb_share and smb_file
         if (s->transport.session != NULL)
         {

+ 6 - 0
src/smb_stat.c

@@ -28,6 +28,11 @@ smb_stat        smb_stat_fd(smb_session *s, smb_fd fd)
     return (smb_session_file_get(s, fd));
 }
 
+void            smb_stat_destroy(smb_stat stat) 
+{
+    smb_stat_list_destroy((smb_stat_list) stat); 
+}
+
 size_t            smb_stat_list_count(smb_stat_list list)
 {
     size_t          count = 0;
@@ -48,6 +53,7 @@ void            smb_stat_list_destroy(smb_stat_list list)
     while(list != NULL)
     {
         tmp = list->next;
+        free(list->name);
         free(list);
         list = tmp;
     }