aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/asprintf.c
blob: 7f33818825c0906e9aae909234268cdf47b7b0f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * Copyright 2026 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <stdarg.h>
#include <stdio.h>
#include <openssl/crypto.h>
#include "internal/cryptlib.h"

int ossl_vasprintf(char **str, const char *format, va_list args)
{
    char *candidate = NULL;
    size_t candidate_len = 64;
    size_t tmp_len = 0;
    char *tmp = NULL;
    int ret;

    if ((candidate = OPENSSL_malloc(candidate_len)) == NULL)
        goto err;
    va_list args_copy;
    va_copy(args_copy, args);
    ret = vsnprintf(candidate, candidate_len, format, args_copy);
    va_end(args_copy);
    if (ret < 0)
        goto err;
    if ((size_t)ret >= candidate_len) {
        /*  Too big to fit in allocation. */

        tmp_len = (size_t)ret + 1;
        if ((tmp = OPENSSL_malloc(tmp_len)) == NULL)
            goto err;
        OPENSSL_clear_free(candidate, candidate_len);
        candidate = tmp;
        candidate_len = tmp_len;
        tmp = NULL;
        ret = vsnprintf(candidate, candidate_len, format, args);
    }
    /* At this point this should not happen unless vsnprintf is insane. */
    if (ret < 0 || (size_t)ret >= candidate_len)
        goto err;
    *str = candidate;
    return ret;

err:
    OPENSSL_clear_free(candidate, candidate_len);
    OPENSSL_clear_free(tmp, tmp_len);
    *str = NULL;
    return -1;
}

int ossl_asprintf(char **str, const char *format, ...)
{
    va_list args;
    int ret;

    va_start(args, format);
    ret = ossl_vasprintf(str, format, args);
    va_end(args);
    return ret;
}