[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] [xen-unstable] Remove uses of strcpy and strncpy from common and x86 code.



# HG changeset patch
# User kfraser@xxxxxxxxxxxxxxxxxxxxx
# Date 1170082782 0
# Node ID bef7fbe25a9f380d40623795befacd447b026837
# Parent  5bb0840984936749b4e99f53931d72a8579d302c
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@xxxxxxx>
Signed-off-by: Keir Fraser <keir@xxxxxxxxxxxxx>
---
 xen/acm/acm_core.c                 |   12 ++++++++----
 xen/arch/x86/cpu/common.c          |    6 +++---
 xen/arch/x86/cpu/cyrix.c           |    7 +++----
 xen/arch/x86/cpu/intel.c           |    2 +-
 xen/arch/x86/dmi_scan.c            |    2 +-
 xen/arch/x86/domain_build.c        |    2 +-
 xen/arch/x86/hvm/intercept.c       |    6 +++---
 xen/arch/x86/setup.c               |    4 ++--
 xen/common/kexec.c                 |    2 +-
 xen/common/keyhandler.c            |    5 ++---
 xen/common/libelf/libelf-dominfo.c |   16 ++++++++--------
 xen/common/libelf/libelf-private.h |   13 ++++---------
 xen/common/perfc.c                 |    3 +--
 xen/common/rangeset.c              |    2 +-
 xen/drivers/char/console.c         |    4 ++--
 xen/include/xen/string.h           |    5 +----
 16 files changed, 42 insertions(+), 49 deletions(-)

diff -r 5bb084098493 -r bef7fbe25a9f xen/acm/acm_core.c
--- a/xen/acm/acm_core.c        Mon Jan 29 13:22:21 2007 +0000
+++ b/xen/acm/acm_core.c        Mon Jan 29 14:59:42 2007 +0000
@@ -89,8 +89,11 @@ acm_set_policy_reference(u8 * buf, u32 b
     if (!acm_bin_pol.policy_reference_name)
         return -ENOMEM;
 
-    strcpy(acm_bin_pol.policy_reference_name, (char *)(buf + sizeof(struct 
acm_policy_reference_buffer)));
-    printk("%s: Activating policy %s\n", __func__, 
acm_bin_pol.policy_reference_name);
+    strlcpy(acm_bin_pol.policy_reference_name,
+            (char *)(buf + sizeof(struct acm_policy_reference_buffer)),
+            ntohl(pr->len));
+    printk("%s: Activating policy %s\n", __func__,
+           acm_bin_pol.policy_reference_name);
     return 0;
 }
 
@@ -106,8 +109,9 @@ acm_dump_policy_reference(u8 *buf, u32 b
 
     memset(buf, 0, ret);
     pr_buf->len = htonl(strlen(acm_bin_pol.policy_reference_name) + 1); /* 
including stringend '\0' */
-    strcpy((char *)(buf + sizeof(struct acm_policy_reference_buffer)),
-           acm_bin_pol.policy_reference_name);
+    strlcpy((char *)(buf + sizeof(struct acm_policy_reference_buffer)),
+            acm_bin_pol.policy_reference_name,
+            ntohl(pr_buf->len));
     return ret;
 }
 
diff -r 5bb084098493 -r bef7fbe25a9f xen/arch/x86/cpu/common.c
--- a/xen/arch/x86/cpu/common.c Mon Jan 29 13:22:21 2007 +0000
+++ b/xen/arch/x86/cpu/common.c Mon Jan 29 14:59:42 2007 +0000
@@ -30,9 +30,9 @@ static void default_init(struct cpuinfo_
        if (c->cpuid_level == -1) {
                /* No cpuid. It must be an ancient CPU */
                if (c->x86 == 4)
-                       strcpy(c->x86_model_id, "486");
+                       safe_strcpy(c->x86_model_id, "486");
                else if (c->x86 == 3)
-                       strcpy(c->x86_model_id, "386");
+                       safe_strcpy(c->x86_model_id, "386");
        }
 }
 
