Define and use xasprintf() so that memory allocate errors are reported.

This commit is contained in:
dsl 2003-07-26 16:06:44 +00:00
parent 8c66a01dc4
commit f2eecf77ab
12 changed files with 44 additions and 26 deletions

View File

@ -2615,7 +2615,7 @@ do_deferred_progs ()
}
for (p = checkin_progs; p != NULL; )
{
asprintf (&fname, "%s/%s", p->dir, CVSADM_CIPROG);
xasprintf (&fname, "%s/%s", p->dir, CVSADM_CIPROG);
f = open_file (fname, "w");
if (fprintf (f, "%s\n", p->name) < 0)
error (1, errno, "writing %s", fname);
@ -2631,7 +2631,7 @@ do_deferred_progs ()
checkin_progs = NULL;
for (p = update_progs; p != NULL; )
{
asprintf (&fname, "%s/%s", p->dir, CVSADM_UPROG);
xasprintf (&fname, "%s/%s", p->dir, CVSADM_UPROG);
f = open_file (fname, "w");
if (fprintf (f, "%s\n", p->name) < 0)
error (1, errno, "writing %s", fname);
@ -5371,7 +5371,7 @@ send_dirent_proc (callerdat, dir, repository, update_dir, entries)
* This case will happen when checking out a module defined as
* ``-a .''.
*/
asprintf (&cvsadm_name, "%s/%s", dir, CVSADM);
xasprintf (&cvsadm_name, "%s/%s", dir, CVSADM);
dir_exists = isdir (cvsadm_name);
free (cvsadm_name);

View File

@ -1797,7 +1797,7 @@ finaladd (finfo, rev, tag, options)
if (ret == 0)
{
char *tmp;
(void) asprintf (&tmp, "%s/%s%s", CVSADM, finfo->file, CVSEXT_LOG);
(void) xasprintf (&tmp, "%s/%s%s", CVSADM, finfo->file, CVSEXT_LOG);
if (unlink_file (tmp) < 0
&& !existence_error (errno))
error (0, errno, "cannot remove %s", tmp);
@ -2004,7 +2004,7 @@ checkaddfile (file, repository, tag, options, rcsnode)
desc = NULL;
descalloc = 0;
desclen = 0;
(void) asprintf (&fname, "%s/%s%s", CVSADM, file, CVSEXT_LOG);
(void) xasprintf (&fname, "%s/%s%s", CVSADM, file, CVSEXT_LOG);
/* If the file does not exist, no big deal. In particular, the
server does not (yet at least) create CVSEXT_LOG files. */
if (isfile (fname))
@ -2066,7 +2066,7 @@ checkaddfile (file, repository, tag, options, rcsnode)
FILE *fp;
/* move the new file out of the way. */
(void) asprintf (&fname, "%s/%s%s", CVSADM, CVSPREFIX, file);
(void) xasprintf (&fname, "%s/%s%s", CVSADM, CVSPREFIX, file);
rename_file (file, fname);
/* Create empty FILE. Can't use copy_file with a DEVNULL

View File

@ -471,6 +471,8 @@ void *xrealloc PROTO((void *ptr, size_t bytes));
void expand_string PROTO ((char **, size_t *, size_t));
void xrealloc_and_strcat PROTO ((char **, size_t *, const char *));
char *xstrdup PROTO((const char *str));
int xasprintf PROTO((char ** __restrict, const char * __restrict, ...))
__attribute__((__format__(__printf__, 2, 3)));
void strip_trailing_newlines PROTO((char *str));
int pathname_levels PROTO ((char *path));

View File

@ -650,7 +650,7 @@ diff_fileproc (callerdat, finfo)
if (tocvsPath)
{
/* Backup the current version of the file to CVS/,,filename */
asprintf(&fname,"%s/%s%s",CVSADM, CVSPREFIX, finfo->file);
xasprintf(&fname,"%s/%s%s",CVSADM, CVSPREFIX, finfo->file);
if (unlink_file_dir (fname) < 0)
if (! existence_error (errno))
error (1, errno, "cannot remove %s", fname);

View File

@ -334,7 +334,7 @@ edit_fileproc (callerdat, finfo)
trying to create the output file fails. But copy_file isn't
set up to facilitate that. */
mkdir_if_needed (CVSADM_BASE);
asprintf(&basefilename, "%s/%s", CVSADM_BASE, finfo->file);
xasprintf(&basefilename, "%s/%s", CVSADM_BASE, finfo->file);
copy_file (finfo->file, basefilename);
free (basefilename);
@ -463,7 +463,7 @@ unedit_fileproc (callerdat, finfo)
if (noexec)
return 0;
asprintf(&basefilename, "%s/%s", CVSADM_BASE, finfo->file);
xasprintf(&basefilename, "%s/%s", CVSADM_BASE, finfo->file);
if (!isfile (basefilename))
{
/* This file apparently was never cvs edit'd (e.g. we are uneditting

View File

@ -654,7 +654,7 @@ WriteTag (dir, tag, date, nonbranch, update_dir, repository)
if (dir == NULL)
tmp = xstrdup(CVSADM_TAG);
else
(void) asprintf (&tmp, "%s/%s", dir, CVSADM_TAG);
(void) xasprintf (&tmp, "%s/%s", dir, CVSADM_TAG);
if (tag || date)
{
@ -848,7 +848,7 @@ subdir_record (cmd, parent, dir)
if (parent == NULL)
entfilename = (char *)CVSADM_ENTLOG;
else
asprintf (&entfilename, "%s/%s", parent, CVSADM_ENTLOG);
xasprintf (&entfilename, "%s/%s", parent, CVSADM_ENTLOG);
entfile = CVS_FOPEN (entfilename, "a");
if (entfile == NULL)
@ -1017,8 +1017,8 @@ base_walk (code, finfo, rev)
doing it. */
if (finfo->update_dir[0] != '\0')
{
asprintf(&baserev_fullname, "%s/%s", finfo->update_dir, CVSADM_BASEREV);
asprintf(&baserevtmp_fullname, "%s/%s",
xasprintf(&baserev_fullname, "%s/%s", finfo->update_dir, CVSADM_BASEREV);
xasprintf(&baserevtmp_fullname, "%s/%s",
finfo->update_dir, CVSADM_BASEREVTMP);
} else {
baserev_fullname = xstrdup(CVSADM_BASEREV);

View File

@ -435,7 +435,7 @@ ignore_files (ilist, entries, update_dir, proc)
this directory if there is a CVS subdirectory.
This will normally be the case, but the user may
have messed up the working directory somehow. */
asprintf (&p, "%s/%s", file, CVSADM);
xasprintf (&p, "%s/%s", file, CVSADM);
dir = isdir (p);
free (p);
if (dir)
@ -468,7 +468,7 @@ ignore_files (ilist, entries, update_dir, proc)
{
char *temp;
(void) asprintf (&temp, "%s/%s", file, CVSADM);
(void) xasprintf (&temp, "%s/%s", file, CVSADM);
if (isdir (temp))
{
free (temp);

View File

@ -197,7 +197,7 @@ remove_fileproc (callerdat, finfo)
* remove the ,t file for it and scratch it from the
* entries file. */
Scratch_Entry (finfo->entries, finfo->file);
(void) asprintf (&fname, "%s/%s%s", CVSADM, finfo->file, CVSEXT_LOG);
(void) xasprintf (&fname, "%s/%s%s", CVSADM, finfo->file, CVSEXT_LOG);
if (unlink_file (fname) < 0
&& !existence_error (errno))
error (0, errno, "cannot remove %s", CVSEXT_LOG);

View File

@ -37,7 +37,7 @@ Name_Repository (dir, update_dir)
xupdate_dir = ".";
if (dir != NULL)
(void) asprintf (&tmp, "%s/%s", dir, CVSADM_REP);
(void) xasprintf (&tmp, "%s/%s", dir, CVSADM_REP);
else
tmp = xstrdup (CVSADM_REP);
@ -53,7 +53,7 @@ Name_Repository (dir, update_dir)
char *cvsadm;
if (dir != NULL)
(void) asprintf (&cvsadm, "%s/%s", dir, CVSADM);
(void) xasprintf (&cvsadm, "%s/%s", dir, CVSADM);
else
cvsadm = xstrdup (CVSADM);

View File

@ -42,8 +42,8 @@ Name_Root (dir, update_dir)
if (dir != NULL)
{
(void) asprintf (&cvsadm, "%s/%s", dir, CVSADM);
(void) asprintf (&tmp, "%s/%s", dir, CVSADM_ROOT);
(void) xasprintf (&cvsadm, "%s/%s", dir, CVSADM);
(void) xasprintf (&tmp, "%s/%s", dir, CVSADM_ROOT);
}
else
{
@ -150,7 +150,7 @@ Create_Root (dir, rootdir)
if (rootdir != NULL)
{
if (dir != NULL)
(void) asprintf (&tmp, "%s/%s", dir, CVSADM_ROOT);
(void) xasprintf (&tmp, "%s/%s", dir, CVSADM_ROOT);
else
tmp = xstrdup (CVSADM_ROOT);

View File

@ -10,6 +10,7 @@
#include "cvs.h"
#include "getline.h"
#include <stdarg.h>
#ifdef HAVE_NANOSLEEP
# include "xtime.h"
@ -75,6 +76,21 @@ xrealloc (ptr, bytes)
return (cp);
}
int
xasprintf(char **buf, const char *fmt, ...)
{
int len;
va_list ap;
va_start(ap, fmt);
len = vasprintf(buf, fmt, ap);
va_end(ap);
if (len == -1)
error(1, 0, "out of memory: xasprintf(..., \"%s\", ...) failed", fmt);
return len;
}
/* Two constants which tune expand_string. Having MIN_INCR as large
as 1024 might waste a bit of memory, but it shouldn't be too bad
(CVS used to allocate arrays of, say, 3000, PATH_MAX (8192, often),

View File

@ -1010,7 +1010,7 @@ update_dirent_proc (callerdat, dir, repository, update_dir, entries)
{
char *tmp;
(void) asprintf (&tmp, "%s/%s", dir, CVSADM_ENTSTAT);
(void) xasprintf (&tmp, "%s/%s", dir, CVSADM_ENTSTAT);
if (unlink_file (tmp) < 0 && ! existence_error (errno))
error (1, errno, "cannot remove file %s", tmp);
#ifdef SERVER_SUPPORT
@ -1304,7 +1304,7 @@ checkout_file (finfo, vers_ts, adding, merging, update_server)
#endif
)
{
(void) asprintf (&backup, "%s/%s%s", CVSADM, CVSPREFIX, finfo->file);
(void) xasprintf (&backup, "%s/%s%s", CVSADM, CVSPREFIX, finfo->file);
if (isfile (finfo->file))
rename_file (finfo->file, backup);
else
@ -1665,7 +1665,7 @@ patch_file (finfo, vers_ts, docheckout, file_info, checksum)
return 0;
}
(void) asprintf (&backup, "%s/%s%s", CVSADM, CVSPREFIX, finfo->file);
(void) xasprintf (&backup, "%s/%s%s", CVSADM, CVSPREFIX, finfo->file);
if (isfile (finfo->file))
rename_file (finfo->file, backup);
else
@ -1675,8 +1675,8 @@ patch_file (finfo, vers_ts, docheckout, file_info, checksum)
error (0, errno, "cannot remove %s", backup);
}
(void) asprintf (&file1, "%s/%s%s-1", CVSADM, CVSPREFIX, finfo->file);
(void) asprintf (&file2, "%s/%s%s-2", CVSADM, CVSPREFIX, finfo->file);
(void) xasprintf (&file1, "%s/%s%s-1", CVSADM, CVSPREFIX, finfo->file);
(void) xasprintf (&file2, "%s/%s%s-2", CVSADM, CVSPREFIX, finfo->file);
fail = 0;