]> git.sur5r.net Git - openldap/blobdiff - libraries/liblutil/passfile.c
Happy new year (belated)
[openldap] / libraries / liblutil / passfile.c
index 6de61aba963a4f056edae57773decee5136d2e27..1b1bad732eac1db9bd40e4959aeb114a630ca165 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2003 The OpenLDAP Foundation.
+ * Copyright 1998-2014 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -44,7 +44,7 @@ lutil_get_filed_password(
        }
 
        passwd->bv_val = NULL;
-       passwd->bv_len = 4196;
+       passwd->bv_len = 4096;
 
 #ifdef HAVE_FSTAT
        {
@@ -56,14 +56,16 @@ lutil_get_filed_password(
                                        filename );
                        }
 
-                       passwd->bv_len = sb.st_size;
+                       if ( sb.st_size )
+                               passwd->bv_len = sb.st_size;
                }
        }
 #endif /* HAVE_FSTAT */
 
-       passwd->bv_val = (char *) malloc( passwd->bv_len + 1 );
+       passwd->bv_val = (char *) ber_memalloc( passwd->bv_len + 1 );
        if( passwd->bv_val == NULL ) {
                perror( filename );
+               fclose( f );
                return -1;
        }
 
@@ -72,12 +74,13 @@ lutil_get_filed_password(
        do {
                if( nleft == 0 ) {
                        /* double the buffer size */
-                       char *p = (char *) realloc( passwd->bv_val,
+                       char *p = (char *) ber_memrealloc( passwd->bv_val,
                                2 * passwd->bv_len + 1 );
                        if( p == NULL ) {
-                               free( passwd->bv_val );
+                               ber_memfree( passwd->bv_val );
                                passwd->bv_val = NULL;
                                passwd->bv_len = 0;
+                               fclose( f );
                                return -1;
                        }
                        nleft = passwd->bv_len;
@@ -88,9 +91,10 @@ lutil_get_filed_password(
                nr = fread( &passwd->bv_val[nread], 1, nleft, f );
 
                if( nr < nleft && ferror( f ) ) {
-                       free( passwd->bv_val );
+                       ber_memfree( passwd->bv_val );
                        passwd->bv_val = NULL;
                        passwd->bv_len = 0;
+                       fclose( f );
                        return -1;
                }