Path: blob/main/crypto/krb5/src/util/support/printf.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* util/support/printf.c */2/*3* Copyright 2003, 2004, 2005, 2007, 2008 Massachusetts Institute of4* Technology. All Rights Reserved.5*6* Export of this software from the United States of America may7* require a specific license from the United States Government.8* It is the responsibility of any person or organization contemplating9* export to obtain such a license before exporting.10*11* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and12* distribute this software and its documentation for any purpose and13* without fee is hereby granted, provided that the above copyright14* notice appear in all copies and that both that copyright notice and15* this permission notice appear in supporting documentation, and that16* the name of M.I.T. not be used in advertising or publicity pertaining17* to distribution of the software without specific, written prior18* permission. Furthermore if you modify this software you must label19* your software as modified software and not distribute it in such a20* fashion that it might be confused with the original M.I.T. software.21* M.I.T. makes no representations about the suitability of22* this software for any purpose. It is provided "as is" without express23* or implied warranty.24*/2526/* Provide {,v}asprintf for platforms that don't have them. */2728#include "k5-platform.h"2930/* On error: BSD: Set *ret to NULL. GNU: *ret is undefined.3132Since we want to be able to use the GNU version directly, we need33provide only the weaker guarantee in this version. */34int35krb5int_vasprintf(char **ret, const char *format, va_list ap)36{37va_list ap2;38char *str = NULL, *nstr;39size_t len = 80;40int len2;4142while (1) {43if (len >= INT_MAX || len == 0)44goto fail;45nstr = realloc(str, len);46if (nstr == NULL)47goto fail;48str = nstr;49va_copy(ap2, ap);50len2 = vsnprintf(str, len, format, ap2);51va_end(ap2);52/* ISO C vsnprintf returns the needed length. Some old53vsnprintf implementations return -1 on truncation. */54if (len2 < 0) {55/* Don't know how much space we need, just that we didn't56supply enough; get a bigger buffer and try again. */57if (len <= SIZE_MAX/2)58len *= 2;59else if (len < SIZE_MAX)60len = SIZE_MAX;61else62goto fail;63} else if ((unsigned int) len2 >= SIZE_MAX) {64/* Need more space than we can request. */65goto fail;66} else if ((size_t) len2 >= len) {67/* Need more space, but we know how much. */68len = (size_t) len2 + 1;69} else {70/* Success! */71break;72}73}74/* We might've allocated more than we need, if we're still using75the initial guess, or we got here by doubling. */76if ((size_t) len2 < len - 1) {77nstr = realloc(str, (size_t) len2 + 1);78if (nstr)79str = nstr;80}81*ret = str;82return len2;8384fail:85free(str);86return -1;87}8889int90krb5int_asprintf(char **ret, const char *format, ...)91{92va_list ap;93int n;9495va_start(ap, format);96n = krb5int_vasprintf(ret, format, ap);97va_end(ap);98return n;99}100101102