Mercurial > projects > libpws
comparison pws-file.c @ 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 | d541e748cfd8 |
children |
comparison
equal
deleted
inserted
replaced
8:956e97003bad | 9:ec5c1b653ee6 |
---|---|
1144 size_t i; | 1144 size_t i; |
1145 struct pws3_field *version_field; | 1145 struct pws3_field *version_field; |
1146 struct pws3_field *field = NULL; | 1146 struct pws3_field *field = NULL; |
1147 struct pws3_field *end_field = NULL; | 1147 struct pws3_field *end_field = NULL; |
1148 | 1148 |
1149 end_field = pws3_field_create(1, PWS3_HEADER_FIELD_END); | |
1150 if (end_field == NULL) { | |
1151 pws_set_system_error(ctx->pws_file, PWS_ERR_NO_MEMORY, errno, | |
1152 NULL); | |
1153 goto out; | |
1154 } | |
1155 | |
1149 version_field = pws3_file_get_header_field(ctx->pws_file, | 1156 version_field = pws3_file_get_header_field(ctx->pws_file, |
1150 PWS3_HEADER_FIELD_VERSION); | 1157 PWS3_HEADER_FIELD_VERSION); |
1151 if (version_field == NULL) { | 1158 if (version_field == NULL) { |
1152 /* add mandatory version header version_field if necessary */ | 1159 /* add mandatory version header version_field if necessary */ |
1153 version_field = pws3_field_create(1, PWS3_HEADER_FIELD_VERSION); | 1160 version_field = pws3_field_create(1, PWS3_HEADER_FIELD_VERSION); |
1178 if (retval != 0) { | 1185 if (retval != 0) { |
1179 goto out; | 1186 goto out; |
1180 } | 1187 } |
1181 } | 1188 } |
1182 | 1189 |
1183 end_field = pws3_field_create(1, PWS3_HEADER_FIELD_END); | |
1184 retval = write_field(ctx, end_field); | 1190 retval = write_field(ctx, end_field); |
1185 if (retval != 0) { | 1191 if (retval != 0) { |
1186 goto out; | 1192 goto out; |
1187 } | 1193 } |
1188 | 1194 |