changeset 9:ec5c1b653ee6

Prevent NULL pointer dereference Creating the end field may fail, thus try to allocate it in advance.
author Guido Berhoerster <guido+libpws@berhoerster.name>
date Tue, 30 Jul 2019 14:52:29 +0200
parents 956e97003bad
children 0feba0fea9d0
files pws-file.c
diffstat 1 files changed, 7 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/pws-file.c	Tue Jul 30 14:42:35 2019 +0200
+++ b/pws-file.c	Tue Jul 30 14:52:29 2019 +0200
@@ -1146,6 +1146,13 @@
 	struct pws3_field *field = NULL;
 	struct pws3_field *end_field = NULL;
 
+	end_field = pws3_field_create(1, PWS3_HEADER_FIELD_END);
+	if (end_field == NULL) {
+		pws_set_system_error(ctx->pws_file, PWS_ERR_NO_MEMORY, errno,
+		    NULL);
+		goto out;
+	}
+
 	version_field = pws3_file_get_header_field(ctx->pws_file,
 	    PWS3_HEADER_FIELD_VERSION);
 	if (version_field == NULL) {
@@ -1180,7 +1187,6 @@
 		}
 	}
 
-	end_field = pws3_field_create(1, PWS3_HEADER_FIELD_END);
 	retval = write_field(ctx, end_field);
 	if (retval != 0) {
 		goto out;