Skip to content

Commit 0c9dbe6

Browse files
wpwrakSebastien Bourdeauducq
authored and
Sebastien Bourdeauducq
committedDec 14, 2011
parser_helper.c: use asprintf instead of DIY solution
1 parent 7a57cc5 commit 0c9dbe6

File tree

1 file changed

+4
-22
lines changed

1 file changed

+4
-22
lines changed
 

‎src/compiler/parser_helper.c

+4-22
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
#include <stdarg.h>
19+
#define _GNU_SOURCE /* for asprintf */
1920
#include <stdlib.h>
2021
#include <stdio.h>
2122
#include <malloc.h>
@@ -44,25 +45,6 @@ static int printable_label(const char *s)
4445
return p-s;
4546
}
4647

47-
static const char *alloc_printf(const char *fmt, ...)
48-
{
49-
va_list ap;
50-
int n;
51-
char *s;
52-
53-
va_start(ap, fmt);
54-
n = vsnprintf(NULL, 0, fmt, ap);
55-
va_end(ap);
56-
57-
s = malloc(n+1);
58-
59-
va_start(ap, fmt);
60-
vsnprintf(s, n+1, fmt, ap);
61-
va_end(ap);
62-
63-
return s;
64-
}
65-
6648
const char *fpvm_parse(const char *expr, int start_token,
6749
struct parser_comm *comm)
6850
{
@@ -77,7 +59,7 @@ const char *fpvm_parse(const char *expr, int start_token,
7759
int tok;
7860
struct id *identifier;
7961
void *p;
80-
const char *error = NULL;
62+
char *error = NULL;
8163

8264
s = new_scanner((unsigned char *)expr);
8365
p = ParseAlloc(malloc);
@@ -103,7 +85,7 @@ const char *fpvm_parse(const char *expr, int start_token,
10385

10486
state.id = identifier;
10587
if(tok == TOK_ERROR) {
106-
error = alloc_printf(
88+
asprintf(&error,
10789
"FPVM, line %d: scan error near '%c'",
10890
s->lineno, printable_char(s->cursor[-1]));
10991
ParseFree(p, free);
@@ -118,7 +100,7 @@ const char *fpvm_parse(const char *expr, int start_token,
118100
delete_scanner(s);
119101

120102
if(!state.success) {
121-
error = alloc_printf(
103+
asprintf(&error,
122104
"FPVM, line %d: %s near '%.*s'",
123105
state.error_lineno,
124106
state.error ? state.error : "parse error",

0 commit comments

Comments
 (0)
Please sign in to comment.