MikroTik API: Undefined Behaviour in C / C++ Sample Code

Hi,

i came across some issue with the MikroTik Api:
I’m requesting some status information in blocks (/system/resource/print) with the C++ Api code.

Actually with one of the routers in our facility (currently 4 CCR1036-12G-4S) the code ends with
an endless loop because sentenceOut is never getting a returnType.

...
void MikrotikAPI::ReadBlock(Block &block)
{
    Sentence sentence;
    block.Clear();

    MIKROTIK_DEBUG ? printf("ReadBlock\n") : 0;

    do {
        ReadSentence(sentence);
        MIKROTIK_DEBUG ? printf("ReadSentence succeeded.\n") : 0;

        block.AddSentence(sentence);
...

This is caused by this piece of code

...

int MikrotikAPI::ReadLength()
{
    char firstChar; // first character read from socket
    char *lengthData;   // length of next message to read...will be cast to int at the end
    int *messageLength;       // calculated length of next message (Cast to int)

    lengthData = (char *) calloc(sizeof(int), 1);

    MIKROTIK_DEBUG ? printf("Start ReadLength()\n") : 0;

    read(fdSock, &firstChar, 1);

    MIKROTIK_DEBUG ? printf("byte1 = %#x\n", firstChar) : 0;

    // read 4 bytes
    // this code SHOULD work, but is untested...
    if ((firstChar & 0xE0) == 0xE0) {
        MIKROTIK_DEBUG ? printf("4-byte encoded length\n") : 0;

...

This code actually results in “undefined behavior” since firstChar is not initialized to any value and if the read
system call writes zero bytes it will stay with any value. (the memory had before).

However this undefined behavior thing (c code is affected as well) is not the reason
since with my compiler it was always zero.

The problem is that actually zero bytes are written, and then it’s adding unlimited sentences to a block
since none of the sentences have a returnValue of != MIKROTIK_NONE.

I was not able to fix it yet (will be soon, i guess).
Anyone came across this issue? has a fix?