From: Johan Herland Date: Tue, 08 Dec 2009 01:55:17 GMT Subject: Re: [RFC/PATCHv10 01/11] fast-import: Proper notes tree manipulation Message-ID: <200912080255.17568.johan@herland.net> In-Reply-To: <20091207164311.GE17173@spearce.org> On Monday 07 December 2009, Shawn O. Pearce wrote: > Johan Herland wrote: > > +static unsigned char convert_num_notes_to_fanout(uintmax_t num_notes) > > +{ > > + unsigned char fanout = 0; > > + while ((num_notes >>= 8)) > > + fanout++; > > + return fanout; > > +} > > + > > +static void construct_path_with_fanout(const char *hex_sha1, > > + unsigned char fanout, char *path) > > +{ > > + unsigned int i = 0, j = 0; > > + if (fanout >= 20) > > + die("Too large fanout (%u)", fanout); > > Shouldn't convert_num_notes_to_fanout have a guard to prevent this > case from happening? Well, it sort of already does (unless uintmax_t is more than 19 * 8 = 152 bits wide... ;) Not sure what you're getting at: - Should I add a "&& fanout < 19" condition to the while loop in convert_num_notes_to_fanout()? - Should I remove the "if (fanout >= 20) die(...)"? Of course, construct_path_with_fanout() is only supposed to be called with values returned from convert_num_notes_to_fanout(), so the condition only tests a precondition that we believe to be true (FTR, it was converted from an equivalent assert() in an earlier iteration), but I normally test for these things anyway (when they are not blindingly obvious), just to make sure... (and I believe a die(...) is kinder to the user than a segfault...) ...Johan -- Johan Herland, www.herland.net