2011-06-19 Paul Eggert <eggert@cs.ucla.edu>
+ * fileio.c: Fix some integer overflow issues.
+ (file_name_as_directory, Fexpand_file_name, Fsubstitute_in_file_name):
+ Don't assume string length fits in int.
+ (directory_file_name): Don't assume string length fits in long.
+ (make_temp_name): Don't assume pid fits in int, or that its print
+ length is less than 20.
+
* data.c (Fsubr_name): Rewrite to avoid a strlen call.
* coding.c (make_subsidiaries): Don't assume string length fits in int.
static char *
file_name_as_directory (char *out, const char *in)
{
- int size = strlen (in) - 1;
+ ptrdiff_t len = strlen (in);
- strcpy (out, in);
-
- if (size < 0)
+ if (len == 0)
{
out[0] = '.';
out[1] = '/';
return out;
}
+ strcpy (out, in);
+
/* For Unix syntax, Append a slash if necessary */
- if (!IS_DIRECTORY_SEP (out[size]))
+ if (!IS_DIRECTORY_SEP (out[len - 1]))
{
- out[size + 1] = DIRECTORY_SEP;
- out[size + 2] = '\0';
+ out[len] = DIRECTORY_SEP;
+ out[len + 1] = '\0';
}
#ifdef DOS_NT
dostounix_filename (out);
static int
directory_file_name (char *src, char *dst)
{
- long slen;
+ ptrdiff_t slen;
slen = strlen (src);
{
Lisp_Object val;
int len, clen;
- int pid;
+ intmax_t pid;
char *p, *data;
- char pidbuf[20];
+ char pidbuf[INT_BUFSIZE_BOUND (pid_t)];
int pidlen;
CHECK_STRING (prefix);
three are incremented if the file already exists. This ensures
262144 unique file names per PID per PREFIX. */
- pid = (int) getpid ();
+ pid = getpid ();
if (base64_p)
{
else
{
#ifdef HAVE_LONG_FILE_NAMES
- sprintf (pidbuf, "%d", pid);
- pidlen = strlen (pidbuf);
+ pidlen = sprintf (pidbuf, "%"PRIdMAX, pid);
#else
pidbuf[0] = make_temp_name_tbl[pid & 63], pid >>= 6;
pidbuf[1] = make_temp_name_tbl[pid & 63], pid >>= 6;
/* This should only point to alloca'd data. */
char *target;
- int tlen;
+ ptrdiff_t tlen;
struct passwd *pw;
#ifdef DOS_NT
int drive = 0;
int collapse_newdir = 1;
int is_escaped = 0;
#endif /* DOS_NT */
- int length;
+ ptrdiff_t length;
Lisp_Object handler, result;
int multibyte;
Lisp_Object hdir;
unsigned char *nm;
register unsigned char *newdir, *p, *o;
- int tlen;
+ ptrdiff_t tlen;
unsigned char *target;
struct passwd *pw;
int lose;
unsigned char *user = nm + 1;
/* Find end of name. */
unsigned char *ptr = (unsigned char *) strchr (user, '/');
- int len = ptr ? ptr - user : strlen (user);
+ ptrdiff_t len = ptr ? ptr - user : strlen (user);
/* Copy the user name into temp storage. */
o = (unsigned char *) alloca (len + 1);
memcpy (o, user, len);
else
{
Lisp_Object orig, decoded;
- int orig_length, decoded_length;
+ ptrdiff_t orig_length, decoded_length;
orig_length = strlen (o);
orig = make_unibyte_string (o, orig_length);
decoded = DECODE_FILE (orig);