ObjFW  Check-in [7267f55840]

Overview
Comment:Better fallback asprintf() implementation

Instead of trying if it's C99 compliant and falling back to a 64 KB
buffer if it is not, resize the buffer in powers of 2 until it fits.

The problem is that some implementations that are not compliant with C99
don't just return -1 for a length of 0, but actually crash (e.g. HP-UX).

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7267f55840e0f00423d782e94e830949f946820e5ea57841791a231165139bdf
User & Date: js on 2021-01-01 18:36:01
Other Links: manifest | tags
Context
2021-01-01
18:49
Handle pthread attrs being unavailable check-in: e45e320379 user: js tags: trunk
18:36
Better fallback asprintf() implementation check-in: 7267f55840 user: js tags: trunk
16:10
Add HP-UX libraries to ignore glob check-in: 125faa9dcc user: js tags: trunk
Changes

Modified src/of_asprintf.m from [cd42320784] to [35d52654ca].

95
96
97
98
99
100
101
102
103
104
105

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120


121

122
123
124
125
126


127
128
129
130
131
132
133

134
135
136
137
138
139
140
141
}
#endif

#ifndef HAVE_ASPRINTF
static int
vasprintf(char **string, const char *format, va_list arguments)
{
	int expectedLength, length;
	va_list argumentsCopy;

	va_copy(argumentsCopy, arguments);


	expectedLength = vsnprintf(NULL, 0, format, argumentsCopy);
	if (expectedLength == -1)
		/*
		 * We have no way to know how large it is. Let's try 64 KB and
		 * hope.
		 */
		expectedLength = 65535;

	if ((*string = malloc((size_t)expectedLength + 1)) == NULL)
		return -1;

	length = vsnprintf(*string, (size_t)expectedLength + 1,
	    format, arguments);



	if (length == -1 || length > expectedLength) {

		free(*string);
		*string = NULL;
		return -1;
	}



	/*
	 * In case we could not determine the size, resize to the actual size
	 * needed, but ignore any failure to do so.
	 */
	if (length < expectedLength) {
		char *resized;


		if ((resized = realloc(*string, length + 1)) != NULL)
			*string = resized;
	}

	return length;
}

static int







|
<

<
>

<
|
|
<
<
<
<

|
|

|
|

>
>
|
>
|
<
|
|

>
>
|
<
<
<
|
|

>
|







95
96
97
98
99
100
101
102

103

104
105

106
107




108
109
110
111
112
113
114
115
116
117
118
119

120
121
122
123
124
125



126
127
128
129
130
131
132
133
134
135
136
137
}
#endif

#ifndef HAVE_ASPRINTF
static int
vasprintf(char **string, const char *format, va_list arguments)
{
	size_t length, bufferLength = 128;



	*string = NULL;


	for (;;) {
		free(*string);





		if ((*string = malloc(bufferLength)) == NULL)
			return -1;

		length = vsnprintf(*string, bufferLength - 1, format,
		    arguments);

		if (length > 0 && (size_t)length < bufferLength - 1)
			break;

		if (bufferLength > INT_MAX / 2) {
			free(*string);

			return -1;
		}

		bufferLength <<= 1;
	}




	if (length != bufferLength - 1) {
		char *resized = realloc(*string, length + 1);

		/* Ignore if making it smaller failed. */
		if (resized != NULL)
			*string = resized;
	}

	return length;
}

static int