Bug in _opcZipFileRead

Coordinator
Mar 10, 2014 at 2:48 PM
There appears to be a bug in _opcZipFileRead() in zip.c.
static inline opc_uint32_t _opcZipFileRead(opcIO_t *io, opc_uint8_t *buf, opc_uint32_t buf_len) {
    OPC_ASSERT(NULL!=io && io->_ioread!=NULL && NULL!=buf);
    opc_uint32_t ret=0;
    if (OPC_ERROR_NONE==io->state.err) {
        int len=io->_ioread(io->iocontext, (char *)buf, buf_len);
        if (ret<0) {
            io->state.err=OPC_ERROR_STREAM;
        } else {
            ret=(opc_uint32_t)len;
            io->state.buf_pos+=ret;
        }
    }
    return ret;
}
The test of: if (ret<0) will always be false since ret is initialized to zero. Also, ret us unsigned so it's impossible to be < 0.

I assume the test should be: if (len < 0) though in that case, ret will be zero; is that the desired return code for an error?

John
Coordinator
Mar 10, 2014 at 2:51 PM
Ah yes, I see this is reported in the issues with an attached fix. Should have checked there first.
Coordinator
Mar 10, 2014 at 2:53 PM
But the fix is still not correct... it's casting len to (opc_uint32_t) which cannot be < 0.