Просмотр исходного кода

Fix some stringop-truncation warnings

Guido Aulisi 4 лет назад
Родитель
Сommit
a431b55161
4 измененных файлов с 17 добавлено и 13 удалено
  1. 6 4
      lib/parse_ini.c
  2. 6 4
      lib/utils_base.c
  3. 3 3
      plugins/check_disk.c
  4. 2 2
      plugins/check_smtp.c

+ 6 - 4
lib/parse_ini.c

@@ -324,11 +324,13 @@ static int add_option(FILE *f, np_arg_list **optlst){
 	optnew->arg=malloc(cfg_len+1);
 	/* 1-character params needs only one dash */
 	if(opt_len==1) {
-		strncpy(&optnew->arg[read_pos], "-", 1);
-		read_pos+=1;
+		optnew->arg[read_pos]='-';
+		++read_pos;
 	} else {
-		strncpy(&optnew->arg[read_pos], "--", 2);
-		read_pos+=2;
+		optnew->arg[read_pos]='-';
+		++read_pos;
+		optnew->arg[read_pos]='-';
+		++read_pos;
 	}
 	strncpy(&optnew->arg[read_pos], optptr, opt_len); read_pos+=opt_len;
 	if(value) {

+ 6 - 4
lib/utils_base.c

@@ -332,6 +332,7 @@ int np_warn_if_not_root(void) {
 char *np_extract_value(const char *varlist, const char *name, char sep) {
 	char *tmp=NULL, *value=NULL;
 	int i;
+	size_t varlistlen;
 
 	while (1) {
 		/* Strip any leading space */
@@ -356,10 +357,11 @@ char *np_extract_value(const char *varlist, const char *name, char sep) {
 					value[tmp-varlist] = '\0';
 				} else {
 					/* Value is delimited by a \0 */
-					if (!strlen(varlist)) continue;
-					value = (char *)calloc(1, strlen(varlist) + 1);
-					strncpy(value, varlist, strlen(varlist));
-					value[strlen(varlist)] = '\0';
+					varlistlen = strlen(varlist);
+					if (!varlistlen) continue;
+					value = (char *)calloc(1, varlistlen + 1);
+					strncpy(value, varlist, varlistlen);
+					value[varlistlen] = '\0';
 				}
 				break;
 			}

+ 3 - 3
plugins/check_disk.c

@@ -488,11 +488,11 @@ main (int argc, char **argv)
           const char *free_pct_str = &human_disk_entry->free_pct_str[0];
           const char *disk_result_str = state_text(human_disk_entry->disk_result);
           const char *avail_bytes_str = human_readable(human_disk_entry->avail_bytes, &human_buf[0], human_opts, 1, 1);
-          strncpy(&human_disk_entry->avail_bytes_str[0], avail_bytes_str, sizeof(human_disk_entry->avail_bytes_str));
+          strncpy(human_disk_entry->avail_bytes_str, avail_bytes_str, sizeof(human_disk_entry->avail_bytes_str - 1));
           const char *total_bytes_str = human_readable(human_disk_entry->total_bytes, &human_buf[0], human_opts, 1, 1);
-          strncpy(&human_disk_entry->total_bytes_str[0], total_bytes_str, sizeof(human_disk_entry->total_bytes_str));
+          strncpy(human_disk_entry->total_bytes_str, total_bytes_str, sizeof(human_disk_entry->total_bytes_str - 1));
 
-          strncpy(&human_disk_entry->disk_result_str[0], disk_result_str, sizeof(human_disk_entry->disk_result_str));
+          strncpy(human_disk_entry->disk_result_str, disk_result_str, sizeof(human_disk_entry->disk_result_str - 1));
           if (human_column_widths.free_pct < strlen(free_pct_str))       human_column_widths.free_pct = strlen(free_pct_str);
           if (human_column_widths.avail_bytes < strlen(avail_bytes_str)) human_column_widths.avail_bytes = strlen(avail_bytes_str);
           if (human_column_widths.total_bytes < strlen(total_bytes_str)) human_column_widths.total_bytes = strlen(total_bytes_str);

+ 2 - 2
plugins/check_smtp.c

@@ -584,7 +584,7 @@ process_arguments (int argc, char **argv)
 					     _("Could not realloc() units [%d]\n"), ncommands);
 			}
 			commands[ncommands] = (char *) malloc (sizeof(char) * 255);
-			strncpy (commands[ncommands], optarg, 255);
+			strncpy (commands[ncommands], optarg, 254);
 			ncommands++;
 			break;
 		case 'R':									/* server responses */
@@ -596,7 +596,7 @@ process_arguments (int argc, char **argv)
 					     _("Could not realloc() units [%d]\n"), nresponses);
 			}
 			responses[nresponses] = (char *) malloc (sizeof(char) * 255);
-			strncpy (responses[nresponses], optarg, 255);
+			strncpy (responses[nresponses], optarg, 254);
 			nresponses++;
 			break;
 		case 'c':									/* critical time threshold */