direct-io.hg

changeset 13663:bef7fbe25a9f

Remove uses of strcpy and strncpy from common and x86 code.

Retain safe_strcpy(). It can hide the third argument to strlcpy() in
most cases.

Based on patches from Christoph Egger <Christoph.Egger@amd.com>
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Mon Jan 29 14:59:42 2007 +0000 (2007-01-29)
parents 5bb084098493
children d2784d93e760
files xen/acm/acm_core.c xen/arch/x86/cpu/common.c xen/arch/x86/cpu/cyrix.c xen/arch/x86/cpu/intel.c xen/arch/x86/dmi_scan.c xen/arch/x86/domain_build.c xen/arch/x86/hvm/intercept.c xen/arch/x86/setup.c xen/common/kexec.c xen/common/keyhandler.c xen/common/libelf/libelf-dominfo.c xen/common/libelf/libelf-private.h xen/common/perfc.c xen/common/rangeset.c xen/drivers/char/console.c xen/include/xen/string.h
line diff
     1.1 --- a/xen/acm/acm_core.c	Mon Jan 29 13:22:21 2007 +0000
     1.2 +++ b/xen/acm/acm_core.c	Mon Jan 29 14:59:42 2007 +0000
     1.3 @@ -89,8 +89,11 @@ acm_set_policy_reference(u8 * buf, u32 b
     1.4      if (!acm_bin_pol.policy_reference_name)
     1.5          return -ENOMEM;
     1.6  
     1.7 -    strcpy(acm_bin_pol.policy_reference_name, (char *)(buf + sizeof(struct acm_policy_reference_buffer)));
     1.8 -    printk("%s: Activating policy %s\n", __func__, acm_bin_pol.policy_reference_name);
     1.9 +    strlcpy(acm_bin_pol.policy_reference_name,
    1.10 +            (char *)(buf + sizeof(struct acm_policy_reference_buffer)),
    1.11 +            ntohl(pr->len));
    1.12 +    printk("%s: Activating policy %s\n", __func__,
    1.13 +           acm_bin_pol.policy_reference_name);
    1.14      return 0;
    1.15  }
    1.16  
    1.17 @@ -106,8 +109,9 @@ acm_dump_policy_reference(u8 *buf, u32 b
    1.18  
    1.19      memset(buf, 0, ret);
    1.20      pr_buf->len = htonl(strlen(acm_bin_pol.policy_reference_name) + 1); /* including stringend '\0' */
    1.21 -    strcpy((char *)(buf + sizeof(struct acm_policy_reference_buffer)),
    1.22 -           acm_bin_pol.policy_reference_name);
    1.23 +    strlcpy((char *)(buf + sizeof(struct acm_policy_reference_buffer)),
    1.24 +            acm_bin_pol.policy_reference_name,
    1.25 +            ntohl(pr_buf->len));
    1.26      return ret;
    1.27  }
    1.28  
     2.1 --- a/xen/arch/x86/cpu/common.c	Mon Jan 29 13:22:21 2007 +0000
     2.2 +++ b/xen/arch/x86/cpu/common.c	Mon Jan 29 14:59:42 2007 +0000
     2.3 @@ -30,9 +30,9 @@ static void default_init(struct cpuinfo_
     2.4  	if (c->cpuid_level == -1) {
     2.5  		/* No cpuid. It must be an ancient CPU */
     2.6  		if (c->x86 == 4)
     2.7 -			strcpy(c->x86_model_id, "486");
     2.8 +			safe_strcpy(c->x86_model_id, "486");
     2.9  		else if (c->x86 == 3)
    2.10 -			strcpy(c->x86_model_id, "386");
    2.11 +			safe_strcpy(c->x86_model_id, "386");
    2.12  	}
    2.13  }
    2.14  
    2.15 @@ -383,7 +383,7 @@ void __devinit identify_cpu(struct cpuin
    2.16  		char *p;
    2.17  		p = table_lookup_model(c);
    2.18  		if ( p )
    2.19 -			strcpy(c->x86_model_id, p);
    2.20 +			safe_strcpy(c->x86_model_id, p);
    2.21  		else
    2.22  			/* Last resort... */
    2.23  			snprintf(c->x86_model_id, sizeof(c->x86_model_id),
     3.1 --- a/xen/arch/x86/cpu/cyrix.c	Mon Jan 29 13:22:21 2007 +0000
     3.2 +++ b/xen/arch/x86/cpu/cyrix.c	Mon Jan 29 14:59:42 2007 +0000
     3.3 @@ -181,7 +181,6 @@ static void __init geode_configure(void)
     3.4  static void __init init_cyrix(struct cpuinfo_x86 *c)
     3.5  {
     3.6  	unsigned char dir0, dir0_msn, dir0_lsn, dir1 = 0;
     3.7 -	char *buf = c->x86_model_id;
     3.8  	const char *p = NULL;
     3.9  
    3.10  	/* Bit 31 in normal CPUID used for nonstandard 3DNow ID;
    3.11 @@ -302,8 +301,8 @@ static void __init init_cyrix(struct cpu
    3.12  		dir0_msn = 7;
    3.13  		break;
    3.14  	}
    3.15 -	strcpy(buf, Cx86_model[dir0_msn & 7]);
    3.16 -	if (p) strcat(buf, p);
    3.17 +	safe_strcpy(c->x86_model_id, Cx86_model[dir0_msn & 7]);
    3.18 +	if (p) strcat(c->x86_model_id, p);
    3.19  	return;
    3.20  }
    3.21  
    3.22 @@ -339,7 +338,7 @@ static void cyrix_identify(struct cpuinf
    3.23  	if ( c->x86 == 4 && test_cyrix_52div() ) {
    3.24  		unsigned char dir0, dir1;
    3.25  		
    3.26 -		strcpy(c->x86_vendor_id, "CyrixInstead");
    3.27 +		safe_strcpy(c->x86_vendor_id, "CyrixInstead");
    3.28  	        c->x86_vendor = X86_VENDOR_CYRIX;
    3.29  	        
    3.30  	        /* Actually enable cpuid on the older cyrix */
     4.1 --- a/xen/arch/x86/cpu/intel.c	Mon Jan 29 13:22:21 2007 +0000
     4.2 +++ b/xen/arch/x86/cpu/intel.c	Mon Jan 29 14:59:42 2007 +0000
     4.3 @@ -152,7 +152,7 @@ static void __devinit init_intel(struct 
     4.4  	}
     4.5  
     4.6  	if ( p )
     4.7 -		strcpy(c->x86_model_id, p);
     4.8 +		safe_strcpy(c->x86_model_id, p);
     4.9  	
    4.10  	c->x86_max_cores = num_cpu_cores(c);
    4.11  
     5.1 --- a/xen/arch/x86/dmi_scan.c	Mon Jan 29 13:22:21 2007 +0000
     5.2 +++ b/xen/arch/x86/dmi_scan.c	Mon Jan 29 14:59:42 2007 +0000
     5.3 @@ -159,7 +159,7 @@ static void __init dmi_save_ident(struct
     5.4  		return;
     5.5  	dmi_ident[slot] = alloc_bootmem(strlen(p)+1);
     5.6  	if(dmi_ident[slot])
     5.7 -		strcpy(dmi_ident[slot], p);
     5.8 +		safe_strcpy(dmi_ident[slot], p);
     5.9  	else
    5.10  		printk(KERN_ERR "dmi_save_ident: out of memory.\n");
    5.11  }
     6.1 --- a/xen/arch/x86/domain_build.c	Mon Jan 29 13:22:21 2007 +0000
     6.2 +++ b/xen/arch/x86/domain_build.c	Mon Jan 29 14:59:42 2007 +0000
     6.3 @@ -871,7 +871,7 @@ int construct_dom0(struct domain *d,
     6.4  
     6.5      memset(si->cmd_line, 0, sizeof(si->cmd_line));
     6.6      if ( cmdline != NULL )
     6.7 -        strlcpy((char *)si->cmd_line, cmdline, sizeof(si->cmd_line));
     6.8 +        safe_strcpy(si->cmd_line, cmdline);
     6.9  
    6.10      if ( fill_console_start_info((void *)(si + 1)) )
    6.11      {
     7.1 --- a/xen/arch/x86/hvm/intercept.c	Mon Jan 29 13:22:21 2007 +0000
     7.2 +++ b/xen/arch/x86/hvm/intercept.c	Mon Jan 29 14:59:42 2007 +0000
     7.3 @@ -173,7 +173,7 @@ int hvm_register_savevm(struct domain *d
     7.4          return -1;
     7.5      }
     7.6  
     7.7 -    strlcpy(se->idstr, idstr, HVM_SE_IDSTR_LEN);
     7.8 +    safe_strcpy(se->idstr, idstr);
     7.9  
    7.10      se->instance_id = instance_id;
    7.11      se->version_id = version_id;
    7.12 @@ -224,7 +224,7 @@ int hvm_save(struct vcpu *v, hvm_domain_
    7.13  
    7.14      for(se = v->domain->arch.hvm_domain.first_se; se != NULL; se = se->next) {
    7.15          /* ID string */
    7.16 -        len = strnlen(se->idstr, HVM_SE_IDSTR_LEN);
    7.17 +        len = strnlen(se->idstr, sizeof(se->idstr));
    7.18          hvm_put_8u(h, len);
    7.19          hvm_put_buffer(h, se->idstr, len);
    7.20  
    7.21 @@ -262,7 +262,7 @@ static HVMStateEntry *find_se(struct dom
    7.22      HVMStateEntry *se;
    7.23  
    7.24      for(se = d->arch.hvm_domain.first_se; se != NULL; se = se->next) {
    7.25 -        if (!strncmp(se->idstr, idstr, HVM_SE_IDSTR_LEN) &&
    7.26 +        if (!strncmp(se->idstr, idstr, sizeof(se->idstr)) &&
    7.27              instance_id == se->instance_id){
    7.28              return se;
    7.29          }
     8.1 --- a/xen/arch/x86/setup.c	Mon Jan 29 13:22:21 2007 +0000
     8.2 +++ b/xen/arch/x86/setup.c	Mon Jan 29 14:59:42 2007 +0000
     8.3 @@ -111,7 +111,7 @@ char acpi_param[10] = "";
     8.4  static void parse_acpi_param(char *s)
     8.5  {
     8.6      /* Save the parameter so it can be propagated to domain0. */
     8.7 -    strlcpy(acpi_param, s, sizeof(acpi_param));
     8.8 +    safe_strcpy(acpi_param, s);
     8.9  
    8.10      /* Interpret the parameter for use within Xen. */
    8.11      if ( !strcmp(s, "off") )
    8.12 @@ -748,7 +748,7 @@ void __init __start_xen(multiboot_info_t
    8.13          if ( (cmdline = strchr(cmdline, ' ')) != NULL )
    8.14          {
    8.15              while ( *cmdline == ' ' ) cmdline++;
    8.16 -            strcpy(dom0_cmdline, cmdline);
    8.17 +            safe_strcpy(dom0_cmdline, cmdline);
    8.18          }
    8.19  
    8.20          cmdline = dom0_cmdline;
     9.1 --- a/xen/common/kexec.c	Mon Jan 29 13:22:21 2007 +0000
     9.2 +++ b/xen/common/kexec.c	Mon Jan 29 14:59:42 2007 +0000
     9.3 @@ -131,7 +131,7 @@ static __init int register_crashdump_tri
     9.4  
     9.5  static void setup_note(Elf_Note *n, const char *name, int type, int descsz)
     9.6  {
     9.7 -    strcpy(ELFNOTE_NAME(n), name);
     9.8 +    safe_strcpy(ELFNOTE_NAME(n), name);
     9.9      n->namesz = strlen(name);
    9.10      n->descsz = descsz;
    9.11      n->type = type;
    10.1 --- a/xen/common/keyhandler.c	Mon Jan 29 13:22:21 2007 +0000
    10.2 +++ b/xen/common/keyhandler.c	Mon Jan 29 14:59:42 2007 +0000
    10.3 @@ -67,8 +67,7 @@ void register_keyhandler(
    10.4      ASSERT(key_table[key].u.handler == NULL);
    10.5      key_table[key].u.handler = handler;
    10.6      key_table[key].flags     = 0;
    10.7 -    strlcpy(key_table[key].desc, desc, STR_MAX);
    10.8 -    key_table[key].desc[STR_MAX-1] = '\0';
    10.9 +    safe_strcpy(key_table[key].desc, desc);
   10.10  }
   10.11  
   10.12  void register_irq_keyhandler(
   10.13 @@ -77,7 +76,7 @@ void register_irq_keyhandler(
   10.14      ASSERT(key_table[key].u.irq_handler == NULL);
   10.15      key_table[key].u.irq_handler = handler;
   10.16      key_table[key].flags         = KEYHANDLER_IRQ_CALLBACK;
   10.17 -    strlcpy(key_table[key].desc, desc, STR_MAX);
   10.18 +    safe_strcpy(key_table[key].desc, desc);
   10.19  }
   10.20  
   10.21  static void show_handlers(unsigned char key)
    11.1 --- a/xen/common/libelf/libelf-dominfo.c	Mon Jan 29 13:22:21 2007 +0000
    11.2 +++ b/xen/common/libelf/libelf-dominfo.c	Mon Jan 29 14:59:42 2007 +0000
    11.3 @@ -128,16 +128,16 @@ int elf_xen_parse_note(struct elf_binary
    11.4      switch (type)
    11.5      {
    11.6      case XEN_ELFNOTE_LOADER:
    11.7 -	elf_strlcpy(parms->loader, str, sizeof(parms->loader));
    11.8 +	safe_strcpy(parms->loader, str);
    11.9  	break;
   11.10      case XEN_ELFNOTE_GUEST_OS:
   11.11 -	elf_strlcpy(parms->guest_os, str, sizeof(parms->guest_os));
   11.12 +	safe_strcpy(parms->guest_os, str);
   11.13  	break;
   11.14      case XEN_ELFNOTE_GUEST_VERSION:
   11.15 -	elf_strlcpy(parms->guest_ver, str, sizeof(parms->guest_ver));
   11.16 +	safe_strcpy(parms->guest_ver, str);
   11.17  	break;
   11.18      case XEN_ELFNOTE_XEN_VERSION:
   11.19 -	elf_strlcpy(parms->xen_ver, str, sizeof(parms->xen_ver));
   11.20 +	safe_strcpy(parms->xen_ver, str);
   11.21  	break;
   11.22      case XEN_ELFNOTE_PAE_MODE:
   11.23  	if (0 == strcmp(str, "yes"))
   11.24 @@ -224,13 +224,13 @@ int elf_xen_parse_guest_info(struct elf_
   11.25  
   11.26  	/* strings */
   11.27  	if (0 == strcmp(name, "LOADER"))
   11.28 -	    elf_strlcpy(parms->loader, value, sizeof(parms->loader));
   11.29 +	    safe_strcpy(parms->loader, value);
   11.30  	if (0 == strcmp(name, "GUEST_OS"))
   11.31 -	    elf_strlcpy(parms->guest_os, value, sizeof(parms->guest_os));
   11.32 +	    safe_strcpy(parms->guest_os, value);
   11.33  	if (0 == strcmp(name, "GUEST_VER"))
   11.34 -	    elf_strlcpy(parms->guest_ver, value, sizeof(parms->guest_ver));
   11.35 +	    safe_strcpy(parms->guest_ver, value);
   11.36  	if (0 == strcmp(name, "XEN_VER"))
   11.37 -	    elf_strlcpy(parms->xen_ver, value, sizeof(parms->xen_ver));
   11.38 +	    safe_strcpy(parms->xen_ver, value);
   11.39  	if (0 == strcmp(name, "PAE"))
   11.40  	{
   11.41  	    if (0 == strcmp(value, "yes[extended-cr3]"))
    12.1 --- a/xen/common/libelf/libelf-private.h	Mon Jan 29 13:22:21 2007 +0000
    12.2 +++ b/xen/common/libelf/libelf-private.h	Mon Jan 29 14:59:42 2007 +0000
    12.3 @@ -21,8 +21,6 @@
    12.4  #define bswap_32(x) swab32(x)
    12.5  #define bswap_64(x) swab64(x)
    12.6  
    12.7 -#define elf_strlcpy(d,s,c) strlcpy(d,s,c)
    12.8 -
    12.9  #else /* !__XEN__ */
   12.10  
   12.11  #include <stdio.h>
   12.12 @@ -52,13 +50,10 @@
   12.13          xc_set_error(XC_INVALID_KERNEL, fmt , ## args );  \
   12.14  	} while (0)
   12.15  
   12.16 -/* SysV unices have no strlcpy/strlcat. */
   12.17 -static inline size_t elf_strlcpy(char *dest, const char *src, size_t size)
   12.18 -{
   12.19 -    strncpy(dest, src, size-1);
   12.20 -    dest[size-1] = '\0';
   12.21 -    return strlen(src);
   12.22 -}
   12.23 +#define safe_strcpy(d,s)                        \
   12.24 +do { strncpy((d),(s),sizeof((d))-1);            \
   12.25 +     (d)[sizeof((d))-1] = '\0';                 \
   12.26 +} while (0)
   12.27  
   12.28  #endif
   12.29  
    13.1 --- a/xen/common/perfc.c	Mon Jan 29 13:22:21 2007 +0000
    13.2 +++ b/xen/common/perfc.c	Mon Jan 29 14:59:42 2007 +0000
    13.3 @@ -148,8 +148,7 @@ static int perfc_copy_info(XEN_GUEST_HAN
    13.4      {
    13.5          for ( i = 0; i < NR_PERFCTRS; i++ )
    13.6          {
    13.7 -            strlcpy(perfc_d[i].name, perfc_info[i].name,
    13.8 -                    sizeof(perfc_d[i].name));
    13.9 +            safe_strcpy(perfc_d[i].name, perfc_info[i].name);
   13.10  
   13.11              switch ( perfc_info[i].type )
   13.12              {
    14.1 --- a/xen/common/rangeset.c	Mon Jan 29 13:22:21 2007 +0000
    14.2 +++ b/xen/common/rangeset.c	Mon Jan 29 14:59:42 2007 +0000
    14.3 @@ -283,7 +283,7 @@ struct rangeset *rangeset_new(
    14.4  
    14.5      if ( name != NULL )
    14.6      {
    14.7 -        strlcpy(r->name, name, sizeof(r->name));
    14.8 +        safe_strcpy(r->name, name);
    14.9      }
   14.10      else
   14.11      {
    15.1 --- a/xen/drivers/char/console.c	Mon Jan 29 13:22:21 2007 +0000
    15.2 +++ b/xen/drivers/char/console.c	Mon Jan 29 14:59:42 2007 +0000
    15.3 @@ -481,7 +481,7 @@ void printk(const char *fmt, ...)
    15.4  
    15.5  void set_printk_prefix(const char *prefix)
    15.6  {
    15.7 -    strcpy(printk_prefix, prefix);
    15.8 +    safe_strcpy(printk_prefix, prefix);
    15.9  }
   15.10  
   15.11  void init_console(void)
   15.12 @@ -771,7 +771,7 @@ void debugtrace_printk(const char *fmt, 
   15.13  
   15.14      ASSERT(debugtrace_buf[debugtrace_bytes - 1] == 0);
   15.15  
   15.16 -    sprintf(buf, "%u ", ++count);
   15.17 +    snprintf(buf, sizeof(buf), "%u ", ++count);
   15.18  
   15.19      va_start(args, fmt);
   15.20      (void)vsnprintf(buf + strlen(buf), sizeof(buf), fmt, args);
    16.1 --- a/xen/include/xen/string.h	Mon Jan 29 13:22:21 2007 +0000
    16.2 +++ b/xen/include/xen/string.h	Mon Jan 29 14:59:42 2007 +0000
    16.3 @@ -82,9 +82,6 @@ extern void * memchr(const void *,int,__
    16.4  }
    16.5  #endif
    16.6  
    16.7 -#define safe_strcpy(d,s)                        \
    16.8 -do { strncpy((d),(s),sizeof((d)));              \
    16.9 -     (d)[sizeof((d))-1] = '\0';                 \
   16.10 -} while (0)
   16.11 +#define safe_strcpy(d, s) strlcpy(d, s, sizeof(d))
   16.12  
   16.13  #endif /* _LINUX_STRING_H_ */