Skip to content

Commit

Permalink
String \0 terminated in group objects
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeramb committed Jun 19, 2024
1 parent acf4a0b commit c5defe9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
8 changes: 3 additions & 5 deletions src/knx/dptconvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,15 +517,13 @@ int busValueToAccess(const uint8_t* payload, size_t payload_length, const Dpt& d
int busValueToString(const uint8_t* payload, size_t payload_length, const Dpt& datatype, KNXValue& value)
{
ASSERT_PAYLOAD(14);
char strValue[15];
strValue[14] = '\0';
for (int n = 0; n < 14; ++n)
{
strValue[n] = signed8FromPayload(payload, n);
if (!datatype.subGroup && (strValue[n] & 0x80))
auto value = signed8FromPayload(payload, n);
if (!datatype.subGroup && (value & 0x80))
return false;
}
value = strValue;
value = (const char*) payload;
return true;
}

Expand Down
16 changes: 13 additions & 3 deletions src/knx/group_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ GroupObject::GroupObject()

GroupObject::GroupObject(const GroupObject& other)
{
_data = new uint8_t[other._dataLength];
_data = new uint8_t[other._table != nullptr ? other.sizeInMemory() : other._dataLength];
_commFlagEx = other._commFlagEx;
_dataLength = other._dataLength;
_asap = other._asap;
Expand Down Expand Up @@ -114,12 +114,12 @@ size_t GroupObject::goSize()
size_t size = sizeInTelegram();
if (size == 0)
return 1;

return size;
}

// see knxspec 3.5.1 p. 178
size_t GroupObject::asapValueSize(uint8_t code)
size_t GroupObject::asapValueSize(uint8_t code) const
{
if (code < 7)
return 0;
Expand Down Expand Up @@ -194,6 +194,16 @@ size_t GroupObject::sizeInTelegram()
return asapValueSize(code);
}

size_t GroupObject::sizeInMemory() const
{
uint8_t code = lowByte(ntohs(_table->_tableData[_asap]));
size_t result = asapValueSize(code);
if (code == 0)
return 1;
if (code == 14)
return 14 + 1;
}

#ifdef SMALL_GROUPOBJECT
GroupObjectUpdatedHandler GroupObject::classCallback()
{
Expand Down
7 changes: 6 additions & 1 deletion src/knx/group_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ class GroupObject
* will return 0.
*/
size_t sizeInTelegram();
/**
* returns the size of the group object in the heap memory of the group object. The function returns the same value as goSize(),
* exept fot the 14 byte string type to reserve one byte of a \0 terminator character.
*/
size_t sizeInMemory() const;
/**
* returns the pointer to the value of the group object. This can be used if a datapoint type is not supported or if you want do
* your own conversion.
Expand Down Expand Up @@ -274,7 +279,7 @@ class GroupObject
static GroupObjectUpdatedHandler _updateHandlerStatic;
#endif

size_t asapValueSize(uint8_t code);
size_t asapValueSize(uint8_t code) const;
size_t goSize();
uint16_t _asap = 0;
ComFlagEx _commFlagEx;
Expand Down
4 changes: 2 additions & 2 deletions src/knx/group_object_table_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ bool GroupObjectTableObject::initGroupObjects()
GroupObject& go = _groupObjects[asap - 1];
go._asap = asap;
go._table = this;

go._dataLength = go.goSize();
go._data = new uint8_t[go._dataLength];
go._data = new uint8_t[go.sizeInMemory()];
memset(go._data, 0, go._dataLength);

if (go.valueReadOnInit())
Expand Down

0 comments on commit c5defe9

Please sign in to comment.