@@ -383,7 +383,7 @@ void __devinit identify_cpu(struct cpuin
                char *p;
                p = table_lookup_model(c);
                if ( p )
-                       strcpy(c->x86_model_id, p);
+                       safe_strcpy(c->x86_model_id, p);
                else
                        /* Last resort... */
                        snprintf(c->x86_model_id, sizeof(c->x86_model_id),
diff -r 5bb084098493 -r bef7fbe25a9f xen/arch/x86/cpu/cyrix.c
--- a/xen/arch/x86/cpu/cyrix.c  Mon Jan 29 13:22:21 2007 +0000
+++ b/xen/arch/x86/cpu/cyrix.c  Mon Jan 29 14:59:42 2007 +0000
@@ -181,7 +181,6 @@ static void __init init_cyrix(struct cpu
 static void __init init_cyrix(struct cpuinfo_x86 *c)
 {
        unsigned char dir0, dir0_msn, dir0_lsn, dir1 = 0;
-       char *buf = c->x86_model_id;
        const char *p = NULL;
 
        /* Bit 31 in normal CPUID used for nonstandard 3DNow ID;
@@ -302,8 +301,8 @@ static void __init init_cyrix(struct cpu
                dir0_msn = 7;
                break;
        }
-       strcpy(buf, Cx86_model[dir0_msn & 7]);
-       if (p) strcat(buf, p);
+       safe_strcpy(c->x86_model_id, Cx86_model[dir0_msn & 7]);
+       if (p) strcat(c->x86_model_id, p);
        return;
 }
 
@@ -339,7 +338,7 @@ static void cyrix_identify(struct cpuinf
        if ( c->x86 == 4 && test_cyrix_52div() ) {
                unsigned char dir0, dir1;
                
-               strcpy(c->x86_vendor_id, "CyrixInstead");
+               safe_strcpy(c->x86_vendor_id, "CyrixInstead");
                c->x86_vendor = X86_VENDOR_CYRIX;
                
                /* Actually enable cpuid on the older cyrix */
diff -r 5bb084098493 -r bef7fbe25a9f xen/arch/x86/cpu/intel.c
--- a/xen/arch/x86/cpu/intel.c  Mon Jan 29 13:22:21 2007 +0000
+++ b/xen/arch/x86/cpu/intel.c  Mon Jan 29 14:59:42 2007 +0000
@@ -152,7 +152,7 @@ static void __devinit init_intel(struct 
        }
 
        if ( p )
-               strcpy(c->x86_model_id, p);
+               safe_strcpy(c->x86_model_id, p);
        
        c->x86_max_cores = num_cpu_cores(c);
 
diff -r 5bb084098493 -r bef7fbe25a9f xen/arch/x86/dmi_scan.c
--- a/xen/arch/x86/dmi_scan.c   Mon Jan 29 13:22:21 2007 +0000
+++ b/xen/arch/x86/dmi_scan.c   Mon Jan 29 14:59:42 2007 +0000
@@ -159,7 +159,7 @@ static void __init dmi_save_ident(struct
                return;
        dmi_ident[slot] = alloc_bootmem(strlen(p)+1);
        if(dmi_ident[slot])
-               strcpy(dmi_ident[slot], p);
+               safe_strcpy(dmi_ident[slot], p);
        else
                printk(KERN_ERR "dmi_save_ident: out of memory.\n");
 }
diff -r 5bb084098493 -r bef7fbe25a9f xen/arch/x86/domain_build.c
--- a/xen/arch/x86/domain_build.c       Mon Jan 29 13:22:21 2007 +0000
+++ b/xen/arch/x86/domain_build.c       Mon Jan 29 14:59:42 2007 +0000
@@ -871,7 +871,7 @@ int construct_dom0(struct domain *d,
 
     memset(si->cmd_line, 0, sizeof(si->cmd_line));
     if ( cmdline != NULL )
-        strlcpy((char *)si->cmd_line, cmdline, sizeof(si->cmd_line));
+        safe_strcpy(si->cmd_line, cmdline);
 
     if ( fill_console_start_info((void *)(si + 1)) )
     {
diff -r 5bb084098493 -r bef7fbe25a9f xen/arch/x86/hvm/intercept.c
--- a/xen/arch/x86/hvm/intercept.c      Mon Jan 29 13:22:21 2007 +0000
+++ b/xen/arch/x86/hvm/intercept.c      Mon Jan 29 14:59:42 2007 +0000
@@ -173,7 +173,7 @@ int hvm_register_savevm(struct domain *d
         return -1;
     }
 
-    strlcpy(se->idstr, idstr, HVM_SE_IDSTR_LEN);
+    safe_strcpy(se->idstr, idstr);
 
     se->instance_id = instance_id;
     se->version_id = version_id;
@@ -224,7 +224,7 @@ int hvm_save(struct vcpu *v, hvm_domain_
 
     for(se = v->domain->arch.hvm_domain.first_se; se != NULL; se = se->next) {
         /* ID string */
-        len = strnlen(se->idstr, HVM_SE_IDSTR_LEN);
+        len = strnlen(se->idstr, sizeof(se->idstr));
         hvm_put_8u(h, len);
         hvm_put_buffer(h, se->idstr, len);
 
@@ -262,7 +262,7 @@ static HVMStateEntry *find_se(struct dom
     HVMStateEntry *se;
 
     for(se = d->arch.hvm_domain.first_se; se != NULL; se = se->next) {
-        if (!strncmp(se->idstr, idstr, HVM_SE_IDSTR_LEN) &&
+        if (!strncmp(se->idstr, idstr, sizeof(se->idstr)) &&
             instance_id == se->instance_id){
             return se;
         }
diff -r 5bb084098493 -r bef7fbe25a9f xen/arch/x86/setup.c
--- a/xen/arch/x86/setup.c      Mon Jan 29 13:22:21 2007 +0000
+++ b/xen/arch/x86/setup.c      Mon Jan 29 14:59:42 2007 +0000
@@ -111,7 +111,7 @@ static void parse_acpi_param(char *s)
 static void parse_acpi_param(char *s)
 {
     /* Save the parameter so it can be propagated to domain0. */
-    strlcpy(acpi_param, s, sizeof(acpi_param));
+    safe_strcpy(acpi_param, s);
 
     /* Interpret the parameter for use within Xen. */
     if ( !strcmp(s, "off") )
@@ -748,7 +748,7 @@ void __init __start_xen(multiboot_info_t
         if ( (cmdline = strchr(cmdline, ' ')) != NULL )
         {
             while ( *cmdline == ' ' ) cmdline++;
-            strcpy(dom0_cmdline, cmdline);
+            safe_strcpy(dom0_cmdline, cmdline);
         }
 
         cmdline = dom0_cmdline;
diff -r 5bb084098493 -r bef7fbe25a9f xen/common/kexec.c
--- a/xen/common/kexec.c        Mon Jan 29 13:22:21 2007 +0000
+++ b/xen/common/kexec.c        Mon Jan 29 14:59:42 2007 +0000
@@ -131,7 +131,7 @@ __initcall(register_crashdump_trigger);
 
 static void setup_note(Elf_Note *n, const char *name, int type, int descsz)
 {
-    strcpy(ELFNOTE_NAME(n), name);
+    safe_strcpy(ELFNOTE_NAME(n), name);
     n->namesz = strlen(name);
     n->descsz = descsz;
     n->type = type;
diff -r 5bb084098493 -r bef7fbe25a9f xen/common/keyhandler.c
--- a/xen/common/keyhandler.c   Mon Jan 29 13:22:21 2007 +0000
+++ b/xen/common/keyhandler.c   Mon Jan 29 14:59:42 2007 +0000
@@ -67,8 +67,7 @@ void register_keyhandler(
     ASSERT(key_table[key].u.handler == NULL);
     key_table[key].u.handler = handler;
     key_table[key].flags     = 0;
-    strlcpy(key_table[key].desc, desc, STR_MAX);
-    key_table[key].desc[STR_MAX-1] = '\0';
+    safe_strcpy(key_table[key].desc, desc);
 }
 
 void register_irq_keyhandler(
@@ -77,7 +76,7 @@ void register_irq_keyhandler(
     ASSERT(key_table[key].u.irq_handler == NULL);
     key_table[key].u.irq_handler = handler;
     key_table[key].flags         = KEYHANDLER_IRQ_CALLBACK;
-    strlcpy(key_table[key].desc, desc, STR_MAX);
+    safe_strcpy(key_table[key].desc, desc);
 }
 
 static void show_handlers(unsigned char key)
diff -r 5bb084098493 -r bef7fbe25a9f xen/common/libelf/libelf-dominfo.c
--- a/xen/common/libelf/libelf-dominfo.c        Mon Jan 29 13:22:21 2007 +0000
+++ b/xen/common/libelf/libelf-dominfo.c        Mon Jan 29 14:59:42 2007 +0000
@@ -128,16 +128,16 @@ int elf_xen_parse_note(struct elf_binary
     switch (type)
     {
     case XEN_ELFNOTE_LOADER:
-       elf_strlcpy(parms->loader, str, sizeof(parms->loader));
+       safe_strcpy(parms->loader, str);
        break;
     case XEN_ELFNOTE_GUEST_OS:
-       elf_strlcpy(parms->guest_os, str, sizeof(parms->guest_os));
+       safe_strcpy(parms->guest_os, str);
        break;
     case XEN_ELFNOTE_GUEST_VERSION:
-       elf_strlcpy(parms->guest_ver, str, sizeof(parms->guest_ver));
+       safe_strcpy(parms->guest_ver, str);
        break;
     case XEN_ELFNOTE_XEN_VERSION:
-       elf_strlcpy(parms->xen_ver, str, sizeof(parms->xen_ver));
+       safe_strcpy(parms->xen_ver, str);
        break;
     case XEN_ELFNOTE_PAE_MODE:
        if (0 == strcmp(str, "yes"))
@@ -224,13 +224,13 @@ int elf_xen_parse_guest_info(struct elf_
 
        /* strings */
        if (0 == strcmp(name, "LOADER"))
-           elf_strlcpy(parms->loader, value, sizeof(parms->loader));
+           safe_strcpy(parms->loader, value);
        if (0 == strcmp(name, "GUEST_OS"))
-           elf_strlcpy(parms->guest_os, value, sizeof(parms->guest_os));
+           safe_strcpy(parms->guest_os, value);
        if (0 == strcmp(name, "GUEST_VER"))
-           elf_strlcpy(parms->guest_ver, value, sizeof(parms->guest_ver));
+           safe_strcpy(parms->guest_ver, value);
        if (0 == strcmp(name, "XEN_VER"))
-           elf_strlcpy(parms->xen_ver, value, sizeof(parms->xen_ver));
+           safe_strcpy(parms->xen_ver, value);
        if (0 == strcmp(name, "PAE"))
        {
            if (0 == strcmp(value, "yes[extended-cr3]"))
diff -r 5bb084098493 -r bef7fbe25a9f xen/common/libelf/libelf-private.h
--- a/xen/common/libelf/libelf-private.h        Mon Jan 29 13:22:21 2007 +0000
+++ b/xen/common/libelf/libelf-private.h        Mon Jan 29 14:59:42 2007 +0000
@@ -20,8 +20,6 @@
 #define bswap_16(x) swab16(x)
 #define bswap_32(x) swab32(x)
 #define bswap_64(x) swab64(x)
-
-#define elf_strlcpy(d,s,c) strlcpy(d,s,c)
 
 #else /* !__XEN__ */
 
@@ -52,13 +50,10 @@
         xc_set_error(XC_INVALID_KERNEL, fmt , ## args );  \
        } while (0)
 
-/* SysV unices have no strlcpy/strlcat. */
-static inline size_t elf_strlcpy(char *dest, const char *src, size_t size)
-{
-    strncpy(dest, src, size-1);
-    dest[size-1] = '\0';
-    return strlen(src);
-}
+#define safe_strcpy(d,s)                        \
+do { strncpy((d),(s),sizeof((d))-1);            \
+     (d)[sizeof((d))-1] = '\0';                 \
+} while (0)
 
 #endif
 
diff -r 5bb084098493 -r bef7fbe25a9f xen/common/perfc.c
--- a/xen/common/perfc.c        Mon Jan 29 13:22:21 2007 +0000
+++ b/xen/common/perfc.c        Mon Jan 29 14:59:42 2007 +0000
@@ -148,8 +148,7 @@ static int perfc_copy_info(XEN_GUEST_HAN
     {
         for ( i = 0; i < NR_PERFCTRS; i++ )
         {
-            strlcpy(perfc_d[i].name, perfc_info[i].name,
-                    sizeof(perfc_d[i].name));
+            safe_strcpy(perfc_d[i].name, perfc_info[i].name);
 
             switch ( perfc_info[i].type )
             {
diff -r 5bb084098493 -r bef7fbe25a9f xen/common/rangeset.c
--- a/xen/common/rangeset.c     Mon Jan 29 13:22:21 2007 +0000
+++ b/xen/common/rangeset.c     Mon Jan 29 14:59:42 2007 +0000
@@ -283,7 +283,7 @@ struct rangeset *rangeset_new(
 
     if ( name != NULL )
     {
-        strlcpy(r->name, name, sizeof(r->name));
+        safe_strcpy(r->name, name);
     }
     else
     {
diff -r 5bb084098493 -r bef7fbe25a9f xen/drivers/char/console.c
--- a/xen/drivers/char/console.c        Mon Jan 29 13:22:21 2007 +0000
+++ b/xen/drivers/char/console.c        Mon Jan 29 14:59:42 2007 +0000
@@ -481,7 +481,7 @@ void printk(const char *fmt, ...)
 
 void set_printk_prefix(const char *prefix)
 {
-    strcpy(printk_prefix, prefix);
+    safe_strcpy(printk_prefix, prefix);
 }
 
 void init_console(void)
@@ -771,7 +771,7 @@ void debugtrace_printk(const char *fmt, 
 
     ASSERT(debugtrace_buf[debugtrace_bytes - 1] == 0);
 
-    sprintf(buf, "%u ", ++count);
+    snprintf(buf, sizeof(buf), "%u ", ++count);
 
     va_start(args, fmt);
     (void)vsnprintf(buf + strlen(buf), sizeof(buf), fmt, args);
diff -r 5bb084098493 -r bef7fbe25a9f xen/include/xen/string.h
--- a/xen/include/xen/string.h  Mon Jan 29 13:22:21 2007 +0000
+++ b/xen/include/xen/string.h  Mon Jan 29 14:59:42 2007 +0000
@@ -82,9 +82,6 @@ extern void * memchr(const void *,int,__
 }
 #endif
 
-#define safe_strcpy(d,s)                        \
-do { strncpy((d),(s),sizeof((d)));              \
-     (d)[sizeof((d))-1] = '\0';                 \
-} while (0)
+#define safe_strcpy(d, s) strlcpy(d, s, sizeof(d))
 
 #endif /* _LINUX_STRING_H_ */

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.