BUG: incorrect buffer alignment in pj_pool_create_on_buf()

IA
Ian Abbott
Wed, Jan 29, 2020 4:39 PM

Hello developers,

This code sequence in pj_pool_create_on_buf() was discussed on
stackoverflow.com[1]:

 /* Check and align buffer */
 align_diff = (pj_size_t)buf;
 if (align_diff & (PJ_POOL_ALIGNMENT-1)) {
align_diff &= (PJ_POOL_ALIGNMENT-1);
buf = (void*) (((char*)buf) + align_diff);
size -= align_diff;
 }

The problem is that it does not round up to the next alignment boundary
properly. (align_diff is the distance from the lower alignment boundary,
not the distance to the upper alignment boundary.)  I suggest the
following correction:

 /* Check and align buffer */
 align_diff = (pj_size_t)buf;
 if (align_diff & (PJ_POOL_ALIGNMENT-1)) {
align_diff &= (PJ_POOL_ALIGNMENT-1);
align_diff = PJ_POOL_ALIGNMENT - align_diff;
buf = (void*) (((char*)buf) + align_diff);
size -= align_diff;
 }

(or something equivalent).

[1] https://stackoverflow.com/questions/59969404/check-and-align-buffer

Regards,
Ian Abbott

--
-=( Ian Abbott abbotti@mev.co.uk || Web: www.mev.co.uk )=-
-=( MEV Ltd. is a company registered in England & Wales. )=-
-=( Registered number: 02862268.  Registered address:    )=-
-=( 15 West Park Road, Bramhall, STOCKPORT, SK7 3JZ, UK. )=-

Hello developers, This code sequence in pj_pool_create_on_buf() was discussed on stackoverflow.com[1]: /* Check and align buffer */ align_diff = (pj_size_t)buf; if (align_diff & (PJ_POOL_ALIGNMENT-1)) { align_diff &= (PJ_POOL_ALIGNMENT-1); buf = (void*) (((char*)buf) + align_diff); size -= align_diff; } The problem is that it does not round up to the next alignment boundary properly. (align_diff is the distance from the lower alignment boundary, not the distance to the upper alignment boundary.) I suggest the following correction: /* Check and align buffer */ align_diff = (pj_size_t)buf; if (align_diff & (PJ_POOL_ALIGNMENT-1)) { align_diff &= (PJ_POOL_ALIGNMENT-1); align_diff = PJ_POOL_ALIGNMENT - align_diff; buf = (void*) (((char*)buf) + align_diff); size -= align_diff; } (or something equivalent). [1] https://stackoverflow.com/questions/59969404/check-and-align-buffer Regards, Ian Abbott -- -=( Ian Abbott <abbotti@mev.co.uk> || Web: www.mev.co.uk )=- -=( MEV Ltd. is a company registered in England & Wales. )=- -=( Registered number: 02862268. Registered address: )=- -=( 15 West Park Road, Bramhall, STOCKPORT, SK7 3JZ, UK. )=-