Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 12 May 2016 23:14:31 +0000 (UTC)
From:      Don Lewis <truckman@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r299577 - head/usr.bin/catman
Message-ID:  <201605122314.u4CNEVn7095408@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: truckman
Date: Thu May 12 23:14:31 2016
New Revision: 299577
URL: https://svnweb.freebsd.org/changeset/base/299577

Log:
  Avoid Coverity NUL termination warning about strncpy() by using
  memcpy() instead.  It's probably a bit more optimal in this case
  anyway. [1]
  
  The program logic leading up to the creation of the strncpy/memcpy
  destination buffer is a bit hairy.  Add a call to assert() to make
  it clear what is happening here and detect any potential buffer
  overruns in the future.
  
  Check a couple syscall error returns.  Ignore the EEXIST error from
  link() to preserve existing behavior. [2] [3]
  
  Reported by:	Coverity
  CID:		1009659 [1], 1009349 [2], 1009350 [3]

Modified:
  head/usr.bin/catman/catman.c

Modified: head/usr.bin/catman/catman.c
==============================================================================
--- head/usr.bin/catman/catman.c	Thu May 12 22:51:04 2016	(r299576)
+++ head/usr.bin/catman/catman.c	Thu May 12 23:14:31 2016	(r299577)
@@ -34,9 +34,11 @@ __FBSDID("$FreeBSD$");
 #include <sys/param.h>
 #include <sys/utsname.h>
 
+#include <assert.h>
 #include <ctype.h>
 #include <dirent.h>
 #include <err.h>
+#include <errno.h>
 #include <fcntl.h>
 #include <locale.h>
 #include <langinfo.h>
@@ -267,7 +269,8 @@ get_cat_section(char *section)
 	char *cat_section;
 
 	cat_section = strdup(section);
-	strncpy(cat_section, "cat", 3);
+	assert(strlen(section) > 3 && strncmp(section, "man", 3) == 0);
+	memcpy(cat_section, "cat", 3);
 	return cat_section;
 }
 
@@ -420,7 +423,8 @@ process_page(char *mandir, char *src, ch
 			    verbose ? "\t" : "", cat, link_name);
 		}
 		if (!pretend)
-			link(link_name, cat);
+			if (link(link_name, cat) < 0 && errno != EEXIST)
+				warn("%s %s: link", link_name, cat);
 		return;
 	}
 	insert_hashtable(links, src_ino, src_dev, strdup(cat));
@@ -608,7 +612,8 @@ select_sections(const struct dirent *ent
 static void
 process_mandir(char *dir_name, char *section)
 {
-	fchdir(starting_dir);
+	if (fchdir(starting_dir) < 0)
+		warn("fchdir");
 	if (already_visited(NULL, dir_name, section == NULL))
 		return;
 	check_writable(dir_name);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201605122314.u4CNEVn7095408>