Skip to content

Commit 3d68740

Browse files
committed
Make s_free() take a void** and set that one to NULL after free'ing
So we don't have to do that manually.
1 parent 6702754 commit 3d68740

2 files changed

Lines changed: 29 additions & 23 deletions

File tree

src/pk/asn1/x509/x509_extensions.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,31 @@ static LTC_INLINE int s_get_element_(const st_oid_detail* details, unsigned long
6262

6363
#ifndef S_FREE
6464
#define S_FREE
65-
#define s_free(p) s_free_((void*) p)
66-
static LTC_INLINE void s_free_(void* p)
65+
#define s_free(p) s_free_((void**) &(p))
66+
static LTC_INLINE void s_free_(void** p)
6767
{
68-
if (p == NULL) {
68+
if (p == NULL || *p == NULL) {
6969
return;
7070
}
71-
XFREE(p);
71+
XFREE(*p);
72+
*p = NULL;
7273
}
7374
#endif
7475

7576
#ifndef S_FREE_X509_STRING_ARRAY
7677
#define S_FREE_X509_STRING_ARRAY
77-
#define s_free_x509_string_array(s, n) s_free_x509_string_array_((ltc_x509_string*)s, n)
78-
static LTC_INLINE void s_free_x509_string_array_(ltc_x509_string *strings, unsigned long num)
78+
#define s_free_x509_string_array(s, n) s_free_x509_string_array_((ltc_x509_string**)&(s), n)
79+
static LTC_INLINE void s_free_x509_string_array_(ltc_x509_string **strings_, unsigned long num)
7980
{
8081
unsigned long n;
82+
ltc_x509_string *strings;
83+
if (strings_ == NULL)
84+
return;
85+
strings = *strings_;
8186
for (n = num; n --> 0;) {
8287
s_free(strings[n].str);
8388
}
84-
s_free(strings);
89+
s_free_((void**)strings_);
8590
}
8691
#endif
8792

src/pk/asn1/x509/x509_import.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -163,28 +163,34 @@ static LTC_INLINE int s_x509_get_name_process(const ltc_asn1_list *set, st_oid_d
163163
return CRYPT_OK;
164164
}
165165

166+
166167
#ifndef S_FREE
167168
#define S_FREE
168-
#define s_free(p) s_free_((void*) p)
169-
static LTC_INLINE void s_free_(void* p)
169+
#define s_free(p) s_free_((void**) &(p))
170+
static LTC_INLINE void s_free_(void** p)
170171
{
171-
if (p == NULL) {
172+
if (p == NULL || *p == NULL) {
172173
return;
173174
}
174-
XFREE((void*)p);
175+
XFREE(*p);
176+
*p = NULL;
175177
}
176178
#endif
177179

178180
#ifndef S_FREE_X509_STRING_ARRAY
179181
#define S_FREE_X509_STRING_ARRAY
180-
#define s_free_x509_string_array(s, n) s_free_x509_string_array_((ltc_x509_string*)s, n)
181-
static LTC_INLINE void s_free_x509_string_array_(ltc_x509_string *strings, unsigned long num)
182+
#define s_free_x509_string_array(s, n) s_free_x509_string_array_((ltc_x509_string**)&(s), n)
183+
static LTC_INLINE void s_free_x509_string_array_(ltc_x509_string **strings_, unsigned long num)
182184
{
183185
unsigned long n;
186+
ltc_x509_string *strings;
187+
if (strings_ == NULL)
188+
return;
189+
strings = *strings_;
184190
for (n = num; n --> 0;) {
185191
s_free(strings[n].str);
186192
}
187-
s_free(strings);
193+
s_free_((void**)strings_);
188194
}
189195
#endif
190196

@@ -333,7 +339,7 @@ static int s_x509_get_name(const ltc_asn1_list *seq, ltc_x509_name *name)
333339
name->names = names;
334340
name->names_num = names_num;
335341
err_out:
336-
if (err != CRYPT_OK && names != NULL) {
342+
if (err != CRYPT_OK) {
337343
s_free_x509_string_array(names, names_num);
338344
}
339345
XFREE(name_elements);
@@ -432,14 +438,10 @@ static int s_x509_get_validity(const ltc_asn1_list *seq, ltc_x509_validity *vali
432438
}
433439
return CRYPT_OK;
434440
err_out:
435-
if (validity->not_after.str) {
441+
if (validity->not_after.str)
436442
s_free(validity->not_after.str);
437-
validity->not_after.str = NULL;
438-
}
439-
if (validity->not_before.str) {
443+
if (validity->not_before.str)
440444
s_free(validity->not_before.str);
441-
validity->not_before.str = NULL;
442-
}
443445
return err;
444446
}
445447

@@ -691,8 +693,7 @@ void x509_free(const ltc_x509_certificate **cert)
691693
s_free(c->signature.signature);
692694
s_free_x509_tbs_cert(&c->tbs_certificate);
693695
der_free_sequence_flexi((void*)c->asn1);
694-
s_free(c);
695-
*cert = NULL;
696+
s_free_((void**)cert);
696697
}
697698

698699
#undef st_oid_detail

0 commit comments

Comments
 (0)