File name too long

jw schultz jw at pegasys.ws
Wed Mar 12 02:24:34 EST 2003


On Tue, Mar 11, 2003 at 03:45:05PM +0100, Paul Slootman wrote:
> On Tue 11 Mar 2003, jw schultz wrote:
> > 
> > Hmm.  I'm thinking we should just build fnametmp a piece at
> > a time.  I coded it up to see how it would look.  Not as
> > intuitive but there is a lot less strlen and no snprintf.
> > It also deals shortens the filename both for MAXPATHLEN and
> > for NAME_MAX so it is almost impossible for the function to
> > fail or produce a pathname that breaks things.
> ...
> > static int get_tmpname(char *fnametmp, char *fname)
> > {
> > 	char    *f;
> > 	int     length = 0;
> > 	int	maxname;
> > 
> > 
> > 	if (tmpdir)
> > 	{
> > 		strlcpy(fnametmp, tmpdir, MAXPATHLEN - 2);
> > 		length = strlen(fnametmp);
+		strcpy(fnametmp + length++, "/");
> > 	}
> > 
> > 	if (f = strrchr(fname, '/')) {
+ 		++f;
> > 		if (!tmpdir) {
> > 			length = f - fname;
> > 			strlcpy(fnametmp, fname, length);
> > 		}
- 		++f;
> > 	} else {
> > 		f = fname;
> > 	}
-	strcpy(fnametmp + length, ".");
-	length += 2;
+	strcpy(fnametmp + length++, ".");
> 
> This will cause the tmpfile to end up in the root directory, if there is
> no tmpdir and no directory in fname!  This is not good.
> How about:
> 
> 	if (length)
>             fnametmp[length++] = '/';
> 	fnametmp[length++] = '.';

Oops.  You are correct.  see adjustments above

> 
> > 
> > 	maxname = MIN(MAXPATHLEN - 8 - length, NAME_MAX - 9);
> 
> Shouldn't this be:
> 
> 	maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 7);
> 	maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 7);
> 
> because in both cases all we will be adding is ".XXXXXX" ?

Yes and No.  I was being overcautious regarding the null
terminator.  In the case of NAME_MAX the leading dot is
already there so we would be adding 8 chars to the basename.
so it should be:

	maxname = MIN(MAXPATHLEN - 7 - length, NAME_MAX - 8);





I threw this out there because it looked like we were
heading towards a rewrite of the whole function and i wanted
to rethink it instead of just reworking parts of it.  If a
concensus is that this is the way to go i'm all for that.
If the pointer arithmatic is too offensive that's fine too.

-- 
________________________________________________________________
	J.W. Schultz            Pegasystems Technologies
	email address:		jw at pegasys.ws

		Remember Cernan and Schmitt


More information about the rsync mailing list