[PATCH 2/2] spfquery: Simplify handling of the "result" buffer.

Florian Forster <[email protected]> Sun, 23 Jan 2011 15:51:12 +0100
Newsgroups gmane.mail.spam.spf.devel
Message-ID <a830fe18b02f0f68034bcd4416f8b4025d43139c.1295794247.git.ff@octo.it>
From: Florian Forster <[email protected]>

This patch changes the "result" buffer to a character array of a fixed
size. Also, the result is now printed as

  status0,status1,status2

instead of:

  status0status1status2
---
 src/spfquery/spfquery.c |   33 ++++++++++++---------------------
 1 files changed, 12 insertions(+), 21 deletions(-)

diff --git a/src/spfquery/spfquery.c b/src/spfquery/spfquery.c
index 1904c8a..a93a81c 100644
--- a/src/spfquery/spfquery.c
+++ b/src/spfquery/spfquery.c
@@ -123,21 +123,16 @@
 #define WARN_ERROR do { res = 255; } while(0)
 #define FAIL_ERROR do { res = 255; goto error; } while(0)
 
-#define RESIZE_RESULT(n) do { \
-	if (result == NULL) { \
-		result_len = 256 + n; \
-		result = malloc(result_len); \
-		result[0] = '\0'; \
-	} \
-	else if (strlen(result) + n >= result_len) { \
-		result_len = result_len + (result_len >> 1) + 8 + n; \
-		result = realloc(result, result_len); \
-	} \
-} while(0)
-#define APPEND_RESULT(n) do { \
-	partial_result = SPF_strresult(n); \
-	RESIZE_RESULT(strlen(partial_result)); \
-	strcat(result, partial_result); \
+#define APPEND_RESULT(n) do {                                             \
+	const char *partial_result = SPF_strresult(n);                        \
+	if (result[0] == 0) {                                                 \
+		strncpy (result, partial_result, sizeof (result));                \
+	} else {                                                              \
+		char tmp[sizeof (result)];                                        \
+		strncpy (tmp, result, sizeof (tmp));                              \
+		snprintf (result, sizeof (result), "%s,%s", tmp, partial_result); \
+	}                                                                     \
+	result[sizeof (result) - 1] = 0;                                      \
 } while(0)
 
 #define X_OR_EMPTY(x) ((x) ? (x) : "")
@@ -354,9 +349,7 @@ int main( int argc, char *argv[] )
 	int				 res = 0;
 	int				 c;
 
-	const char		*partial_result;
-	char			*result = NULL;
-	int				 result_len = 0;
+	char			 result[1024];
 
 	opts = (SPF_client_options_t *)malloc(sizeof(SPF_client_options_t));
 	memset(opts, 0, sizeof(SPF_client_options_t));
@@ -635,8 +628,7 @@ int main( int argc, char *argv[] )
 			CONTINUE_ERROR;
 		}
 
-		if (result != NULL)
-			result[0] = '\0';
+		memset (result, 0, sizeof (result));
 		APPEND_RESULT(SPF_response_result(spf_response));
 		
 		if (req->rcpt_to != NULL  && *req->rcpt_to != '\0' ) {
@@ -704,7 +696,6 @@ int main( int argc, char *argv[] )
 	}
 
   error:
-	FREE(result, free);
 	FREE_RESPONSE(spf_response);
 	FREE_REQUEST(spf_request);
 	FREE(spf_server, SPF_server_free);
-- 
1.7.2.3