]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldbm/nextid.c
Import minor trace output cleanup
[openldap] / servers / slapd / back-ldbm / nextid.c
index e51f4d84e76ce4de24f4dc6a4819befdc4cc3deb..63c7ed4d89e62cfbd146dea13c5dced424ab5825 100644 (file)
@@ -1,9 +1,15 @@
 /* id.c - keep track of the next id to be given out */
 
+#include "portable.h"
+
 #include <stdio.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+
+#include <ac/socket.h>
+
+#ifdef HAVE_SYS_PARAM_H
 #include <sys/param.h>
+#endif
+
 #include "slap.h"
 #include "back-ldbm.h"
 
@@ -19,43 +25,55 @@ next_id( Backend *be )
        sprintf( buf, "%s/NEXTID", li->li_directory );
 
        pthread_mutex_lock( &li->li_nextid_mutex );
+
        /* first time in here since startup - try to read the nexid */
        if ( li->li_nextid == -1 ) {
                if ( (fp = fopen( buf, "r" )) == NULL ) {
                        Debug( LDAP_DEBUG_ANY,
-                           "next_id %d: could not open \"%s\"\n",
+                           "next_id %lu: could not open \"%s\"\n",
                            li->li_nextid, buf, 0 );
                        li->li_nextid = 1;
+
                } else {
                        if ( fgets( buf2, sizeof(buf2), fp ) != NULL ) {
                                li->li_nextid = atol( buf2 );
+
+                               if(li->li_nextid < 1) {
+                                       /* protect against bad data */
+                                       Debug( LDAP_DEBUG_ANY,
+                                       "next_id %lu: atol(%s) return non-positive integer\n",
+                                               li->li_nextid, buf2, 0 );
+                                       li->li_nextid = 1;
+                               }
+
                        } else {
                                Debug( LDAP_DEBUG_ANY,
-                           "next_id %d: could not fgets nextid from \"%s\"\n",
+                          "next_id %lu: could not fgets nextid from \"%s\"\n",
                                    li->li_nextid, buf2, 0 );
                                li->li_nextid = 1;
                        }
+
                        fclose( fp );
                }
        }
 
-       li->li_nextid++;
+       id = li->li_nextid++;
+
        if ( (fp = fopen( buf, "w" )) == NULL ) {
-               Debug( LDAP_DEBUG_ANY, "next_id %d: could not open \"%s\"\n",
+               Debug( LDAP_DEBUG_ANY, "next_id %lu: could not open \"%s\"\n",
                    li->li_nextid, buf, 0 );
        } else {
                if ( fprintf( fp, "%ld\n", li->li_nextid ) == EOF ) {
-                       Debug( LDAP_DEBUG_ANY, "next_id %d: cannot fprintf\n",
+                       Debug( LDAP_DEBUG_ANY, "next_id %lu: cannot fprintf\n",
                            li->li_nextid, 0, 0 );
                }
                if( fclose( fp ) != 0 ) {
-                       Debug( LDAP_DEBUG_ANY, "next_id %d: cannot fclose\n",
+                       Debug( LDAP_DEBUG_ANY, "next_id %lu: cannot fclose\n",
                            li->li_nextid, 0, 0 );
                }
        }
-       id = li->li_nextid - 1;
-       pthread_mutex_unlock( &li->li_nextid_mutex );
 
+       pthread_mutex_unlock( &li->li_nextid_mutex );
        return( id );
 }
 
@@ -67,6 +85,7 @@ next_id_return( Backend *be, ID id )
        FILE            *fp;
 
        pthread_mutex_lock( &li->li_nextid_mutex );
+
        if ( id != li->li_nextid - 1 ) {
                pthread_mutex_unlock( &li->li_nextid_mutex );
                return;
@@ -77,17 +96,17 @@ next_id_return( Backend *be, ID id )
        li->li_nextid--;
        if ( (fp = fopen( buf, "w" )) == NULL ) {
                Debug( LDAP_DEBUG_ANY,
-                   "next_id_return of %d: could not open \"%s\" next id %d\n",
+                 "next_id_return of %lu: could not open \"%s\" next id %lu\n",
                    id, buf, li->li_nextid );
        } else {
                if ( fprintf( fp, "%ld\n", li->li_nextid ) == EOF ) {
                        Debug( LDAP_DEBUG_ANY,
-                   "next_id_return of %d: cannot fprintf \"%s\" next id %d\n",
+                 "next_id_return of %lu: cannot fprintf \"%s\" next id %lu\n",
                            id, buf, li->li_nextid );
                }
                if( fclose( fp ) != 0 ) {
                        Debug( LDAP_DEBUG_ANY,
-                   "next_id_return of %d: cannot fclose \"%s\" next id %d\n",
+                 "next_id_return of %lu: cannot fclose \"%s\" next id %lu\n",
                            id, buf, li->li_nextid );
                }
        }
@@ -106,26 +125,39 @@ next_id_get( Backend *be )
        sprintf( buf, "%s/NEXTID", li->li_directory );
 
        pthread_mutex_lock( &li->li_nextid_mutex );
+
        /* first time in here since startup - try to read the nexid */
        if ( li->li_nextid == -1 ) {
                if ( (fp = fopen( buf, "r" )) == NULL ) {
                        Debug( LDAP_DEBUG_ANY,
-                           "next_id %d: could not open \"%s\"\n",
+                           "next_id_get %lu: could not open \"%s\"\n",
                            li->li_nextid, buf, 0 );
                        li->li_nextid = 1;
+
                } else {
                        if ( fgets( buf2, sizeof(buf2), fp ) != NULL ) {
                                li->li_nextid = atol( buf2 );
+
+                               if(li->li_nextid < 1) {
+                                       /* protect against bad data */
+                                       Debug( LDAP_DEBUG_ANY,
+                                       "next_id_get %lu: atol(%s) return non-positive integer\n",
+                                               li->li_nextid, buf2, 0 );
+                                       li->li_nextid = 1;
+                               }
+
                        } else {
                                Debug( LDAP_DEBUG_ANY,
-                           "next_id %d: cannot fgets nextid from \"%s\"\n",
+                           "next_id_get %lu: cannot fgets nextid from \"%s\"\n",
                                    li->li_nextid, buf2, 0 );
                                li->li_nextid = 1;
                        }
                        fclose( fp );
                }
        }
+
        id = li->li_nextid;
+
        pthread_mutex_unlock( &li->li_nextid_mutex );
 
        return( id );