From: Pierre Habouzit Date: Mon, 30 Oct 2006 11:50:19 +0000 (+0100) Subject: less horrible strncpy's X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=commitdiff_plain;h=c6b9d35ed9361e4defab9762a7480d5126405ae9 less horrible strncpy's fix Makefile.am Signed-off-by: Pierre Habouzit --- diff --git a/Makefile.am b/Makefile.am index d07b188..9315b21 100644 --- a/Makefile.am +++ b/Makefile.am @@ -48,19 +48,13 @@ muttng_LDADD = @MUTT_LIB_OBJECTS@ @LIBOBJS@ $(LIBIMAP) $(LIBPOP) $(LIBNNTP) \ muttng_DEPENDENCIES = @MUTT_LIB_OBJECTS@ @LIBOBJS@ \ $(top_builddir)/lib-lib/liblib.a \ + $(top_builddir)/lib-hash/libhash.a \ $(top_builddir)/lib/libsane.a \ $(LIBIMAPDEPS) $(LIBPOPDEPS) $(LIBNNTPDEPS) $(INTLDEPS) makedoc_SOURCES = makedoc.c -makedoc_LDADD = -makedoc_DEPENDENCIES = - -# $(makedoc_OBJECTS): $(makedoc_SOURCES) -# $(HOST_CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) -c $< - -# makedoc: $(makedoc_OBJECTS) $(makedoc_DEPENDENCIES) -# @rm -rf makedoc -# $(HOST_CC) $(AM_CFLAGS) $(LDFLAGS) $(makedoc_LDFLAGS) $(makedoc_OBJECTS) $(makedoc_LDADD) -o makedoc +makedoc_LDADD = -Llib-lib -llib +makedoc_DEPENDENCIES = $(top_builddir)/lib-lib/liblib.a CPP=@CPP@ @@ -102,12 +96,15 @@ EXTRA_DIST = config.rpath COPYRIGHT GPL OPS OPS.PGP OPS.CRYPT OPS.SMIME TODO \ regex.c mutt_libesmtp.h crypt-gpgme.h muttng_dotlock_SOURCES = dotlock.c -muttng_dotlock_LDADD = @LIBOBJS@ -muttng_dotlock_DEPENDENCIES = @LIBOBJS@ +muttng_dotlock_LDADD = -Llib-lib -llib @LIBOBJS@ +muttng_dotlock_DEPENDENCIES = @LIBOBJS@ $(top_builddir)/lib-lib/liblib.a pgpringng_SOURCES = pgppubring.c pgplib.c lib.c extlib.c pgppacket.c -pgpringng_LDADD = @LIBOBJS@ $(INTLLIBS) -Llib-lib -llib -Llib-hash -lhash -Llib -lsane -pgpringng_DEPENDENCIES = @LIBOBJS@ $(INTLDEPS) +pgpringng_LDADD = @LIBOBJS@ $(INTLLIBS) \ + -Llib-lib -llib -Llib-hash -lhash -Llib -lsane +pgpringng_DEPENDENCIES = @LIBOBJS@ $(INTLDEPS) \ + $(top_builddir)/lib-lib/liblib.a \ + $(top_builddir)/lib-hash/libhash.a pgpewrapng_SOURCES = pgpewrap.c pgpewrapng_LDADD = diff --git a/attach.c b/attach.c index 8b9cb82..8eeb71b 100644 --- a/attach.c +++ b/attach.c @@ -353,7 +353,7 @@ int mutt_is_autoview (BODY * b, const char *type) if (!type) snprintf (_type, sizeof (_type), "%s/%s", TYPE (b), b->subtype); else - strncpy (_type, type, sizeof (_type)); + m_strcpy(_type, sizeof(_type), type); mutt_check_lookup_list (b, _type, sizeof (_type)); type = _type; diff --git a/browser.c b/browser.c index 3fd3563..53bab1a 100644 --- a/browser.c +++ b/browser.c @@ -350,7 +350,7 @@ static const char *newsgroup_format_str (char *dest, size_t destlen, char op, break; case 'f': - strncpy (fn, folder->ff->name, sizeof (fn) - 1); + m_strcpy(fn, sizeof(fn), folder->ff->name); snprintf (tmp, sizeof (tmp), "%%%ss", fmt); snprintf (dest, destlen, tmp, fn); break; @@ -779,10 +779,8 @@ void _mutt_select_file (char *f, size_t flen, int flags, char ***files, for (i = m_strlen(f) - 1; i > 0 && f[i] != '/'; i--); if (i > 0) { if (f[0] == '/') { - if (i > sizeof (LastDir) - 1) - i = sizeof (LastDir) - 1; - strncpy (LastDir, f, i); - LastDir[i] = 0; + i = MIN(sizeof(LastDir) - 1, i); + m_strcpy(LastDir, sizeof(LastDir), f); } else { getcwd (LastDir, sizeof (LastDir)); diff --git a/complete.c b/complete.c index 649fa0a..55df906 100644 --- a/complete.c +++ b/complete.c @@ -147,7 +147,7 @@ int mutt_complete (char *s, size_t slen) else { *p = 0; len = (size_t) (p - s); - strncpy (dirpart, s, len); + memcpy(dirpart, s, len); dirpart[len] = 0; p++; strfcpy (filepart, p, sizeof (filepart)); diff --git a/compose.c b/compose.c index d2942ea..a0eb9ac 100644 --- a/compose.c +++ b/compose.c @@ -480,7 +480,7 @@ static const char *compose_format_str (char *buf, size_t buflen, char op, break; case 'v': - strncpy (buf, mutt_make_version (0), buflen); + m_strcpy(buf, buflen, mutt_make_version (0)); break; case 0: diff --git a/crypt-gpgme.c b/crypt-gpgme.c index 9728bcc..d01792b 100644 --- a/crypt-gpgme.c +++ b/crypt-gpgme.c @@ -687,8 +687,7 @@ static int get_micalg (gpgme_ctx_t ctx, char *buf, size_t buflen) if (result) { algorithm_name = gpgme_hash_algo_name (result->signatures->hash_algo); if (algorithm_name) { - strncpy (buf, algorithm_name, buflen - 1); - buf[buflen - 1] = 0; + m_strcpy(buf, buflen, algorithm_name); } } diff --git a/curs_main.c b/curs_main.c index 03315f2..81d59c2 100644 --- a/curs_main.c +++ b/curs_main.c @@ -1202,13 +1202,13 @@ int mutt_index_menu (void) #endif { if (Context && Context->path) - strncpy (buf, Context->path, sizeof (buf)); + m_strcpy(buf, sizeof(buf), Context->path); if (op != OP_SIDEBAR_OPEN) buffy_next (buf, sizeof (buf)); } if (op == OP_SIDEBAR_OPEN) { - strncpy (buf, NONULL(sidebar_get_current ()), sizeof (buf)); + m_strcpy(buf, sizeof(buf), sidebar_get_current()); } else if (mutt_enter_fname (cp, buf, sizeof (buf), &menu->redraw, 1) == -1) { if (menu->menu == MENU_PAGER) { diff --git a/dotlock.c b/dotlock.c index 43d3058..9df9848 100644 --- a/dotlock.c +++ b/dotlock.c @@ -55,7 +55,7 @@ # define LONG_STRING 1024 # define MAXLOCKATTEMPT 5 -# define strfcpy(A,B,C) strncpy (A,B,C), *(A+(C)-1)=0 +# define strfcpy(A,B,C) m_strcpy(A,C,B) # ifdef USE_SETGID diff --git a/edit.c b/edit.c index 07bc98d..02d1858 100644 --- a/edit.c +++ b/edit.c @@ -375,7 +375,7 @@ int mutt_builtin_editor (const char *path, HEADER * msg, HEADER * cur) break; case 'r': if (*p) { - strncpy (tmp, p, sizeof (tmp)); + m_strcpy(tmp, sizeof(tmp), p); mutt_expand_path (tmp, sizeof (tmp)); buf = be_snarf_file (tmp, buf, &bufmax, &buflen, 1); } diff --git a/gnupgparse.c b/gnupgparse.c index ef08bcd..1f84aaf 100644 --- a/gnupgparse.c +++ b/gnupgparse.c @@ -221,7 +221,7 @@ static pgp_key_t parse_pub_line (char *buf, int *is_subkey, pgp_key_t k) time.tm_sec = 0; time.tm_min = 0; time.tm_hour = 12; - strncpy (tstr, p, 11); + m_strcpy(tstr, sizeof(tstr), p); tstr[4] = '\0'; time.tm_year = atoi (tstr) - 1900; tstr[7] = '\0'; diff --git a/hcache.c b/hcache.c index 6cf01e6..e513c76 100644 --- a/hcache.c +++ b/hcache.c @@ -663,7 +663,7 @@ mutt_hcache_fetch(void *db, const char *filename, if (!h) return NULL; - strncpy(path, h->folder, sizeof (path)); + m_strcpy(path, sizeof(path), h->folder); str_cat(path, sizeof (path), filename); ksize = strlen(h->folder) + keylen(path + strlen(h->folder)); @@ -693,7 +693,7 @@ mutt_hcache_store(void *db, const char *filename, HEADER * header, if (!h) return -1; - strncpy(path, h->folder, sizeof (path)); + m_strcpy(path, sizeof(path), h->folder); str_cat(path, sizeof (path), filename); ksize = strlen(h->folder) + keylen(path + strlen(h->folder)); @@ -718,7 +718,7 @@ mutt_hcache_delete(void *db, const char *filename, if (!h) return -1; - strncpy(path, h->folder, sizeof (path)); + m_strcpy(path, sizeof(path), h->folder); str_cat(path, sizeof (path), filename); ksize = strlen(h->folder) + keylen(path + strlen(h->folder)); @@ -788,7 +788,7 @@ void *mutt_hcache_fetch (void *db, const char *filename, return NULL; } - strncpy (path, h->folder, sizeof (path)); + m_strcpy(path, sizeof(path), h->folder); strncat (path, filename, sizeof (path) - m_strlen(path)); key.dptr = path; @@ -818,7 +818,7 @@ mutt_hcache_store (void *db, const char *filename, HEADER * header, return -1; } - strncpy (path, h->folder, sizeof (path)); + m_strcpy(path, sizeof(path), h->folder); strncat (path, filename, sizeof (path) - m_strlen(path)); key.dptr = path; @@ -845,7 +845,7 @@ mutt_hcache_delete (void *db, const char *filename, return -1; } - strncpy (path, h->folder, sizeof (path)); + m_strcpy(path, sizeof(path), h->folder); strncat (path, filename, sizeof (path) - m_strlen(path)); key.dptr = path; diff --git a/imap/browse.c b/imap/browse.c index d9bdf83..54d608b 100644 --- a/imap/browse.c +++ b/imap/browse.c @@ -101,8 +101,7 @@ int imap_browse (char *path, struct browser_state *state) imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox)); imap_munge_mbox_name (buf, sizeof (buf), mbox); imap_unquote_string (buf); /* As kludgy as it gets */ - mbox[sizeof (mbox) - 1] = '\0'; - strncpy (mbox, buf, sizeof (mbox) - 1); + m_strcpy(mbox, sizeof(mbox), buf); n = m_strlen(mbox); debug_print (3, ("mbox: %s\n", mbox)); diff --git a/imap/util.c b/imap/util.c index 63dd769..8e6832b 100644 --- a/imap/util.c +++ b/imap/util.c @@ -443,7 +443,7 @@ void imap_unmunge_mbox_name (char *s) buf = m_strdup(s); if (buf) { imap_utf7_decode (&buf); - strncpy (s, buf, m_strlen(s)); + m_strcpy(s, m_strlen(s) + 1, buf); } p_delete(&buf); diff --git a/init.c b/init.c index a456c16..6ca24b3 100644 --- a/init.c +++ b/init.c @@ -2282,7 +2282,7 @@ int mutt_command_complete (char *buffer, size_t len, int pos, int numtabs) Matches[(numtabs - 2) % Num_matched]); /* return the completed command */ - strncpy (buffer, Completed, len - spaces); + m_strcpy(buffer, len - spaces, Completed); } else if (!str_ncmp (buffer, "set", 3) || !str_ncmp (buffer, "unset", 5) @@ -2330,7 +2330,7 @@ int mutt_command_complete (char *buffer, size_t len, int pos, int numtabs) snprintf (Completed, sizeof(Completed), "%s", Matches[(numtabs - 2) % Num_matched]); - strncpy (pt, Completed, buffer + len - pt - spaces); + m_strcpy(pt, buffer + len - pt - spaces, Completed); } else if (!str_ncmp (buffer, "exec", 4)) { struct binding_t *menu = km_get_table (CurrentMenu); @@ -2374,7 +2374,7 @@ int mutt_command_complete (char *buffer, size_t len, int pos, int numtabs) snprintf (Completed, sizeof(Completed), "%s", Matches[(numtabs - 2) % Num_matched]); - strncpy (pt, Completed, buffer + len - pt - spaces); + m_strcpy(pt, buffer + len - pt - spaces, Completed); } else return 0; diff --git a/lib/str.h b/lib/str.h index b2457d4..bc3cb82 100644 --- a/lib/str.h +++ b/lib/str.h @@ -30,7 +30,7 @@ # define ISSPACE(c) isspace((unsigned char)c) # define ISBLANK(c) (c == ' ' || c == '\t') -# define strfcpy(A,B,C) strncpy(A,B,C), *(A+(C)-1)=0 +# define strfcpy(A,B,C) m_strcpy(A,C,B) /* this macro must check for *c == 0 since isspace(0) has * unreliable behavior on some systems */ # define SKIPWS(c) while (*(c) && isspace ((unsigned char) *(c))) c++; diff --git a/makedoc.c b/makedoc.c index 3148ca4..6910e54 100644 --- a/makedoc.c +++ b/makedoc.c @@ -31,6 +31,7 @@ #include #include +#include #ifdef HAVE_UNISTD_H # include @@ -519,7 +520,7 @@ static void handle_confline (char *s) if (!strcmp (buff, "}")) break; - strncpy (val + STRLEN (val), buff, sizeof (val) - STRLEN (val)); + m_strcat(val, sizeof(val), buff); } while ((s = get_token (buff, sizeof (buff), s))); diff --git a/menu.c b/menu.c index 1c4e7b9..42f411c 100644 --- a/menu.c +++ b/menu.c @@ -140,7 +140,7 @@ static void print_enriched_string (int attr, unsigned char *s, int do_color) static void menu_make_entry (char *s, int l, MUTTMENU * menu, int i) { if (menu->dialog) { - strncpy (s, menu->dialog[i], l); + m_strcpy(s, l, menu->dialog[i]); menu->current = -1; /* hide menubar */ } else diff --git a/nntp/newsrc.c b/nntp/newsrc.c index 1f9bd5b..ff9df9e 100644 --- a/nntp/newsrc.c +++ b/nntp/newsrc.c @@ -265,7 +265,7 @@ const char *nntp_format_str (char *dest, size_t destlen, char op, switch (op) { case 's': - strncpy (fn, NewsServer, sizeof (fn) - 1); + m_strcpy(fn, sizeof (fn), NewsServer); str_tolower (fn); snprintf (tmp, sizeof (tmp), "%%%ss", fmt); snprintf (dest, destlen, tmp, fn); @@ -656,8 +656,8 @@ static int mutt_update_list_file (char *filename, char *section, * via rename(2); as dirname(2) may modify its argument, * temporarily use buf as copy of it */ - strncpy (buf, filename, sizeof (buf)); - strncpy (tmpfile, basename (filename), sizeof (tmpfile)); + m_strcpy(buf, sizeof(buf), filename); + m_strcpy(tmpfile, sizeof(tmpfile), basename(filename)); mutt_adv_mktemp ((const char*) dirname (buf), tmpfile, sizeof (tmpfile)); debug_print (1, ("Opening %s\n", tmpfile)); if (!(ofp = fopen (tmpfile, "w"))) { @@ -1114,7 +1114,7 @@ void nntp_buffy (char* dst, size_t dstlen) { if (!unread) continue; } - strncpy (dst, data->group, dstlen); + m_strcpy(dst, dstlen, data->group); break; } } diff --git a/send.c b/send.c index bc84b6b..01fde6c 100644 --- a/send.c +++ b/send.c @@ -267,7 +267,7 @@ static int edit_envelope (ENVELOPE * en, int flags) if (ascii_strncasecmp ("subject:", uh->data, 8) == 0) { p = uh->data + 8; SKIPWS (p); - strncpy (buf, p, sizeof (buf)); + m_strcpy(buf, sizeof(buf), p); } } } diff --git a/sidebar.c b/sidebar.c index 905a6da..cd3db75 100644 --- a/sidebar.c +++ b/sidebar.c @@ -230,9 +230,9 @@ int make_sidebar_entry (char* box, int idx, size_t len) shortened = 1; } - snprintf (entry, len-lencnt, "%s", box); + m_strcpy(entry, len - lencnt, box); entry[m_strlen(entry)] = ' '; - strncpy (entry + (len - lencnt), no, lencnt); + m_strcpy(entry + (len - lencnt), lencnt, no); addnstr (entry, len); diff --git a/smime.c b/smime.c index 0498122..608d53b 100644 --- a/smime.c +++ b/smime.c @@ -410,8 +410,8 @@ char *smime_ask_for_key (char *prompt, char *mailbox, short public) Table[cur].hash = hash; Table[cur].suffix = hash_suffix; - strncpy (Table[cur].email, fields[0], sizeof (Table[cur].email)); - strncpy (Table[cur].nick, fields[2], sizeof (Table[cur].nick)); + m_strcpy(Table[cur].email, sizeof(Table[cur].email), fields[0]); + m_strcpy(Table[cur].nick, sizeof(Table[cur].nick), fields[2]); Table[cur].trust = *fields[4]; Table[cur].public = public; diff --git a/sort.c b/sort.c index b932ab1..af00646 100644 --- a/sort.c +++ b/sort.c @@ -118,11 +118,8 @@ int compare_to (const void *a, const void *b) * On the next call that pointer may get smashed so we copy the return value * to our own memory space. */ - strncpy (fa, mutt_get_name ((*ppa)->env->to), sizeof (fa)); - fa[sizeof (fa) - 1] = '\0'; - - strncpy (fb, mutt_get_name ((*ppb)->env->to), sizeof (fb)); - fb[sizeof (fb) - 1] = '\0'; + m_strcpy(fa, sizeof(fa), mutt_get_name((*ppa)->env->to)); + m_strcpy(fb, sizeof(fb), mutt_get_name((*ppb)->env->to)); result = str_casecmp (fa, fb); AUXSORT (result, a, b); @@ -141,11 +138,8 @@ int compare_from (const void *a, const void *b) * On the next call that pointer may get smashed so we copy the return value * to our own memory space. */ - strncpy (fa, mutt_get_name ((*ppa)->env->from), sizeof (fa)); - fa[sizeof (fa) - 1] = '\0'; - - strncpy (fb, mutt_get_name ((*ppb)->env->from), sizeof (fb)); - fb[sizeof (fb) - 1] = '\0'; + m_strcpy(fa, sizeof(fa), mutt_get_name((*ppa)->env->from)); + m_strcpy(fb, sizeof(fb), mutt_get_name((*ppb)->env->from)); result = str_casecmp (fa, fb); AUXSORT (result, a, b); diff --git a/status.c b/status.c index 95ad3ff..bab4295 100644 --- a/status.c +++ b/status.c @@ -283,7 +283,7 @@ static const char *status_format_str (char *buf, size_t buflen, char op, break; case 'v': - strncpy (buf, mutt_make_version (0), buflen); + m_strcpy(buf, buflen, mutt_make_version(0)); break; case 'V': diff --git a/thread.c b/thread.c index ae5ebbe..dc786bb 100644 --- a/thread.c +++ b/thread.c @@ -243,7 +243,7 @@ void mutt_draw_tree (CONTEXT * ctx) myarrow[width + 1] = 0; new_tree = p_new(char, (2 + depth * width)); if (start_depth > 1) { - strncpy (new_tree, pfx, (start_depth - 1) * width); + memcpy(new_tree, pfx, (start_depth - 1) * width); strfcpy (new_tree + (start_depth - 1) * width, arrow, (1 + depth - start_depth) * width + 2); }