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)
(cherry picked from commit 7ec5c6ec53f333b7a65a02d46ee8a3757c264e2e)

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