On Tue, 2019-11-05 at 15:48 +0100, Jan Beich wrote:
> Kurt Miller <[email protected]> writes:
> 
> > 
> > On Tue, 2019-11-05 at 14:44 +0100, Jan Beich wrote:
> > 
> > > 
> > > Kurt Miller <[email protected]> writes:
> > > 
> > > > 
> > > > 
> > > > On Tue, 2019-11-05 at 09:17 +0100, Jan Beich wrote:
> > > > 
> > > > > 
> > > > > 
> > > > > Jeremie Courreges-Anglas <[email protected]> writes:
> > > > > 
> > > > > > 
> > > > > > 
> > > > > > 
> > > > > > ++#elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || 
> > > > > > defined(HW_PHYSMEM64))
> > > > > > +   int64_t physical_memory;
> > > > > HW_MEMSIZE and HW_PHYSMEM64 return uint64_t, not int64_t.
> > > > > 
> > > > > > 
> > > > > > 
> > > > > > 
> > > > > > ++#elif defined(HAVE_BSD_SYSCTL) && defined(HW_PHYSMEM))
> > > > > > ++  int physical_memory;
> > > > > HW_PHYSMEM returns u_long (unsigned long) on DragonFly and FreeBSD.
> > > > > int or signed long may upset -fsanitize=integer on 32-bit archs.
> > > > > 
> > > > > Note, the code can be simplified via sysconf(3).
> > > > > 
> > > > > --- builtin/gc.c      2019-11-04 05:07:07 UTC
> > > > > +++ builtin/gc.c
> > > > > @@ -243,20 +243,27 @@ static uint64_t total_ram(void)
> > > > >  
> > > > >       if (!sysinfo(&si))
> > > > >               return si.totalram;
> > > > > -#elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || 
> > > > > defined(HW_PHYSMEM))
> > > > > -     int64_t physical_memory;
> > > > > -     int mib[2];
> > > > > -     size_t length;
> > > > > -
> > > > > -     mib[0] = CTL_HW;
> > > > > +#elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || 
> > > > > defined(HW_PHYSMEM64) || defined(HW_PHYSMEM))
> > > > > +# if defined(HW_MEMSIZE) || defined(HW_PHYSMEM64)
> > > > > +     uint64_t physical_memory;
> > > > > +# else
> > > > > +     u_long physical_memory;
> > > > > +# endif
> > > > > +     int mib[2] = {
> > > > > +             CTL_HW,
> > > > >  # if defined(HW_MEMSIZE)
> > > > > -     mib[1] = HW_MEMSIZE;
> > > > > +             HW_MEMSIZE,
> > > > > +# elif defined(HW_PHYSMEM64)
> > > > > +             HW_PHYSMEM64,
> > > > >  # else
> > > > > -     mib[1] = HW_PHYSMEM;
> > > > > +             HW_PHYSMEM,
> > > > >  # endif
> > > > > -     length = sizeof(int64_t);
> > > > > +     };
> > > > > +     size_t length = sizeof(mib);
> > > > size_t length = sizeof(physical_memory);
> > > Sorry. sizeof(int[2]) > sizeof(unsigned long) on i386, so sysctl(3)
> > > could overflow &physical_memory iff FreeBSD kernel tried to return
> > > larger value or padded it with junk/zeros.
> > I think you are confused. The fourth argument to sysctl(2) is
> > the address of a size_t that contains sizeof the third argument
> > before the call. Also HW_PHYSMEM64 is int64_t on OpenBSD.
> I didn't disagree. My reply was an attempt to understand what may go
> wrong at runtime as the typo didn't trigger -fsanitize=address.

Oh, sorry. I misunderstood your reply and thought it was
trying to explain why sizeof(mib) was correct.

-Kurt

Reply via email to