]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/quic/quic_record_rx_wrap.c
Stop raising ERR_R_MALLOC_FAILURE in most places
[thirdparty/openssl.git] / ssl / quic / quic_record_rx_wrap.c
1 /*
2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include "internal/cryptlib.h"
11 #include "internal/refcount.h"
12 #include "quic_record_rx_wrap.h"
13
14 OSSL_QRX_PKT_WRAP *ossl_qrx_pkt_wrap_new(OSSL_QRX_PKT *pkt)
15 {
16 CRYPTO_RWLOCK *refcount_lock = NULL;
17 OSSL_QRX_PKT_WRAP *res = NULL;
18
19 if (pkt == NULL)
20 return NULL;
21
22 #ifdef HAVE_ATOMICS
23 refcount_lock = CRYPTO_THREAD_lock_new();
24 if (refcount_lock == NULL) {
25 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
26 return NULL;
27 }
28 #endif
29
30 if ((res = OPENSSL_zalloc(sizeof(*res))) == NULL) {
31 CRYPTO_THREAD_lock_free(refcount_lock);
32 return NULL;
33 }
34
35 res->pkt = pkt;
36 res->handle = pkt->handle;
37 res->references = 1;
38 res->lock = refcount_lock;
39
40 return res;
41 }
42
43 int ossl_qrx_pkt_wrap_up_ref(OSSL_QRX_PKT_WRAP *pkt_wrap)
44 {
45 int ref = 0;
46
47 if (pkt_wrap == NULL || pkt_wrap->pkt == NULL)
48 return 0;
49 CRYPTO_UP_REF(&pkt_wrap->references, &ref, pkt_wrap->lock);
50 return 1;
51 }
52
53 void ossl_qrx_pkt_wrap_free(OSSL_QRX *qrx, OSSL_QRX_PKT_WRAP *pkt_wrap)
54 {
55 int ref = 0;
56
57 if (pkt_wrap == NULL)
58 return;
59 CRYPTO_DOWN_REF(&pkt_wrap->references, &ref, pkt_wrap->lock);
60 if (ref > 0)
61 return;
62 ossl_qrx_release_pkt(qrx, pkt_wrap->handle);
63 CRYPTO_THREAD_lock_free(pkt_wrap->lock);
64 OPENSSL_free(pkt_wrap);
65 }