[PATCH/RFC V2 06/26] main: Introduce xstrdup() function

Kevin Cernekee cernekee at gmail.com
Sun Aug 11 21:49:07 EDT 2013


This will abort on error, instead of causing a segfault.

Signed-off-by: Kevin Cernekee <cernekee at gmail.com>
---
 main.c |   12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/main.c b/main.c
index b3b1776..03ba644 100644
--- a/main.c
+++ b/main.c
@@ -366,9 +366,21 @@ static int config_line_num = 0;
  *    For this we use the keep_config_arg() macro below.
  * 3. It may be freed during normal operation, so we have to use strdup()
  *    even when it's an option from argv[]. (e.g. vpninfo->cert_password).
+ *    For this we use the xstrdup() function below.
  */
 #define keep_config_arg() (config_file && config_arg ? strdup(config_arg) : config_arg)
 
+static char *xstrdup(const char *arg)
+{
+	char *ret = strdup(arg);
+
+	if (!ret) {
+		fprintf(stderr, _("Failed to allocate string\n"));
+		exit(1);
+	}
+	return ret;
+}
+
 static int next_option(int argc, char **argv, char **config_arg)
 {
 	/* These get re-used */
-- 
1.7.9.5




More information about the openconnect-devel mailing list