Applied vodz' patches #49 and #50 (with a small correction in runshell.c)
authorRobert Griebl <griebl@gmx.de>
Fri, 19 Jul 2002 00:05:54 +0000 (00:05 -0000)
committerRobert Griebl <griebl@gmx.de>
Fri, 19 Jul 2002 00:05:54 +0000 (00:05 -0000)
#49: I found one memory overflow and memory leak in "ln" applet.
Last patch reduced also 54 bytes. ;)

#50: I found bug in loginutils/Makefile.in.
New patch have also new function to libbb and
aplied this to applets and other cosmetic changes.

18 files changed:
Makefile
archival/gunzip.c
archival/libunarchive/get_header_ar.c
archival/libunarchive/unarchive.c
coreutils/expr.c
coreutils/ln.c
include/libbb.h
libbb/Makefile.in
libbb/concat_path_file.c
libbb/run_shell.c
libbb/xconnect.c
libbb/xreadlink.c
loginutils/Makefile.in
loginutils/getty.c
loginutils/passwd.c
modutils/insmod.c
shell/cmdedit.c
util-linux/getopt.c

index a112e57810a0b504170b326dd5c5e1da1d98bbe7..1e1eddbc53558677b94a16024d8dc029ecbf8fd7 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -47,7 +47,7 @@ endif
 
 
 busybox: depend $(libraries-y)
-       $(CC) $(LDFLAGS) $(libraries-y) $(LIBRARIES) -o $@
+       $(CC) $(LDFLAGS) -o $@ $(libraries-y) $(LIBRARIES)
        $(STRIPCMD) $@
 
 busybox.links: applets/busybox.mkll
index 83ed5e84af5fe8dd07eb61627ab26c175ae42547..4ab197f098ea5163277c5816ff2499b38713e78b 100644 (file)
@@ -119,8 +119,7 @@ static int gunzip_file (const char *path, int flags)
        } else {
            error_msg_and_die("Invalid extension");
        }
-       out_path = (char *) xcalloc(sizeof(char), length + 1);
-       strncpy(out_path, path, length);
+       out_path = xstrndup(path, length);
                        
        /* Open output file */
        out_file = xfopen(out_path, "w");
index 1618b767f54a44ac302a1955a6271c4ca3144913..f172fa7c9f95d82241ec8d19574f6fcd6207645c 100644 (file)
@@ -91,8 +91,7 @@ file_header_t *get_header_ar(FILE *src_stream)
                }
        } else {
                /* short filenames */
-               typed->name = xcalloc(1, 16);
-               strncpy(typed->name, ar.formated.name, 16);
+               typed->name = xstrndup(ar.formated.name, 16);
        }
        typed->name[strcspn(typed->name, " /")]='\0';
 
index 312b498a13e448c57e032af9e5d01d72708a970e..03e3c3ec2934abb8eadb065679a7bf5519fd51d2 100644 (file)
@@ -58,9 +58,7 @@ char *extract_archive(FILE *src_stream, FILE *out_stream, const file_header_t *f
                                return(NULL);
                        }
                }
-               full_name = xmalloc(strlen(prefix) + strlen(path) + 1);
-               strcpy(full_name, prefix);
-               strcat(full_name, path);
+               bb_asprintf(&full_name, "%s%s", prefix, path);
        } else {
                full_name = file_entry->name;
        }
index 0299cc73acea7e24597d56a9f48cd6049253d328..55ae3a969eda90a743feb7180aa75cc3b8bfcbf0 100644 (file)
@@ -146,12 +146,8 @@ static int null (VALUE *v)
 
 static void tostring (VALUE *v)
 {
-       char *temp;
-
        if (v->type == integer) {
-               temp = xmalloc (4 * (sizeof (int) / sizeof (char)));
-               sprintf (temp, "%d", v->u.i);
-               v->u.s = temp;
+               bb_asprintf (&(v->u.s), "%d", v->u.i);
                v->type = string;
        }
 }
@@ -377,9 +373,7 @@ static VALUE *eval6 (void)
                else {
                        v = xmalloc (sizeof(VALUE));
                        v->type = string;
-                       v->u.s = strncpy ((char *) xmalloc (i2->u.i + 1),
-                               l->u.s + i1->u.i - 1, i2->u.i);
-                               v->u.s[i2->u.i] = 0;
+                       v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
                }
                freev (l);
                freev (i1);
