Skip to content

Commit e3e3989

Browse files
olszomalmtrojnar
authored andcommitted
Fix potential memory leak and handle zero-length allocation, CID 379854
1 parent cf66c1a commit e3e3989

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/p11_attr.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,16 @@ void pkcs11_addattr_obj(PKCS11_TEMPLATE *tmpl, int type, pkcs11_i2d_fn enc, void
152152
size_t n;
153153

154154
n = enc(obj, NULL);
155+
if (n == 0)
156+
return;
157+
155158
buf = p = OPENSSL_malloc(n);
156-
if (n && p) {
157-
enc(obj, &p);
158-
i = pkcs11_addattr(tmpl, type, buf, n);
159-
tmpl->allocated |= 1<<i;
160-
}
159+
if (!buf)
160+
return;
161+
162+
enc(obj, &p);
163+
i = pkcs11_addattr(tmpl, type, buf, n);
164+
tmpl->allocated |= 1<<i;
161165
}
162166

163167
void pkcs11_zap_attrs(PKCS11_TEMPLATE *tmpl)

0 commit comments

Comments
 (0)