git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Klaus Robert Suetterlin <robert@mpe.mpg.de>
To: Edgar Toernig <froese@gmx.de>
Cc: git@vger.kernel.org
Subject: Re: [PATCH]: first take at cleanup of #include, xmalloc / xrealloc, git status report usage.
Date: Fri, 29 Apr 2005 19:07:43 +0200	[thread overview]
Message-ID: <20050429170743.GD93323@xdt04.mpe-garching.mpg.de> (raw)
In-Reply-To: <20050429182407.5f6afd15.froese@gmx.de>

Thanks for reviewing this lengthy patch!

On Fri, Apr 29, 2005 at 06:24:07PM +0200, Edgar Toernig wrote:
> Robert S?tterlin wrote:
> >[...]
> > +static int
> > +create_directories(const char *path)
> >   {
> > -	int len = strlen(path);
> > -	char *buf = xmalloc(len + 1);
> > -	const char *slash = path;
> > +	char *buf = (char *)path;
> > +	char *slash = buf;
> > 
> >   	while ((slash = strchr(slash+1, '/')) != NULL) {
> > -		len = slash - path;
> > -		memcpy(buf, path, len);
> > -		buf[len] = 0;
> > -		mkdir(buf, 0755);
> > +		*slash = '\0';
> > +		if (0 != mkdir(buf, 0755))
> > +			return error("Unable to mkdir(``%s'', 0755)", buf);
> > +		*slash = '/';
> 
> You need the temp buffer.  Simply casting the const away may
> shut up the compiler but it's not correct.

Ok, I see!  Someone will pass a const char * (e.g. static string,
readonly mmap, ...).  In that case I would rather change the signature
of the function ;).  My am I a smart ass, ain't I.

What I wanted to achieve was getting rid of xmalloc that would just
die("horribly") in case we cannot allocate the memory.  Or some
other hacked up malloc / realloc alternative.

Aside: I really do not like the current habit of die("if anything does
   not work out.") in git code all that much.  People seem to take
   the die() as cast in stone, and do not free resources in code
   pathes that lead to a die(), currently.  Of course this die()
   might change in the near future.  And we will have to examine
   every die() very carefully to make sure Joe Lazy Programmer
   didn't leave any garbage lying around.
   
   IMHO, the right thing for die() would be to dump core instead of just
   exiting.  As die() should be called in the ``can't happen'' or rather
   ``isn't resolved correctly'' cases only.  And programmers would be
   able to use the core to identify these cases quickly.

Unfortunately what You say is true and I cannot see a way around
some kind of working copy (except for forcing the caller to provide
a non-const char*).  Maybe I will put ``char scratchpath[MAXPATH + 1];''
and ``#include <sys/param.h>'' in cache.h :).

> 
> > -		if (errno != EEXIST)
> > +		if (EEXIST != errno)
> 
> Too much Star Wars?  Joda-speak?

No.  This is my prefered style for two reasons:

1) Putting the non-l-value on the left hand side protects against
   my most common typo: "=" instead of "==" or "!=".  No matter which
   compiler or warning level.

2) More often than not the variable part will be longer than
   ``errno''.  So putting EEXIST and the comparator front helps seeing
   the condition I test against.  Just compare:

while (-1 != (ch = getopt(argc, argv, "abcde:fg:h:ijkl:mnopq:r:stuvwx:y:z")))
while ((ch = getopt(argc, argv, "abcde:fg:h:ijkl:mnopq:r:stuvwx:y:z")) != -1)


> 
> Ciao, ET.
> 
> 
> PS: the mkdir mode should be 0777 ...
Thanks!  I did just a literal copy of what was there before.  I
added the check for the return value, to get the right diagnostic
output.

Kind regards,

--Robert Suetterlin (robert@mpe.mpg.de)
phone: (+49)89 / 30000-3546   fax: (+49)89 / 30000-3950

      reply	other threads:[~2005-04-29 17:02 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-29 13:19 [PATCH]: first take at cleanup of #include, xmalloc / xrealloc, git status report usage Robert Sütterlin
2005-04-29 16:24 ` Edgar Toernig
2005-04-29 17:07   ` Klaus Robert Suetterlin [this message]

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=20050429170743.GD93323@xdt04.mpe-garching.mpg.de \
    --to=robert@mpe.mpg.de \
    --cc=froese@gmx.de \
    --cc=git@vger.kernel.org \
    /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).