[RFC][plugin]: fix fixme

Denis Lila dlila at redhat.com
Wed Mar 30 08:00:44 PDT 2011


> return id++; // we have now made id = 4 but 4 is in use

That's right, but we return 3. id needs to be incremented
to the next, and 3 is the current.

----- Original Message -----
> ----- Original Message -----
> > > Are we not worried about the identifiers going into negatives?
> >
> > At first I didn't think this would be possible without running
> > out of memory first, but looking at it again, you're right - in
> > very rare cases it is possible to not run out of memory and still
> > have that function return a negative number (but none of these
> > cases are possible right now, since we don't really dereference
> > anything). Still, it's good to guard against all possibilities.
> >
> > I've attached the improved patch.
> >
> > Regards,
> > Denis.
> 
> -- snip --
> 
> + private static boolean checkNeg() {
> + if (nextUniqueIdentifier < 1) {
> + wrapped = true;
> + nextUniqueIdentifier = 1;
> + }
> + return wrapped;
> + }
> + private int getNextID() {
> + // TODO: we may want to count the number of iterations in this
> + // loop. If we're wasting too much time there, throw some error
> + // to indicate that we need to switch to longs.
> + while (checkNeg() && objects.containsKey(nextUniqueIdentifier))
> + nextUniqueIdentifier++;
> + return nextUniqueIdentifier++;
> + }
> 
> 
> In getNextID, I think you're going to be incrementing it by 1 extra.
> 
> pretend max int = 6
> id in use: 1 2 3 4 5 6
> 
> nextUniqueIdentifier = 1
> once it reaches 6 in use it wraps.
> 3 gets dereferenced before we reach it.
> 1 2 4 5 6
> you get to 2...
> checkNeg() = true, containsKey = true
> id++; // id is now 3
> checkNeg = true, containsKey = false

> 
> -- Andrew



More information about the distro-pkg-dev mailing list