ldb: detect eof on ldif files
authorAndrew Tridgell <tridge@samba.org>
Tue, 29 Mar 2011 05:31:17 +0000 (16:31 +1100)
committerAndrew Tridgell <tridge@samba.org>
Tue, 29 Mar 2011 06:24:03 +0000 (08:24 +0200)
use feof() to detect parsing errors in ldif files

Autobuild-User: Andrew Tridgell <tridge@samba.org>
Autobuild-Date: Tue Mar 29 08:24:04 CEST 2011 on sn-devel-104

source4/lib/ldb/tools/ldbedit.c
source4/lib/ldb/tools/ldbmodify.c

index 36d054e563954df0fe03b2be01742e608056f519..aaf6d80352d804b8a652f51786799cbd38b4cc89 100644 (file)
@@ -281,6 +281,21 @@ static int do_edit(struct ldb_context *ldb, struct ldb_message **msgs1,
                msgs2[count2++] = ldif->msg;
        }
 
+       /* the feof() test works here, even for the last line of the
+        * file, as we parse ldif files character by character, and
+        * feof() is only true if we have failed to read a character
+        * from the file. So if the last line is bad, we don't get
+        * feof() set, so we know the record was bad. Only if we
+        * attempt to go to the next record will we get feof() and
+        * thus consider that the ldif has ended without errors
+        */
+       if (!feof(f)) {
+               fprintf(stderr, "Error parsing ldif - aborting\n");
+               fclose(f);
+               unlink(file_template);
+               return -1;
+       }
+
        fclose(f);
        unlink(file_template);
 
index 1484bbf6550a2c551260144d5b10d8a798c944b8..1374765f57540f5cfd8abbc12af75e01266fa9c3 100644 (file)
@@ -87,6 +87,11 @@ static int process_file(struct ldb_context *ldb, FILE *f, unsigned int *count)
                ldb_ldif_read_free(ldb, ldif);
        }
 
+       if (!feof(f)) {
+               fprintf(stderr, "Failed to parse ldif\n");
+               return -1;
+       }
+
        return ret;
 }