Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/crypto/krb5/src/util/support/printf.c
34889 views
1
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
/* util/support/printf.c */
3
/*
4
* Copyright 2003, 2004, 2005, 2007, 2008 Massachusetts Institute of
5
* Technology. All Rights Reserved.
6
*
7
* Export of this software from the United States of America may
8
* require a specific license from the United States Government.
9
* It is the responsibility of any person or organization contemplating
10
* export to obtain such a license before exporting.
11
*
12
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13
* distribute this software and its documentation for any purpose and
14
* without fee is hereby granted, provided that the above copyright
15
* notice appear in all copies and that both that copyright notice and
16
* this permission notice appear in supporting documentation, and that
17
* the name of M.I.T. not be used in advertising or publicity pertaining
18
* to distribution of the software without specific, written prior
19
* permission. Furthermore if you modify this software you must label
20
* your software as modified software and not distribute it in such a
21
* fashion that it might be confused with the original M.I.T. software.
22
* M.I.T. makes no representations about the suitability of
23
* this software for any purpose. It is provided "as is" without express
24
* or implied warranty.
25
*/
26
27
/* Provide {,v}asprintf for platforms that don't have them. */
28
29
#include "k5-platform.h"
30
31
/* On error: BSD: Set *ret to NULL. GNU: *ret is undefined.
32
33
Since we want to be able to use the GNU version directly, we need
34
provide only the weaker guarantee in this version. */
35
int
36
krb5int_vasprintf(char **ret, const char *format, va_list ap)
37
{
38
va_list ap2;
39
char *str = NULL, *nstr;
40
size_t len = 80;
41
int len2;
42
43
while (1) {
44
if (len >= INT_MAX || len == 0)
45
goto fail;
46
nstr = realloc(str, len);
47
if (nstr == NULL)
48
goto fail;
49
str = nstr;
50
va_copy(ap2, ap);
51
len2 = vsnprintf(str, len, format, ap2);
52
va_end(ap2);
53
/* ISO C vsnprintf returns the needed length. Some old
54
vsnprintf implementations return -1 on truncation. */
55
if (len2 < 0) {
56
/* Don't know how much space we need, just that we didn't
57
supply enough; get a bigger buffer and try again. */
58
if (len <= SIZE_MAX/2)
59
len *= 2;
60
else if (len < SIZE_MAX)
61
len = SIZE_MAX;
62
else
63
goto fail;
64
} else if ((unsigned int) len2 >= SIZE_MAX) {
65
/* Need more space than we can request. */
66
goto fail;
67
} else if ((size_t) len2 >= len) {
68
/* Need more space, but we know how much. */
69
len = (size_t) len2 + 1;
70
} else {
71
/* Success! */
72
break;
73
}
74
}
75
/* We might've allocated more than we need, if we're still using
76
the initial guess, or we got here by doubling. */
77
if ((size_t) len2 < len - 1) {
78
nstr = realloc(str, (size_t) len2 + 1);
79
if (nstr)
80
str = nstr;
81
}
82
*ret = str;
83
return len2;
84
85
fail:
86
free(str);
87
return -1;
88
}
89
90
int
91
krb5int_asprintf(char **ret, const char *format, ...)
92
{
93
va_list ap;
94
int n;
95
96
va_start(ap, format);
97
n = krb5int_vasprintf(ret, format, ap);
98
va_end(ap);
99
return n;
100
}
101
102