index 1eb853d2fd23bbe893e59c55de4250c0c698eedf..427ffcc6ee2d8f7d1447ef4c5021993f35593466 100644 (file)
@@ -43,45 +43,47 @@ static int fs_link(const char *link_destname, const char *link_srcname,
 {
        int status;
        int src_is_dir;
-       char *src_name;
+       char *src_name = 0;
+       const char *src;
 
        if (link_destname==NULL)
                return(FALSE);
 
-       src_name = (char *) xmalloc(strlen(link_srcname)+strlen(link_destname)+1);
-
        if (link_srcname==NULL)
-               strcpy(src_name, link_destname);
+               src = link_destname;
        else
-               strcpy(src_name, link_srcname);
+               src = link_srcname;
 
        if (flag&LN_NODEREFERENCE)
-               src_is_dir = is_directory(src_name, TRUE, NULL);
+               src_is_dir = is_directory(src, TRUE, NULL);
        else
-               src_is_dir = is_directory(src_name, FALSE, NULL);       
+               src_is_dir = is_directory(src, FALSE, NULL);
        
        if ((src_is_dir==TRUE)&&((flag&LN_NODEREFERENCE)==0)) {
                char* srcdir_name;
 
                srcdir_name = xstrdup(link_destname);
-               strcat(src_name, "/");
-               strcat(src_name, get_last_path_component(srcdir_name));
+               src_name = concat_path_file(src, get_last_path_component(srcdir_name));
+               src = src_name;
                free(srcdir_name);
        }
        
        if (flag&LN_FORCE)
-               unlink(src_name);
+               unlink(src);
                
        if (flag&LN_SYMLINK)
-               status = symlink(link_destname, src_name);
+               status = symlink(link_destname, src);
        else
-               status = link(link_destname, src_name);
+               status = link(link_destname, src);
 
        if (status != 0) {
-               perror_msg(src_name);
-               return(FALSE);
+               perror_msg(src);
+               status = FALSE;
+       } else {
+               status = TRUE;
        }
-       return(TRUE);
+       free(src_name);
+       return status;
 }
 
 extern int ln_main(int argc, char **argv)
index 02babb884bfa26ff6c64250a05be1af78b298b0c..55a0ff809cc39dc1ad455758f5e28830bc7b2243 100644 (file)
@@ -321,6 +321,8 @@ void reset_ino_dev_hashtable(void);
 extern size_t xstrlen(const char *string);
 #define strlen(x)   xstrlen(x)
 
+void bb_asprintf(char **string_ptr, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
+
 
 #define FAIL_DELAY    3
 extern void change_identity ( const struct passwd *pw );
index 70cc26dc10b6e22aff08b700d5d30642249fb537..e7ca9aa55e46263329efd8e4fe7397e0bcfeedca 100644 (file)
@@ -41,7 +41,7 @@ LIBBB_SRC:= \
        simplify_path.c inet_common.c inode_hash.c obscure.c pwd2spwd.c xfuncs.c \
        correct_password.c change_identity.c setup_environment.c run_shell.c \
        pw_encrypt.c restricted_shell.c xgethostbyname2.c create_icmp6_socket.c \
-       xconnect.c
+       xconnect.c bb_asprintf.c
 
 LIBBB_OBJS=$(patsubst %.c,$(LIBBB_DIR)%.o, $(LIBBB_SRC))
 
index e62b99ef689a39a9bea136dfc7589e25097d8953..0146606a1db50f89defb4c4431c7cda2495bf97e 100644 (file)
@@ -38,8 +38,7 @@ extern char *concat_path_file(const char *path, const char *filename)
        lc = last_char_is(path, '/');
        while (*filename == '/')
                filename++;
-       outbuf = xmalloc(strlen(path)+strlen(filename)+1+(lc==NULL));
-       sprintf(outbuf, "%s%s%s", path, (lc==NULL)? "/" : "", filename);
+       bb_asprintf(&outbuf, "%s%s%s", path, (lc==NULL)? "/" : "", filename);
 
        return outbuf;
 }
index 30050fecb16faf425f4f42f3ece600b5e6af92ca..b26eba1152397989a5191bec8f6a68ad0777a9e9 100644 (file)
@@ -60,9 +60,8 @@ void run_shell ( const char *shell, int loginshell, const char *command, const c
        args [0] = get_last_path_component ( xstrdup ( shell ));
        
        if ( loginshell ) {
-               char *args0 = xmalloc ( xstrlen ( args [0] ) + 2 );
-               args0 [0] = '-';
-               strcpy ( args0 + 1, args [0] );
+               char *args0;
+               bb_asprintf ( &args0, "-%s", args [0] );
                args [0] = args0;
        }
     
index d6d144f3f99243c2a14a4a72d523253fe1b6b534..9e771495d671f9147f4b5de2496e986dae885477 100644 (file)
@@ -6,6 +6,7 @@
  *
  */
 
+#include "inet_common.h"
 #include <unistd.h>
 #include <string.h>
 #include <stdlib.h>
index 932e487a59d718093cb29f9d4967d8dc504772e8..9944b5129409f673b15ecb90372e3ceda6fa6364 100644 (file)
@@ -24,7 +24,7 @@ extern char *xreadlink(const char *path)
                buf = xrealloc(buf, bufsize += GROWBY);
                readsize = readlink(path, buf, bufsize); /* 1st try */
                if (readsize == -1) {
-                   perror_msg("%s:%s", applet_name, path);
+                   perror_msg("%s", path);
                    return NULL;
                }
        }           
@@ -34,4 +34,3 @@ extern char *xreadlink(const char *path)
 
        return buf;
 }       
