From: schwarze@mdocml.bsd.lv
To: source@mdocml.bsd.lv
Subject: mdocml: Some simple set_basedir() cleanup; more to come.
Date: Wed, 18 Jun 2014 20:45:38 -0400 (EDT) [thread overview]
Message-ID: <201406190045.s5J0jcDe001035@krisdoz.my.domain> (raw)
Log Message:
-----------
Some simple set_basedir() cleanup; more to come.
1) Refrain from calling set_basedir() in the -t case,
and do not attempt to strip anything from the file names in that case.
Testing individual files cannot reasonably have any notion of a base dir.
2) Remove the possibility of passing NULL to set_basedir().
It was dangerous because it was not idempotent, and it served no purpose
except closing a file descriptor right before exit(), which is pointless.
Besides, the file descriptor is likely to be removed completely, soon.
3) Make sure that /foobar isn't treated as a subdirectory of /foo;
this fixes a bug reported by espie@.
Modified Files:
--------------
mdocml:
mandocdb.c
Revision Data
-------------
Index: mandocdb.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mandocdb.c,v
retrieving revision 1.149
retrieving revision 1.150
diff -Lmandocdb.c -Lmandocdb.c -u -p -r1.149 -r1.150
--- mandocdb.c
+++ mandocdb.c
@@ -433,10 +433,10 @@ main(int argc, char *argv[])
if (OP_UPDATE == op || OP_DELETE == op || OP_TEST == op) {
/*
- * All of these deal with a specific directory.
+ * Most of these deal with a specific directory.
* Jump into that directory first.
*/
- if (0 == set_basedir(path_arg))
+ if (OP_TEST != op && 0 == set_basedir(path_arg))
goto out;
if (dbopen(1)) {
@@ -525,7 +525,6 @@ main(int argc, char *argv[])
}
}
out:
- set_basedir(NULL);
manpath_free(&dirs);
mchars_free(mc);
mparse_free(mp);
@@ -800,10 +799,10 @@ filescan(const char *file)
return;
}
- if (strstr(buf, basedir) == buf)
- start = buf + strlen(basedir) + 1;
- else if (OP_TEST == op)
+ if (OP_TEST == op)
start = buf;
+ else if (strstr(buf, basedir) == buf)
+ start = buf + strlen(basedir);
else {
exitcode = (int)MANDOCLEVEL_BADARG;
say("", "%s: outside base directory", buf);
@@ -829,8 +828,9 @@ filescan(const char *file)
say(file, "Filename too long");
return;
}
- start = strstr(buf, basedir) == buf ?
- buf + strlen(basedir) + 1 : buf;
+ start = buf;
+ if (OP_TEST != op && strstr(buf, basedir) == buf)
+ start += strlen(basedir);
}
mlink = mandoc_calloc(1, sizeof(struct mlink));
@@ -2375,6 +2375,7 @@ set_basedir(const char *targetdir)
{
static char startdir[PATH_MAX];
static int fd;
+ char *cp;
/*
* Remember where we started by keeping a fd open to the origin
@@ -2385,8 +2386,7 @@ set_basedir(const char *targetdir)
if ('\0' == *startdir) {
if (NULL == getcwd(startdir, PATH_MAX)) {
exitcode = (int)MANDOCLEVEL_SYSERR;
- if (NULL != targetdir)
- say("", "&getcwd");
+ say("", "&getcwd");
return(0);
}
if (-1 == (fd = open(startdir, O_RDONLY, 0))) {
@@ -2394,8 +2394,6 @@ set_basedir(const char *targetdir)
say("", "&open %s", startdir);
return(0);
}
- if (NULL == targetdir)
- targetdir = startdir;
} else {
if (-1 == fd)
return(0);
@@ -2406,10 +2404,6 @@ set_basedir(const char *targetdir)
say("", "&chdir %s", startdir);
return(0);
}
- if (NULL == targetdir) {
- close(fd);
- return(1);
- }
}
if (NULL == realpath(targetdir, basedir)) {
basedir[0] = '\0';
@@ -2420,6 +2414,16 @@ set_basedir(const char *targetdir)
exitcode = (int)MANDOCLEVEL_BADARG;
say("", "&chdir");
return(0);
+ }
+ cp = strchr(basedir, '\0');
+ if ('/' != cp[-1]) {
+ if (cp - basedir >= PATH_MAX - 1) {
+ exitcode = (int)MANDOCLEVEL_SYSERR;
+ say("", "Filename too long");
+ return(0);
+ }
+ *cp++ = '/';
+ *cp = '\0';
}
return(1);
}
--
To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv
reply other threads:[~2014-06-19 0:45 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=201406190045.s5J0jcDe001035@krisdoz.my.domain \
--to=schwarze@mdocml.bsd.lv \
--cc=source@mdocml.bsd.lv \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).