GetCurrentHardwareId sized its bit stream at 125 bits (16 bytes) but writes 147 (3 version + 9 x 16-bit hashes), so the last two hash slots overflowed the heap block by 3 bytes on every id computation. The Windows heap absorbed it silently for years; it SIGSEGVs under macOS malloc — found when ANSGateway enabled this code on POSIX (fixed there in ANSGateway 3a34c68/6c3cdfd; this is the upstream port). Two parts, both id-preserving (only the first 16 bytes feed the base32 encode — verified byte-identical ids before/after in the gateway): - hwid.cpp: size the stream to the 147 bits actually written. - BitStream3: bounds-safe byteAt/setByte accessors on every Write/Read, Attach records the real buffer size, default ctor zero-inits — an under-sized stream can no longer corrupt the heap, and out-of-range reads return 0 (identical to the base32 padding zeros the Match path always read there). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
39 lines
1.1 KiB
C++
39 lines
1.1 KiB
C++
#pragma once
|
|
|
|
class BitStream3
|
|
{
|
|
public:
|
|
BitStream3();
|
|
BitStream3(int bitCount);
|
|
~BitStream3();
|
|
|
|
void Create(int bitCount);
|
|
void Attach(void * buf, int bitCount);
|
|
int Write(const unsigned char * buf, int bitCount);
|
|
int WriteUInt16(unsigned short val);
|
|
int Read(unsigned char * buf, int bitCount);
|
|
int ReadUInt16(unsigned short * val);
|
|
unsigned char * GetBuffer();
|
|
|
|
protected:
|
|
// Bounds-safe accessors: every Write/Read goes through these so a caller
|
|
// that under-sizes the stream can no longer corrupt the heap (the 125-vs-147
|
|
// bug in GetCurrentHardwareId overflowed 3 bytes for years). Out-of-range
|
|
// reads return 0 — identical to the base32 padding zeros the hardware-id
|
|
// Match path has always effectively read there.
|
|
unsigned char byteAt(int i) const
|
|
{
|
|
return (buffer && i >= 0 && i < bufSize) ? buffer[i] : 0;
|
|
}
|
|
void setByte(int i, unsigned char v)
|
|
{
|
|
if (buffer && i >= 0 && i < bufSize) buffer[i] = v;
|
|
}
|
|
|
|
bool ownMemory;
|
|
unsigned char * buffer;
|
|
int bufSize, bitSize;
|
|
int currentBit, currentByte;
|
|
static const unsigned char hiMask[];
|
|
};
|