Skip to content

Commit e72dcfc

Browse files
committed
Bugfixes
1 parent 65fe0de commit e72dcfc

File tree

3 files changed

+52
-50
lines changed

3 files changed

+52
-50
lines changed

src/main/fmt/obj/Decompressor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ namespace lsp
277277
return (nread < 0) ? status_t(-nread) : STATUS_CORRUPTED;
278278

279279
// Read delta
280-
size_t delta;
280+
size_t delta = 0;
281281
status_t res = read_varint(&delta);
282282
if (res != STATUS_OK)
283283
return res;

src/main/io/OutBitStream.cpp

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
#include <lsp-plug.in/io/OutFileStream.h>
2424
#include <lsp-plug.in/common/endian.h>
2525

26-
#define BITSTREAM_BUFSZ (sizeof(umword_t) * 8)
27-
#define BITSTREAM_BUFSZ32 (sizeof(uint32_t) * 8)
28-
2926
namespace lsp
3027
{
3128
namespace io
3229
{
30+
static constexpr size_t BITSTREAM_BUFSZ = sizeof(umword_t) * 8;
31+
static constexpr size_t BITSTREAM_BUFSZ32 = sizeof(uint32_t) * 8;
32+
3333
OutBitStream::OutBitStream()
3434
{
3535
pOS = NULL;
@@ -234,23 +234,23 @@ namespace lsp
234234

235235
size_t written = 0;
236236

237-
#ifdef LSP_UNALIGNED_MEMORY_SAFE
238-
// x86 allows unaligned access, write with machine words first
239-
const umword_t *wptr = reinterpret_cast<const umword_t *>(buf);
240-
size_t blocks = count & (~(sizeof(umword_t) - 1));
237+
#ifdef LSP_UNALIGNED_MEMORY_SAFE
238+
// x86 allows unaligned access, write with machine words first
239+
const umword_t *wptr = reinterpret_cast<const umword_t *>(buf);
240+
size_t blocks = count & (~(sizeof(umword_t) - 1));
241241

242-
for ( ; written < blocks; written += sizeof(umword_t))
242+
for ( ; written < blocks; written += sizeof(umword_t))
243+
{
244+
status_t res = writev(BE_TO_CPU(*(wptr++)), sizeof(umword_t)*8);
245+
if (res != STATUS_OK)
243246
{
244-
status_t res = writev(BE_TO_CPU(*(wptr++)), sizeof(umword_t)*8);
245-
if (res != STATUS_OK)
246-
{
247-
set_error(res);
248-
return (written <= 0) ? -res : written;
249-
}
247+
set_error(res);
248+
return (written <= 0) ? -res : written;
250249
}
250+
}
251251

252-
buf = wptr;
253-
#endif /* LSP_UNALIGNED_MEMORY_SAFE */
252+
buf = wptr;
253+
#endif /* LSP_UNALIGNED_MEMORY_SAFE */
254254

255255
// Write the rest data with bytes
256256
const uint8_t *bptr = reinterpret_cast<const uint8_t *>(buf);
@@ -274,22 +274,22 @@ namespace lsp
274274

275275
size_t written = 0;
276276

277-
#ifdef LSP_UNALIGNED_MEMORY_SAFE
278-
// x86 allows unaligned memory access, write with machine words first
279-
const umword_t *wptr = reinterpret_cast<const umword_t *>(buf);
280-
size_t blocks = bits & (~((sizeof(umword_t) << 3) - 1));
281-
for ( ; written < blocks; written += sizeof(umword_t)*8)
277+
#ifdef LSP_UNALIGNED_MEMORY_SAFE
278+
// x86 allows unaligned memory access, write with machine words first
279+
const umword_t *wptr = reinterpret_cast<const umword_t *>(buf);
280+
size_t blocks = bits & (~((sizeof(umword_t) << 3) - 1));
281+
for ( ; written < blocks; written += sizeof(umword_t)*8)
282+
{
283+
status_t res = writev(BE_TO_CPU(*(wptr++)), sizeof(umword_t)*8);
284+
if (res != STATUS_OK)
282285
{
283-
status_t res = writev(BE_TO_CPU(*(wptr++)), sizeof(umword_t)*8);
284-
if (res != STATUS_OK)
285-
{
286-
set_error(res);
287-
return (written <= 0) ? -res : written;
288-
}
286+
set_error(res);
287+
return (written <= 0) ? -res : written;
289288
}
289+
}
290290

291-
buf = wptr;
292-
#endif /* LSP_UNALIGNED_MEMORY_SAFE */
291+
buf = wptr;
292+
#endif /* LSP_UNALIGNED_MEMORY_SAFE */
293293

294294
// Write the rest data with bytes
295295
const uint8_t *bptr = reinterpret_cast<const uint8_t *>(buf);
@@ -389,7 +389,7 @@ namespace lsp
389389
}
390390

391391
size_t avail = lsp_min(bits, BITSTREAM_BUFSZ - nBits);
392-
nBuffer = (nBuffer << avail) | (value >> (BITSTREAM_BUFSZ32 - avail));
392+
nBuffer = (avail < BITSTREAM_BUFSZ) ? (nBuffer << avail) | (value >> (BITSTREAM_BUFSZ32 - avail)) : avail;
393393
nBits += avail;
394394
bits -= avail;
395395
value <<= avail;
@@ -401,10 +401,22 @@ namespace lsp
401401
status_t OutBitStream::writev(uint64_t value, size_t bits)
402402
{
403403
status_t res;
404-
if (pOS == NULL)
405-
return set_error(STATUS_CLOSED);
406404

407-
#if defined(ARCH_64BIT)
405+
#if defined(ARCH_32BIT)
406+
// Need to write high part?
407+
if (bits > BITSTREAM_BUFSZ32)
408+
{
409+
if ((res = writev(uint32_t(value >> BITSTREAM_BUFSZ32), bits - BITSTREAM_BUFSZ32)) != STATUS_OK)
410+
return res;
411+
bits = BITSTREAM_BUFSZ32;
412+
}
413+
414+
// Write low part
415+
return writev(uint32_t(value), bits);
416+
#else
417+
if (pOS == NULL)
418+
return set_error(STATUS_CLOSED);
419+
408420
value <<= (BITSTREAM_BUFSZ - bits);
409421
while (bits > 0)
410422
{
@@ -416,26 +428,14 @@ namespace lsp
416428
}
417429

418430
size_t avail = lsp_min(bits, BITSTREAM_BUFSZ - nBits);
419-
nBuffer = (nBuffer << avail) | (value >> (BITSTREAM_BUFSZ - avail));
431+
nBuffer = (avail < BITSTREAM_BUFSZ) ? (nBuffer << avail) | (value >> (BITSTREAM_BUFSZ - avail)) : value;
420432
nBits += avail;
421433
bits -= avail;
422434
value <<= avail;
423435
}
424436

425437
return set_error(STATUS_OK);
426-
427-
#else
428-
// Need to write high part?
429-
if (bits > BITSTREAM_BUFSZ)
430-
{
431-
if ((res = writev(uint32_t(value >> BITSTREAM_BUFSZ), bits - BITSTREAM_BUFSZ)) != STATUS_OK)
432-
return res;
433-
bits = BITSTREAM_BUFSZ;
434-
}
435-
436-
// Write low part
437-
return writev(uint32_t(value), bits);
438-
#endif
438+
#endif /* ARCH_32BIT */
439439
}
440440

441441
} /* namespace io */

src/test/utest/fmt/obj/compressor.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,10 @@ UTEST_BEGIN("runtime.fmt.obj", compressor)
484484
printf("Validating result...\n");
485485
ObjRecorder osrc, odst;
486486

487-
UTEST_ASSERT(read_obj_file(&osrc, &src) == STATUS_OK);
488-
UTEST_ASSERT(read_compressed_obj_file(&odst, &dst) == STATUS_OK);
487+
status_t res = read_obj_file(&osrc, &src);
488+
UTEST_ASSERT_MSG(res == STATUS_OK, "read_obj_file failed with code=%d", int(res));
489+
res = read_compressed_obj_file(&odst, &dst);
490+
UTEST_ASSERT_MSG(res == STATUS_OK, "read_compressed_obj_file failed with code=%d", int(res));
489491
ssize_t diff = compare(&osrc, &odst);
490492
UTEST_ASSERT_MSG(diff < 0, "Source and destination records #%d differ", int(diff));
491493
}

0 commit comments

Comments
 (0)