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

(cherry picked from commit 74fa6c5570d6394af8c5352bd37e21e970f1575c)

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