Przeglądaj źródła

Fix snprintf warnings

Compiler shows warnings about possible not large enough buffer, so check
snprintf return value properly.

Signed-off-by: Jan Friesse <jfriesse@redhat.com>
Reviewed-by: Christine Caulfield <ccaulfie@redhat.com>
Jan Friesse 7 lat temu
rodzic
commit
cc81696ff5
3 zmienionych plików z 21 dodań i 4 usunięć
  1. 8 1
      exec/logconfig.c
  2. 5 2
      exec/main.c
  3. 8 1
      tools/corosync-notifyd.c

+ 8 - 1
exec/logconfig.c

@@ -585,7 +585,14 @@ static int corosync_main_config_read_logging (
 			continue ;
 			continue ;
 		}
 		}
 
 
-		snprintf(key_item, MAP_KEYNAME_MAXLEN, "logging.logger_subsys.%s", key_subsys);
+		if (snprintf(key_item, MAP_KEYNAME_MAXLEN, "logging.logger_subsys.%s",
+		    key_subsys) >= MAP_KEYNAME_MAXLEN) {
+			/*
+			 * This should never happen
+			 */
+			error_reason = "Can't snprintf logger_subsys key_item";
+			goto parse_error;
+		}
 
 
 		if (corosync_main_config_set(key_item, key_subsys, &error_reason) < 0) {
 		if (corosync_main_config_set(key_item, key_subsys, &error_reason) < 0) {
 			goto parse_error;
 			goto parse_error;

+ 5 - 2
exec/main.c

@@ -218,10 +218,13 @@ static void corosync_blackbox_write_to_file (void)
 	localtime_r(&cur_time_t, &cur_time_tm);
 	localtime_r(&cur_time_t, &cur_time_tm);
 
 
 	strftime(time_str, PATH_MAX, "%Y-%m-%dT%H:%M:%S", &cur_time_tm);
 	strftime(time_str, PATH_MAX, "%Y-%m-%dT%H:%M:%S", &cur_time_tm);
-	snprintf(fname, PATH_MAX, "%s/fdata-%s-%lld",
+	if (snprintf(fname, PATH_MAX, "%s/fdata-%s-%lld",
 	    get_run_dir(),
 	    get_run_dir(),
 	    time_str,
 	    time_str,
-	    (long long int)getpid());
+	    (long long int)getpid()) >= PATH_MAX) {
+		log_printf(LOGSYS_LEVEL_ERROR, "Can't snprintf blackbox file name");
+		return ;
+	}
 
 
 	if ((res = qb_log_blackbox_write_to_file(fname)) < 0) {
 	if ((res = qb_log_blackbox_write_to_file(fname)) < 0) {
 		LOGSYS_PERROR(-res, LOGSYS_LEVEL_ERROR, "Can't store blackbox file");
 		LOGSYS_PERROR(-res, LOGSYS_LEVEL_ERROR, "Can't store blackbox file");

+ 8 - 1
tools/corosync-notifyd.c

@@ -303,7 +303,14 @@ static void _cs_cmap_connections_key_changed (
 		return ;
 		return ;
 	}
 	}
 
 
-	snprintf(obj_name, CS_MAX_NAME_LENGTH, "%s.%d.%s", conn_str, pid, (char*)new_value.data);
+	if (snprintf(obj_name, CS_MAX_NAME_LENGTH, "%s.%d.%s", conn_str, pid,
+	    (char*)new_value.data) >= CS_MAX_NAME_LENGTH) {
+		/*
+		 * This should never happen
+		 */
+		qb_log(LOG_ERR, "Can't snprintf obj_name");
+		return ;
+	}
 
 
 	if (event == CMAP_TRACK_ADD) {
 	if (event == CMAP_TRACK_ADD) {
 		_cs_application_connection_event(obj_name, "connected");
 		_cs_application_connection_event(obj_name, "connected");