From 8bc26371882b7ebaaa25ca0c2ce81b1f719daeea Mon Sep 17 00:00:00 2001 From: Eduardo Rocha Date: Fri, 24 Mar 2006 14:36:27 +0000 Subject: Fix a SEGFAULT that happens when textfile_del is called using an empty file. --- common/test_textfile.c | 22 ++++++++++++++++++++++ common/textfile.c | 6 ++++-- 2 files changed, 26 insertions(+), 2 deletions(-) (limited to 'common') diff --git a/common/test_textfile.c b/common/test_textfile.c index 5a5b22c5..8b60bbe8 100644 --- a/common/test_textfile.c +++ b/common/test_textfile.c @@ -48,6 +48,28 @@ int main(int argc, char *argv[]) fd = creat(filename, 0644); close(fd); + sprintf(key, "00:00:00:00:00:00"); + if (textfile_del(filename, key) < 0) + fprintf(stderr, "%s (%d)\n", strerror(errno), errno); + + memset(value, 0, sizeof(value)); + if (textfile_put(filename, key, value) < 0) + fprintf(stderr, "%s (%d)\n", strerror(errno), errno); + + str = textfile_get(filename, key); + if (!str) + fprintf(stderr, "No value for %s\n", key); + else + free(str); + + if (textfile_del(filename, key) < 0) + fprintf(stderr, "%s (%d)\n", strerror(errno), errno); + + str = textfile_get(filename, key); + if (str) { + fprintf(stderr, "Found value for %s\n", key); + free(str); + } for (i = 1; i < max + 1; i++) { sprintf(key, "00:00:00:00:00:%02X", i); diff --git a/common/textfile.c b/common/textfile.c index bb55109c..84a72b3c 100644 --- a/common/textfile.c +++ b/common/textfile.c @@ -143,8 +143,10 @@ static int write_key(const char *pathname, const char *key, const char *value) size = st.st_size; if (!size) { - pos = lseek(fd, size, SEEK_SET); - err = write_key_value(fd, key, value); + if (value) { + pos = lseek(fd, size, SEEK_SET); + err = write_key_value(fd, key, value); + } goto unlock; } -- cgit