initial headway is limited by both file_size and mhcmdi.sizeofcmds

https://github.com/upx/upx/issues/732
	modified:   p_mach.cpp
This commit is contained in:
John Reiser 2023-11-17 07:23:11 -08:00
parent e79c790264
commit ae77fd60bb
1 changed files with 2 additions and 2 deletions

View File

@ -1952,7 +1952,7 @@ tribool PackMachBase<T>::canPack()
throwCantPack("256 < Mach_header.ncmds");
}
unsigned const sz_mhcmds = (unsigned)mhdri.sizeofcmds;
unsigned headway = file_size - sizeof(mhdri);
unsigned headway = umin(sz_mhcmds, file_size - sizeof(mhdri));
if (headway < sz_mhcmds) {
char buf[32]; snprintf(buf, sizeof(buf), "bad sizeofcmds %d", sz_mhcmds);
throwCantPack(buf);
@ -1961,7 +1961,7 @@ tribool PackMachBase<T>::canPack()
|| 32768 < sz_mhcmds) { // somewhat arbitrary, but *-darwin.macho-upxmain.c
throwCantPack("32768 < Mach_header.sizeofcmds (or ==0)");
}
rawmseg_buf.alloc(sz_mhcmds);
rawmseg_buf.alloc(headway);
rawmseg = (Mach_segment_command *)(void *)rawmseg_buf;
fi->readx(rawmseg, mhdri.sizeofcmds);