#define NOSKIP 0x0020 /* do not skip blanks */
#define NUMBER_SIGNED 0x0040 /* signed integer */
#define GROUP 0x0080 /* ': group numbers */
-#define MALLOC 0x0100 /* a: malloc strings */
+#define GNU_MALLOC 0x0100 /* a: malloc strings */
#define CHAR 0x0200 /* hh: char */
#define I18N 0x0400 /* I: use locale's digits */
#define HEXA_FLOAT 0x0800 /* hexadecimal float */
#define READ_POINTER 0x1000 /* this is a pointer value */
-
+#define POSIX_MALLOC 0x2000 /* m: malloc strings */
+#define MALLOC (GNU_MALLOC | POSIX_MALLOC)
#include <locale/localeinfo.h>
#include <libioP.h>
if (done == 0) done = EOF; \
goto errout; \
} while (0)
+#define add_ptr_to_free(ptr) \
+ do \
+ { \
+ if (ptrs_to_free == NULL \
+ || ptrs_to_free->count == (sizeof (ptrs_to_free->ptrs) \
+ / sizeof (ptrs_to_free->ptrs[0]))) \
+ { \
+ struct ptrs_to_free *new_ptrs = alloca (sizeof (*ptrs_to_free)); \
+ new_ptrs->count = 0; \
+ new_ptrs->next = ptrs_to_free; \
+ ptrs_to_free = new_ptrs; \
+ } \
+ ptrs_to_free->ptrs[ptrs_to_free->count++] = (ptr); \
+ } \
+ while (0)
#define ARGCHECK(s, format) \
do \
{ \
_IO_funlockfile (S); \
__libc_cleanup_region_end (0)
+struct ptrs_to_free
+{
+ size_t count;
+ struct ptrs_to_free *next;
+ char **ptrs[32];
+};
/* Read formatted input from S according to the format string
FORMAT, using the argument list in ARG.
#else
const char *thousands;
#endif
+ struct ptrs_to_free *ptrs_to_free = NULL;
/* State for the conversions. */
mbstate_t state;
/* Integral holding variables. */
}
/* String conversions (%s, %[) take a `char **'
arg and fill it in with a malloc'd pointer. */
- flags |= MALLOC;
+ flags |= GNU_MALLOC;
break;
+ case L_('m'):
+ flags |= POSIX_MALLOC;
+ break;
case L_('z'):
if (need_longlong && sizeof (size_t) > sizeof (unsigned long int))
flags |= LONGDBL;
if (flags & MALLOC) \
{ \
/* The string is to be stored in a malloc'd buffer. */ \
+ /* For %mS using char ** is actually wrong, but \
+ shouldn't make a difference on any arch glibc \
+ supports and would unnecessarily complicate \
+ things. */ \
strptr = ARG (char **); \
if (strptr == NULL) \
conv_error (); \
strsize = 100; \
*strptr = (char *) malloc (strsize * sizeof (Type)); \
Str = (Type *) *strptr; \
+ if (Str != NULL) \
+ add_ptr_to_free (strptr); \
+ else if (flags & POSIX_MALLOC) \
+ goto reteof; \
} \
else \
Str = ARG (Type *); \
strleng + MB_CUR_MAX);
if (newstr == NULL)
{
+ if (flags & POSIX_MALLOC)
+ goto reteof;
/* We lose. Oh well. Terminate the
string and stop converting,
so at least we don't skip any input. */
((char *) (*strptr))[strleng] = '\0';
+ strptr = NULL;
++done;
conv_error ();
}
str = (char *) realloc (*strptr, strsize + 1);
if (str == NULL)
{
+ if (flags & POSIX_MALLOC)
+ goto reteof;
/* We lose. Oh well. Terminate the
string and stop converting,
so at least we don't skip any input. */
((char *) (*strptr))[strsize - 1] = '\0';
+ strptr = NULL;
++done;
conv_error ();
}
newstr = (char *) realloc (*strptr, strleng + n + 1);
if (newstr == NULL)
{
+ if (flags & POSIX_MALLOC)
+ goto reteof;
/* We lose. Oh well. Terminate the string
and stop converting, so at least we don't
skip any input. */
((char *) (*strptr))[strleng] = '\0';
+ strptr = NULL;
++done;
conv_error ();
}
if (cp != NULL)
*strptr = cp;
}
+ strptr = NULL;
++done;
}
* sizeof (wchar_t));
if (wstr == NULL)
{
+ if (flags & POSIX_MALLOC)
+ goto reteof;
/* We lose. Oh well. Terminate the string
and stop converting, so at least we don't
skip any input. */
((wchar_t *) (*strptr))[strsize - 1] = L'\0';
+ strptr = NULL;
++done;
conv_error ();
}
* sizeof (wchar_t)));
if (wstr == NULL)
{
+ if (flags & POSIX_MALLOC)
+ goto reteof;
/* We lose. Oh well. Terminate the
string and stop converting, so at
least we don't skip any input. */
((wchar_t *) (*strptr))[strsize - 1] = L'\0';
+ strptr = NULL;
++done;
conv_error ();
}
if (cp != NULL)
*strptr = (char *) cp;
}
+ strptr = NULL;
++done;
}
* sizeof (wchar_t));
if (wstr == NULL)
{
+ if (flags & POSIX_MALLOC)
+ goto reteof;
/* We lose. Oh well. Terminate the string
and stop converting, so at least we don't
skip any input. */
((wchar_t *) (*strptr))[strsize - 1] = L'\0';
+ strptr = NULL;
++done;
conv_error ();
}
* sizeof (wchar_t)));
if (wstr == NULL)
{
+ if (flags & POSIX_MALLOC)
+ goto reteof;
/* We lose. Oh well. Terminate the
string and stop converting,
so at least we don't skip any input. */
((wchar_t *) (*strptr))[strsize - 1] = L'\0';
+ strptr = NULL;
++done;
conv_error ();
}
if (cp != NULL)
*strptr = (char *) cp;
}
+ strptr = NULL;
++done;
}
strleng + MB_CUR_MAX);
if (newstr == NULL)
{
+ if (flags & POSIX_MALLOC)
+ goto reteof;
/* We lose. Oh well. Terminate the string
and stop converting, so at least we don't
skip any input. */
((char *) (*strptr))[strleng] = '\0';
+ strptr = NULL;
++done;
conv_error ();
}
newsize = strsize + 1;
goto allocagain;
}
+ if (flags & POSIX_MALLOC)
+ goto reteof;
/* We lose. Oh well. Terminate the
string and stop converting,
so at least we don't skip any input. */
((char *) (*strptr))[strsize - 1] = '\0';
+ strptr = NULL;
++done;
conv_error ();
}
newstr = (char *) realloc (*strptr, strleng + n + 1);
if (newstr == NULL)
{
+ if (flags & POSIX_MALLOC)
+ goto reteof;
/* We lose. Oh well. Terminate the string
and stop converting, so at least we don't
skip any input. */
((char *) (*strptr))[strleng] = '\0';
+ strptr = NULL;
++done;
conv_error ();
}
if (cp != NULL)
*strptr = cp;
}
+ strptr = NULL;
++done;
}
if (errp != NULL)
*errp |= errval;
+ if (done == EOF)
+ {
+ reteof:
+ if (__builtin_expect (ptrs_to_free != NULL, 0))
+ {
+ struct ptrs_to_free *p = ptrs_to_free;
+ while (p != NULL)
+ {
+ for (size_t cnt = 0; cnt < p->count; ++cnt)
+ {
+ free (*p->ptrs[cnt]);
+ *p->ptrs[cnt] = NULL;
+ }
+ p = p->next;
+ free (ptrs_to_free);
+ ptrs_to_free = p;
+ }
+ }
+ return EOF;
+ }
+ else if (__builtin_expect (strptr != NULL, 0))
+ {
+ free (*strptr);
+ *strptr = NULL;
+ }
return done;
}