[MERGED] osmocom-bb[master]: mobile/main.c: clean up config file selection logic

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Mon Sep 11 06:51:17 UTC 2017


Harald Welte has submitted this change and it was merged.

Change subject: mobile/main.c: clean up config file selection logic
......................................................................


mobile/main.c: clean up config file selection logic

The 903e2515 introduced the following problems:

  - The home variable is allocated dynamically by talloc,
    but not being freed. There is no need for dynamical
    memory allocation, as the getenv() returns a pointer
    to a value in the environment or NULL.

  - In case of custom configuration file, a pointer to
    a part of stack (not heap) is passed to talloc_free().
    This may cause unexpected behaviour of segfault.

Let's fix both of them.

Change-Id: I79cc3b954c3018b7e780f6351c3030c3062470b5
---
M src/host/layer23/src/mobile/main.c
1 file changed, 15 insertions(+), 9 deletions(-)

Approvals:
  Max: Looks good to me, but someone else must approve
  Harald Welte: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/src/host/layer23/src/mobile/main.c b/src/host/layer23/src/mobile/main.c
index 4a2e4ec..bc66557 100644
--- a/src/host/layer23/src/mobile/main.c
+++ b/src/host/layer23/src/mobile/main.c
@@ -38,7 +38,6 @@
 #include <getopt.h>
 #include <stdlib.h>
 #include <stdio.h>
-#include <stdbool.h>
 #include <unistd.h>
 #include <string.h>
 #include <errno.h>
@@ -52,8 +51,7 @@
 void *l23_ctx = NULL;
 struct llist_head ms_list;
 static char *gsmtap_ip = 0;
-static const char *config_file = ".osmocom/bb/mobile.cfg";
-bool use_default_cfg = true;
+static const char *custom_cfg_file = NULL;
 struct gsmtap_inst *gsmtap_inst = NULL;
 static char *vty_ip = "127.0.0.1";
 unsigned short vty_port = 4247;
@@ -138,8 +136,7 @@
 			vty_ip = optarg;
 			break;
 		case 'c':
-			config_file = optarg;
-			use_default_cfg = false;
+			custom_cfg_file = optarg;
 			break;
 		case 'v':
 			vty_port = atoi(optarg);
@@ -208,9 +205,9 @@
 
 int main(int argc, char **argv)
 {
+	char *config_file;
 	int quit = 0;
 	int rc;
-	char const * home;
 
 	printf("%s\n", openbsc_copyright);
 
@@ -240,9 +237,18 @@
 		gsmtap_source_add_sink(gsmtap_inst);
 	}
 
-	if (use_default_cfg) {
-		home = talloc_strdup(l23_ctx, getenv("HOME"));
-		config_file = talloc_asprintf_append(home, "/%s", config_file);
+	if (custom_cfg_file) {
+		/* Use full path provided by user */
+		config_file = talloc_strdup(l23_ctx, custom_cfg_file);
+	} else {
+		/* Obtain the user's home directory path */
+		const char *home_dir = getenv("HOME");
+		if (!home_dir)
+			home_dir = "~";
+
+		/* Concatenate it with default config path */
+		config_file = talloc_asprintf(l23_ctx, "%s/%s",
+			home_dir, ".osmocom/bb/mobile.cfg");
 	}
 
 	/* save the config file directory name */

-- 
To view, visit https://gerrit.osmocom.org/3897
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I79cc3b954c3018b7e780f6351c3030c3062470b5
Gerrit-PatchSet: 1
Gerrit-Project: osmocom-bb
Gerrit-Branch: master
Gerrit-Owner: Vadim Yanitskiy <axilirator at gmail.com>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Max <msuraev at sysmocom.de>



More information about the gerrit-log mailing list