projects/libpws

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 Jul 30 14:52:29 2019 +0200 (2019-07-30)
parents 956e97003bad
children 0feba0fea9d0
files pws-file.c
line diff
     1.1 --- a/pws-file.c	Tue Jul 30 14:42:35 2019 +0200
     1.2 +++ b/pws-file.c	Tue Jul 30 14:52:29 2019 +0200
     1.3 @@ -1146,6 +1146,13 @@
     1.4  	struct pws3_field *field = NULL;
     1.5  	struct pws3_field *end_field = NULL;
     1.6  
     1.7 +	end_field = pws3_field_create(1, PWS3_HEADER_FIELD_END);
     1.8 +	if (end_field == NULL) {
     1.9 +		pws_set_system_error(ctx->pws_file, PWS_ERR_NO_MEMORY, errno,
    1.10 +		    NULL);
    1.11 +		goto out;
    1.12 +	}
    1.13 +
    1.14  	version_field = pws3_file_get_header_field(ctx->pws_file,
    1.15  	    PWS3_HEADER_FIELD_VERSION);
    1.16  	if (version_field == NULL) {
    1.17 @@ -1180,7 +1187,6 @@
    1.18  		}
    1.19  	}
    1.20  
    1.21 -	end_field = pws3_field_create(1, PWS3_HEADER_FIELD_END);
    1.22  	retval = write_field(ctx, end_field);
    1.23  	if (retval != 0) {
    1.24  		goto out;