-
index cb6452c39db5078a6a4c4c2432cde5598838afe7..1c5680aa52f6e2688f4fae1c8fb321250abe3bce 100644 (file)
@@ -40,7 +40,7 @@ needcrypt-$(CONFIG_LOGIN) := y
 needcrypt-$(CONFIG_SU)    := y
 
 ifeq ($(needcrypt-y),y)
-  libraries-y +=-lcrypt
+  LIBRARIES += -lcrypt
 endif
 
 $(LOGINUTILS_DIR)$(LOGINUTILS_AR): $(patsubst %,$(LOGINUTILS_DIR)%, $(LOGINUTILS-y))
index 2144c95ffd29cd2603d1ba4b2977cc6a98abc562..0176d621cfdb2db3b50e3af87eac35335ed5dcc5 100644 (file)
@@ -232,7 +232,7 @@ static void termio_final(struct options *op, struct termio *tp,
                                  struct chardata *cp);
 static int caps_lock(const char *s);
 static int bcode(const char *s);
-static void error(const char *fmt, ...);
+static void error(const char *fmt, ...) __attribute__ ((noreturn));
 
 /* The following is used for understandable diagnostics. */
 
@@ -299,8 +299,7 @@ int getty_main(int argc, char **argv)
                int iv;
 
                iv = getpid();
-               if (ioctl(0, TIOCSPGRP, &iv) < 0)
-                       perror_msg("ioctl() TIOCSPGRP call failed");
+               ioctl(0, TIOCSPGRP, &iv);
        }
 #endif
        /* Initialize the termio settings (raw mode, eight-bit, blocking i/o). */
@@ -368,7 +367,6 @@ int getty_main(int argc, char **argv)
 
        (void) execl(options.login, options.login, "--", logname, (char *) 0);
        error("%s: can't exec %s: %m", options.tty, options.login);
-       return (0);                                     /* quiet GCC */
 }
 
 /* parse-args - parse command-line arguments */
