From bd9b2727ef1cb63630c58afe1bba2f4f93246180 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 29 Mar 2011 16:31:17 +1100 Subject: ldb: detect eof on ldif files use feof() to detect parsing errors in ldif files Autobuild-User: Andrew Tridgell Autobuild-Date: Tue Mar 29 08:24:04 CEST 2011 on sn-devel-104 --- source4/lib/ldb/tools/ldbedit.c | 15 +++++++++++++++ source4/lib/ldb/tools/ldbmodify.c | 5 +++++ 2 files changed, 20 insertions(+) (limited to 'source4') diff --git a/source4/lib/ldb/tools/ldbedit.c b/source4/lib/ldb/tools/ldbedit.c index 36d054e563..aaf6d80352 100644 --- a/source4/lib/ldb/tools/ldbedit.c +++ b/source4/lib/ldb/tools/ldbedit.c @@ -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); diff --git a/source4/lib/ldb/tools/ldbmodify.c b/source4/lib/ldb/tools/ldbmodify.c index 1484bbf655..1374765f57 100644 --- a/source4/lib/ldb/tools/ldbmodify.c +++ b/source4/lib/ldb/tools/ldbmodify.c @@ -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; } -- cgit