Browse code

core: main - coherent behaviour for fix_cfg_file()

- duplicate to build the output always, safer not to try to change the
static string defined via macro at runtime

Daniel-Constantin Mierla authored on 08/04/2021 07:38:48
Showing 1 changed files
... ...
@@ -1260,14 +1260,15 @@ static struct name_lst* parse_phostport_mh(char* s, char** host, int* hlen,
1260 1260
 
1261 1261
 
1262 1262
 
1263
-/** Update \c cfg_file variable to contain full pathname. The function updates
1263
+/** Update \c cfg_file variable to contain full pathname or '-' (for stdin)
1264
+ * allocated in system memory. The function updates
1264 1265
  * the value of \c cfg_file global variable to contain full absolute pathname
1265
- * to the main configuration file of SER. The function uses CFG_FILE macro to
1266
+ * to the main configuration file. The function uses CFG_FILE macro to
1266 1267
  * determine the default path to the configuration file if the user did not
1267 1268
  * specify one using the command line option. If \c cfg_file contains an
1268
- * absolute pathname then it is used unmodified, if it contains a relative
1269
+ * absolute pathname then it is cloned unmodified, if it contains a relative
1269 1270
  * pathanme than the value returned by \c getcwd function will be added at the
1270
- * beginning. This function must be run before SER changes its current working
1271
+ * beginning. This function must be run before changing its current working
1271 1272
  * directory to / (in daemon mode).
1272 1273
  * @return Zero on success, negative number
1273 1274
  * on error.
... ...
@@ -1276,34 +1277,55 @@ int fix_cfg_file(void)
1276 1277
 {
1277 1278
 	char* res = NULL;
1278 1279
 	size_t max_len, cwd_len, cfg_len;
1279
-	
1280
+
1280 1281
 	if (cfg_file == NULL) cfg_file = CFG_FILE;
1281
-	if (cfg_file[0] == '/') return 0;
1282
-	if (cfg_file[0] == '-' && strlen(cfg_file)==1) return 0;
1283
-	
1282
+	if (cfg_file[0] == '/') {
1283
+		cfg_len = strlen(cfg_file);
1284
+		if(cfg_len < 2) {
1285
+			/* do not accept only '/' */
1286
+			fprintf(stderr, "ERROR: invalid cfg file value\n");
1287
+			return -1;
1288
+		}
1289
+		if ((res = malloc(cfg_len + 1)) == NULL) goto error;
1290
+		memcpy(res, cfg_file, cfg_len);
1291
+		cfg_file[cfg_len] = 0;
1292
+		cfg_file = res;
1293
+		return 0;
1294
+	}
1295
+	if (cfg_file[0] == '-') {
1296
+		cfg_len = strlen(cfg_file);
1297
+		if(cfg_len == 1) {
1298
+			if ((res = malloc(2)) == NULL) goto error;
1299
+			res[0] = '-';
1300
+			res[1] = '\0';
1301
+			cfg_file = res;
1302
+			return 0;
1303
+		}
1304
+	}
1305
+
1284 1306
 	/* cfg_file contains a relative pathname, get the current
1285 1307
 	 * working directory and add it at the beginning
1286 1308
 	 */
1287 1309
 	cfg_len = strlen(cfg_file);
1288
-	
1310
+
1289 1311
 	max_len = pathmax();
1290 1312
 	if ((res = malloc(max_len)) == NULL) goto error;
1291
-	
1313
+
1292 1314
 	if (getcwd(res, max_len) == NULL) goto error;
1293 1315
 	cwd_len = strlen(res);
1294
-	
1316
+
1295 1317
 	/* Make sure that the buffer is big enough */
1296 1318
 	if (cwd_len + 1 + cfg_len >= max_len) goto error;
1297
-	
1319
+
1298 1320
 	res[cwd_len] = '/';
1299 1321
 	memcpy(res + cwd_len + 1, cfg_file, cfg_len);
1300
-	
1322
+
1301 1323
 	res[cwd_len + 1 + cfg_len] = '\0'; /* Add terminating zero */
1302 1324
 	cfg_file = res;
1303 1325
 	return 0;
1304
-	
1326
+
1305 1327
  error:
1306
-	fprintf(stderr, "ERROR: Unable to fix cfg_file to contain full pathname\n");
1328
+	fprintf(stderr, "ERROR: Unable to fix cfg file to contain full pathname\n");
1307 1329
 	if (res) free(res);
1308 1330
 	return -1;
1309 1331
 }