@@ -382,10 +380,9 @@ static void parse_args(int argc, char **argv, struct options *op)
        while (isascii(c = getopt(argc, argv, "I:LH:f:hil:mt:wn"))) {
                switch (c) {
                case 'I':
-                       if (!(op->initstring = strdup(optarg))) {
+                       if (!(op->initstring = strdup(optarg)))
                                error("can't malloc initstring");
-                               break;
-                       }
+                               
                        {
                                char ch, *p, *q;
                                int i;
index 079791c4cf1b5519e23d6593b4c17a6a81e77716..c8756211a7fac6467aa4a9a86809afdb4cef61a9 100644 (file)
@@ -173,7 +173,7 @@ extern int passwd_main(int argc, char **argv)
        ruid = getuid();
        pw = (struct passwd *) getpwuid(ruid);
        if (!pw) {
-               error_msg_and_die("Cannot determine your user name.\n");
+               error_msg_and_die("Cannot determine your user name.");
        }
        myname = (char *) xstrdup(pw->pw_name);
        if (optind < argc) {
index 5a40e4199042155d0013b8811096762ff5f5518f..4f2e9a779484c34f9c34758b9b499ed57a97fbef 100644 (file)
 #ifndef MODUTILS_MODULE_H
 static const int MODUTILS_MODULE_H = 1;
 
-#ident "$Id: insmod.c,v 1.87 2002/07/02 19:14:23 andersen Exp $"
+#ident "$Id: insmod.c,v 1.88 2002/07/19 00:05:48 sandman Exp $"
 
 /* This file contains the structures used by the 2.0 and 2.1 kernels.
    We do not use the kernel headers directly because we do not wish
@@ -454,7 +454,7 @@ int delete_module(const char *);
 #ifndef MODUTILS_OBJ_H
 static const int MODUTILS_OBJ_H = 1;
 
-#ident "$Id: insmod.c,v 1.87 2002/07/02 19:14:23 andersen Exp $"
+#ident "$Id: insmod.c,v 1.88 2002/07/19 00:05:48 sandman Exp $"
 
 /* The relocatable object is manipulated using elfin types.  */
 
@@ -740,7 +740,7 @@ static int n_ext_modules_used;
 extern int delete_module(const char *);
 
 static char *m_filename;
-static char m_fullName[FILENAME_MAX];
+static char *m_fullName;
 
 
 
@@ -3503,10 +3503,8 @@ extern int insmod_main( int argc, char **argv)
                tmp[len] = '\0';
        }
 
-       if (len > (sizeof(m_fullName)-3))
-               error_msg_and_die("%s: module name too long", tmp);
+       bb_asprintf(&m_fullName, "%s.o", tmp, ".o");
 
-       strcat(strcpy(m_fullName, tmp), ".o");
        if (!m_name) {
                m_name = tmp;
        } else {
@@ -3522,25 +3520,32 @@ extern int insmod_main( int argc, char **argv)
                /* Hmm.  Could not open it.  First search under /lib/modules/`uname -r`,
                 * but do not error out yet if we fail to find it... */
                if (uname(&myuname) == 0) {
-                       char module_dir[FILENAME_MAX];
+                       char *module_dir;
+                       char *tmdn;
                        char real_module_dir[FILENAME_MAX];
-                       snprintf (module_dir, sizeof(module_dir), "%s/%s", 
-                                       _PATH_MODULES, myuname.release);
+
+                       tmdn = concat_path_file(_PATH_MODULES, myuname.release);
                        /* Jump through hoops in case /lib/modules/`uname -r`
                         * is a symlink.  We do not want recursive_action to
                         * follow symlinks, but we do want to follow the
                         * /lib/modules/`uname -r` dir, So resolve it ourselves
                         * if it is a link... */
-                       if (realpath (module_dir, real_module_dir) == NULL)
-                               strcpy(real_module_dir, module_dir);
-                       recursive_action(real_module_dir, TRUE, FALSE, FALSE,
+                       if (realpath (tmdn, real_module_dir) == NULL)
+                               module_dir = tmdn;
+                       else
+                               module_dir = real_module_dir;
+                       recursive_action(module_dir, TRUE, FALSE, FALSE,
                                        check_module_name_match, 0, m_fullName);
+                       free(tmdn);
                }
 
                /* Check if we have found anything yet */
                if (m_filename == 0 || ((fp = fopen(m_filename, "r")) == NULL))
                {
                        char module_dir[FILENAME_MAX];
+
+                       free(m_filename);
+                       m_filename = 0;
                        if (realpath (_PATH_MODULES, module_dir) == NULL)
                                strcpy(module_dir, _PATH_MODULES);
                        /* No module found under /lib/modules/`uname -r`, this
index c5f2e9da2e7e66d322cfad57169807eccd0c8db0..5cbc8113159f3fbc7b1490134ac05c2c4418f596 100644 (file)
@@ -611,8 +611,7 @@ static char **username_tab_completion(char *ud, int *num_matches)
                        /* Null usernames should result in all users as possible completions. */
                        if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
 
-                               temp = xmalloc(3 + strlen(entry->pw_name));
-                               sprintf(temp, "~%s/", entry->pw_name);
+                               bb_asprintf(&temp, "~%s/", entry->pw_name);
                                matches = xrealloc(matches, (nm + 1) * sizeof(char *));
 
                                matches[nm++] = temp;
index 95ecba6e6645dbbfd69ac9eb3fd3af57e88770e2..2390f3056d4783a4ba7759eef0172db0fd926726 100644 (file)
@@ -96,9 +96,7 @@ const char *normalize(const char *arg)
                 free(BUFFER);
 
         if (!quote) { /* Just copy arg */
-                BUFFER=xmalloc(strlen(arg)+1);
-
-                strcpy(BUFFER,arg);
+               BUFFER=xstrdup(arg);
                 return BUFFER;
         }
 
@@ -204,7 +202,6 @@ static const int LONG_OPTIONS_INCR = 10;
 /* Register a long option. The contents of name is copied. */
 void add_longopt(const char *name,int has_arg)
 {
-        char *tmp;
         if (!name) { /* init */
                 free(long_options);
                 long_options=NULL;
@@ -228,9 +225,7 @@ void add_longopt(const char *name,int has_arg)
                 long_options[long_options_nr-1].has_arg=has_arg;
                 long_options[long_options_nr-1].flag=NULL;
                 long_options[long_options_nr-1].val=LONG_OPT;
-                tmp = xmalloc(strlen(name)+1);
-                strcpy(tmp,name);
-                long_options[long_options_nr-1].name=tmp;
+               long_options[long_options_nr-1].name=xstrdup(name);
         }
         long_options_nr++;
 }
@@ -326,7 +321,7 @@ int getopt_main(int argc, char *argv[])
                         /* For some reason, the original getopt gave no error
                            when there were no arguments. */
                         printf(" --\n");
-                        exit(0);
+                       return 0;
                 } else
                         error_msg_and_die("missing optstring argument");
         }
@@ -336,7 +331,7 @@ int getopt_main(int argc, char *argv[])
                 optstr=xmalloc(strlen(argv[1])+1);
                 strcpy(optstr,argv[1]+strspn(argv[1],"-+"));
                 argv[1]=argv[0];
-                exit(generate_output(argv+1,argc-1,optstr,long_options));
+               return (generate_output(argv+1,argc-1,optstr,long_options));
         }
 
         while ((opt=getopt_long(argc,argv,shortopts,longopts,NULL)) != EOF)
@@ -347,8 +342,7 @@ int getopt_main(int argc, char *argv[])
                 case 'o':
                         if (optstr)
                                 free(optstr);
-                        optstr=xmalloc(strlen(optarg)+1);
-                        strcpy(optstr,optarg);
+                       optstr=xstrdup(optarg);
                         break;
                 case 'l':
                         add_long_options(optarg);
@@ -356,8 +350,7 @@ int getopt_main(int argc, char *argv[])
                 case 'n':
                         if (name)
                                 free(name);
-                        name=xmalloc(strlen(optarg)+1);
-                        strcpy(name,optarg);
+                       name=xstrdup(optarg);
                         break;
                 case 'q':
                         quiet_errors=1;
@@ -369,7 +362,7 @@ int getopt_main(int argc, char *argv[])
                         set_shell(optarg);
                         break;
                 case 'T':
-                        exit(4);
+                       return 4;
                 case 'u':
                         quote=0;
                         break;
@@ -381,8 +374,7 @@ int getopt_main(int argc, char *argv[])
                 if (optind >= argc)
                         error_msg_and_die("missing optstring argument");
                 else {
-                        optstr=xmalloc(strlen(argv[optind])+1);
-                        strcpy(optstr,argv[optind]);
+                       optstr=xstrdup(argv[optind]);
                         optind++;
                 }
         }
@@ -390,7 +382,7 @@ int getopt_main(int argc, char *argv[])
                 argv[optind-1]=name;
         else
                 argv[optind-1]=argv[0];
-        exit(generate_output(argv+optind-1,argc-optind+1,optstr,long_options));
+       return (generate_output(argv+optind-1,argc-optind+1,optstr,long_options));
 }
 
 /*