libbb: add a function to make a copy of a region of memory
authorRon Yorston <rmy@frippery.org>
Sun, 19 Jul 2015 21:05:20 +0000 (23:05 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sun, 19 Jul 2015 21:05:20 +0000 (23:05 +0200)
Introduce a library routine to package the idiom:

    p = xmalloc(b, n);
    memcpy(p, b, n);

and use it where possible.  The example in traceroute used xzalloc
but it didn't need to.

function                                             old     new   delta
xmemdup                                                -      32     +32
last_main                                            834     826      -8
make_device                                         2321    2311     -10
common_traceroute_main                              3698    3685     -13
readtoken1                                          3182    3168     -14
procps_scan                                         1222    1206     -16
forkchild                                            655     638     -17
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/6 up/down: 32/-78)            Total: -46 bytes

Signed-off-by: Ron Yorston <rmy@frippery.org>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
include/libbb.h
libbb/procps.c
libbb/xfuncs_printf.c
miscutils/last_fancy.c
networking/traceroute.c
networking/udhcp/d6_dhcpc.c
shell/ash.c
util-linux/mdev.c

index f0ac1f50d4ccaeea73aed8bf22bd09f38b9f43a2..54d01b75a09da4ba816d42f86ca3dc1b5a29b5cf 100644 (file)
@@ -673,6 +673,7 @@ uint16_t inet_cksum(uint16_t *addr, int len) FAST_FUNC;
 
 char *xstrdup(const char *s) FAST_FUNC RETURNS_MALLOC;
 char *xstrndup(const char *s, int n) FAST_FUNC RETURNS_MALLOC;
+void *xmemdup(const void *s, int n) FAST_FUNC RETURNS_MALLOC;
 void overlapping_strcpy(char *dst, const char *src) FAST_FUNC;
 char *safe_strncpy(char *dst, const char *src, size_t size) FAST_FUNC;
 char *strncpy_IFNAMSIZ(char *dst, const char *src) FAST_FUNC;
index 3d335b37b6968e7343d9c09a53f8b160d8376f1c..71ad071e6692fe4ba71eaa3b1e19f42e2ff698c6 100644 (file)
@@ -554,8 +554,7 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
                                break;
                        if (flags & PSSCAN_ARGVN) {
                                sp->argv_len = n;
-                               sp->argv0 = xmalloc(n + 1);
-                               memcpy(sp->argv0, buf, n + 1);
+                               sp->argv0 = xmemdup(buf, n + 1);
                                /* sp->argv0[n] = '\0'; - buf has it */
                        } else {
                                sp->argv_len = 0;
index e4ac6a002a56854550ac4758238133cdaa51ea70..73488908d99abcbe3e53ca9199b6fdd7a6c996d4 100644 (file)
@@ -112,6 +112,11 @@ char* FAST_FUNC xstrndup(const char *s, int n)
        return memcpy(t, s, n);
 }
 
+void* FAST_FUNC xmemdup(const void *s, int n)
+{
+       return memcpy(xmalloc(n), s, n);
+}
+
 // Die if we can't open a file and return a FILE* to it.
 // Notice we haven't got xfread(), This is for use with fscanf() and friends.
 FILE* FAST_FUNC xfopen(const char *path, const char *mode)
index 8194e31b5675e51d0db57e7601fbabb03745086a..e56e0ba85995772ecf5f28e3ea33598e0ee4d223 100644 (file)
@@ -233,7 +233,7 @@ int last_main(int argc UNUSED_PARAM, char **argv)
                                break;
                        }
                        /* add_entry */
-                       llist_add_to(&zlist, memcpy(xmalloc(sizeof(ut)), &ut, sizeof(ut)));
+                       llist_add_to(&zlist, xmemdup(&ut, sizeof(ut)));
                        break;
                case USER_PROCESS: {
                        int show;
@@ -275,7 +275,7 @@ int last_main(int argc UNUSED_PARAM, char **argv)
                                show_entry(&ut, state, boot_time);
                        }
                        /* add_entry */
-                       llist_add_to(&zlist, memcpy(xmalloc(sizeof(ut)), &ut, sizeof(ut)));
+                       llist_add_to(&zlist, xmemdup(&ut, sizeof(ut)));
                        break;
                }
                }
index 97a7a19e0b4044419803a7a3f5f720c411db0f7c..12ba614e8b413ad373d9937ab49696e0a748ce16 100644 (file)
@@ -387,15 +387,6 @@ struct globals {
 #define outudp  ((struct udphdr *)(outip + 1))
 
 
-/* libbb candidate? tftp uses this idiom too */
-static len_and_sockaddr* dup_sockaddr(const len_and_sockaddr *lsa)
-{
-       len_and_sockaddr *new_lsa = xzalloc(LSA_LEN_SIZE + lsa->len);
-       memcpy(new_lsa, lsa, LSA_LEN_SIZE + lsa->len);
-       return new_lsa;
-}
-
-
 static int
 wait_for_reply(len_and_sockaddr *from_lsa, struct sockaddr *to, unsigned *timestamp_us, int *left_ms)
 {
@@ -1074,7 +1065,7 @@ common_traceroute_main(int op, char **argv)
                printf(" from %s", source);
        printf(", %d hops max, %d byte packets\n", max_ttl, packlen);
 
-       from_lsa = dup_sockaddr(dest_lsa);
+       from_lsa = xmemdup(dest_lsa, LSA_LEN_SIZE + dest_lsa->len);
        lastaddr = xzalloc(dest_lsa->len);
        to = xzalloc(dest_lsa->len);
        seq = 0;
index 044f04673da0477d456f39bb3381272f5ee076c0..4e9b705b9ebf3617db4975ab9f999345580f5da0 100644 (file)
@@ -118,7 +118,7 @@ static void *d6_copy_option(uint8_t *option, uint8_t *option_end, unsigned code)
        uint8_t *opt = d6_find_option(option, option_end, code);
        if (!opt)
                return opt;
-       return memcpy(xmalloc(opt[3] + 4), opt, opt[3] + 4);
+       return xmemdup(opt, opt[3] + 4);
 }
 
 static void *d6_store_blob(void *dst, const void *src, unsigned len)
index f4779ee2bdb5417d77a4e6e12209a7d93565572d..f6190c3e250cc6b2cce219c1be88db602fecbad1 100644 (file)
@@ -4751,7 +4751,7 @@ forkchild(struct job *jp, union node *n, int mode)
                 * Our solution: ONLY bare $(trap) or `trap` is special.
                 */
                /* Save trap handler strings for trap builtin to print */
-               trap_ptr = memcpy(xmalloc(sizeof(trap)), trap, sizeof(trap));
+               trap_ptr = xmemdup(trap, sizeof(trap));
                /* Fall through into clearing traps */
        }
        clear_traps();
index ca4b9151082f41816849f91631b57b9964ba95fd..662e8ab38b0762256439f531296c94195b27de03 100644 (file)
@@ -471,7 +471,7 @@ static const struct rule *next_rule(void)
        if (G.parser) {
                parse_next_rule();
                if (G.rule_vec) { /* mdev -s */
-                       rule = memcpy(xmalloc(sizeof(G.cur_rule)), &G.cur_rule, sizeof(G.cur_rule));
+                       rule = xmemdup(&G.cur_rule, sizeof(G.cur_rule));
                        G.rule_vec = xrealloc_vector(G.rule_vec, 4, G.rule_idx);
                        G.rule_vec[G.rule_idx++] = rule;
                        dbg3("> G.rule_vec[G.rule_idx:%d]=%p", G.rule_idx, G.rule_vec[G.rule_idx]);