Skip to content

Fix regression in GetVarStr introduced in commit '8a5ada2d7'#480

Open
peolsson wants to merge 1 commit intottlappalainen:masterfrom
peolsson:fix-getvarstr-regression
Open

Fix regression in GetVarStr introduced in commit '8a5ada2d7'#480
peolsson wants to merge 1 commit intottlappalainen:masterfrom
peolsson:fix-getvarstr-regression

Conversation

@peolsson
Copy link
Copy Markdown

When calling GetVarStr with a NULL pointer, it was originally expected that the function returned the length of the string, even though the string itself could not be stored. This is also what is expected in N2kDeviceList.cpp, which makes the change in '8a5ada2d7' to break the expected behaviour.

This change restores the original behaviour.

@ttlappalainen
Copy link
Copy Markdown
Owner

You need to check length for possible UTF8:

...
//*****************************************************************************
int N2kUCS2ToUTF8Len(const unsigned char *str, int strLen, unsigned char nulChar=0xff) {
  int UTF8Len=0;

  for (int i=0; i+1<strLen; i+=2) {
    uint16_t UCS2Char = str[i] + (str[i+1] << 8);

    // Decode
    if ( 0x80 > UCS2Char ) {
      //Tokensize: 1 byte
      if ( UCS2Char!=nulChar ) UTF8Len++;
    } else if ( 0x800 > UCS2Char ) {
      //Tokensize: 2 bytes
      UTF8Len+=2;
    } else {
      //Tokensize: 3 bytes
      UTF8Len+=3;
    }
  }

  return UTF8Len;
}

//*****************************************************************************
int N2kUCS2ToUTF8(const unsigned char *str, int strLen, char* buf, int bufLen, unsigned char nulChar=0xff) {
...
    if ( Type==0x01 ) {
      StrBufSize=Len;
    } else {
      const unsigned char *UnicodeStr=&Data[Index];
      StrBufSize=N2kUCS2ToUTF8Len(UnicodeStr,Len,nulChar);
    }
...

@peolsson peolsson force-pushed the fix-getvarstr-regression branch from 122a389 to 8023cc5 Compare February 15, 2026 18:46
@peolsson
Copy link
Copy Markdown
Author

Thanks for the update. I added handling for unicode strings now as well.

@ttlappalainen
Copy link
Copy Markdown
Owner

Move N2kUCS2ToUTF8Len above N2kUCS2ToUTF8 as I pointed. They have nearley same functionality so better to keep them near each other.

When calling GetVarStr with a NULL pointer, it was originally expected
that the function returned the length of the string, even though the
string itself could not be stored. This is also what is expected in
N2kDeviceList.cpp, which makes the change in '8a5ada2d7' to break the
expected behaviour.

This change restores the original behaviour.
@peolsson peolsson force-pushed the fix-getvarstr-regression branch from 8023cc5 to 6e05186 Compare February 20, 2026 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants