NeoMutt  2018-07-16 +952-a2da0a
Teaching an old dog new tricks
DOXYGEN
safe_asprintf.c File Reference

Wrapper for vasprintf()/vsnprintf() More...

#include "config.h"
#include <stdarg.h>
#include <stdio.h>
#include "mutt/mutt.h"
+ Include dependency graph for safe_asprintf.c:

Go to the source code of this file.

Functions

int safe_asprintf (char **strp, const char *fmt,...)
 

Detailed Description

Wrapper for vasprintf()/vsnprintf()

Authors
  • Michael R. Elkins

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file safe_asprintf.c.

Function Documentation

int safe_asprintf ( char **  strp,
const char *  fmt,
  ... 
)

Definition at line 77 of file safe_asprintf.c.

78 {
79  int rlen = STRING;
80 
81  *strp = mutt_mem_malloc(rlen);
82  while (true)
83  {
84  va_list ap;
85  va_start(ap, fmt);
86  const int n = vsnprintf(*strp, rlen, fmt, ap);
87  va_end(ap);
88  if (n < 0)
89  {
90  FREE(strp);
91  return n;
92  }
93 
94  if (n < rlen)
95  {
96  /* reduce space to just that which was used. note that 'n' does not
97  * include the terminal nul char.
98  */
99  if (n == 0) /* convention is to use NULL for zero-length strings. */
100  FREE(strp);
101  else if (n != rlen - 1)
102  mutt_mem_realloc(strp, n + 1);
103  return n;
104  }
105  /* increase size and try again */
106  rlen = n + 1;
107  mutt_mem_realloc(strp, rlen);
108  }
109  /* not reached */
110 }
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:124
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:99
#define STRING
Definition: string2.h:35
#define FREE(x)
Definition: memory.h:46

+ Here is the call graph for this function:

+ Here is the caller graph for this function: