Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

parser_helper.c: use asprintf instead of DIY solution

  • Loading branch information...
commit 0c9dbe645dedbf0aae32f1d0cf09d8081cd8d3ce 1 parent 7a57cc5
Werner Almesberger authored December 12, 2011 sbourdeauducq committed December 14, 2011

Showing 1 changed file with 4 additions and 22 deletions. Show diff stats Hide diff stats

  1. 26  src/compiler/parser_helper.c
26  src/compiler/parser_helper.c
@@ -16,6 +16,7 @@
16 16
  */
17 17
 
18 18
 #include <stdarg.h>
  19
+#define _GNU_SOURCE /* for asprintf */
19 20
 #include <stdlib.h>
20 21
 #include <stdio.h>
21 22
 #include <malloc.h>
@@ -44,25 +45,6 @@ static int printable_label(const char *s)
44 45
 	return p-s;
45 46
 }
46 47
 
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  
-
66 48
 const char *fpvm_parse(const char *expr, int start_token,
67 49
     struct parser_comm *comm)
68 50
 {
@@ -77,7 +59,7 @@ const char *fpvm_parse(const char *expr, int start_token,
77 59
 	int tok;
78 60
 	struct id *identifier;
79 61
 	void *p;
80  
-	const char *error = NULL;
  62
+	char *error = NULL;
81 63
 
82 64
 	s = new_scanner((unsigned char *)expr);
83 65
 	p = ParseAlloc(malloc);
@@ -103,7 +85,7 @@ const char *fpvm_parse(const char *expr, int start_token,
103 85
 
104 86
 		state.id = identifier;
105 87
 		if(tok == TOK_ERROR) {
106  
-			error = alloc_printf(
  88
+			asprintf(&error,
107 89
 			    "FPVM, line %d: scan error near '%c'",
108 90
 			    s->lineno, printable_char(s->cursor[-1]));
109 91
 			ParseFree(p, free);
@@ -118,7 +100,7 @@ const char *fpvm_parse(const char *expr, int start_token,
118 100
 	delete_scanner(s);
119 101
 
120 102
 	if(!state.success) {
121  
-		error = alloc_printf(
  103
+		asprintf(&error,
122 104
 		    "FPVM, line %d: %s near '%.*s'",
123 105
 		    state.error_lineno,
124 106
 		    state.error ? state.error : "parse error",

0 notes on commit 0c9dbe6

Please sign in to comment.
Something went wrong with that request. Please try again.