Re: cu coredumps

看板DFBSD_bugs作者時間21年前 (2004/10/28 04:32), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串16/19 (看更多)
This is a multi-part message in MIME format. --------------070900060407070302010805 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Matthew Dillon wrote: > (1) No you cannot safely use data that has been free()'d. Not ever. The > original free() 15+ years ago had the side effect that the last > free()'d data could still be used until the next free(), but nobody > in their right mind programs to that spec any more. > > (2) No, you cannot assume that freed data will always cause a seg fault, > or contain good or bad data. > > (3) Stop worrying about performance for operations that are executed once > and will take far less then 1 microsecond, and stop worrying about > memory leaks for one-time string operations. Don't worry about > freeing the old data in this case. > > (4) Use asprintf(). Do not use malloc+str*() functions. Especially do > not use str*() functions for this sort of thing. Just use asprintf(). > > -Matt > Thank you for the asprintf() advice Matt. I am not sure if I used it right but it seems to work and it looks nicer. How's this one then? Better? I hope so :) -- Internet Explorer? Try FireFox at http://www.mozilla.org/products/firefox/ Outlook Express? Try ThunderBird at http://www.mozilla.org/products/thunderbird/ --------------070900060407070302010805 Content-Type: text/plain; name="hunt.c.diff" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="hunt.c.diff" --- hunt.c.old 2004-10-27 16:44:06.000000000 +0100 +++ hunt.c 2004-10-27 21:05:14.000000000 +0100 @@ -36,6 +36,7 @@ */ #include <sys/types.h> +#include <sys/stat.h> #include <err.h> #include <libutil.h> #include "tipconf.h" @@ -59,16 +60,37 @@ char *name; { register char *cp; + char *tmp_cp; sig_t f; int res; + struct stat sb; f = signal(SIGALRM, dead); while ((cp = getremote(name))) { deadfl = 0; - if ((uucplock = strrchr(cp, '/')) == NULL) - uucplock = cp; - else - ++uucplock; + + if (stat(cp, &sb) == 0) { + if ((uucplock = strrchr(cp, '/')) == NULL) { + uucplock = cp; + } else { + ++uucplock; + } + } else { + /* + * try to prefix with /dev/ if the cp doesn't + * have a / + */ + if ((uucplock = strrchr(cp, '/')) == NULL) { + if (asprintf(&tmp_cp, "%s%s", "/dev/", cp) > 0) { + cp = tmp_cp; + } else { + uucplock = cp; + } + } else { + uucplock = cp; + } + } + if ((res = uu_lock(uucplock)) != UU_LOCK_OK) { if (res != UU_LOCK_INUSE) fprintf(stderr, "uu_lock: %s\n", uu_lockerr(res)); --------------070900060407070302010805--
文章代碼(AID): #11W0N400 (DFBSD_bugs)
文章代碼(AID): #11W0N400 (DFBSD_bugs)