Skip to content

Commit 87444bb

Browse files
authored
Simplify parse loop. (#2080)
* Simplify column parsing. * Fix boundary condition. * Mirror previous implementation where no column is added if no null terminator is found. * Lint * Fix warning.
1 parent 71e4a43 commit 87444bb

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

Packet++/src/PostgresLayer.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -761,25 +761,23 @@ namespace pcpp
761761

762762
PostgresColumnInfo column;
763763

764-
const char* nameEnd = static_cast<const char*>(memchr(iter, '\0', end - iter));
765-
766-
if (nameEnd != nullptr)
767-
{
768-
column.name.assign(iter, nameEnd - iter);
769-
iter = nameEnd + 1;
770-
}
771-
else
764+
const char* nameEnd = std::find(iter, end, '\0');
765+
if (nameEnd == end)
772766
{
773-
column.name.assign(iter, end - iter);
774767
break;
775768
}
776769

777-
if (iter + sizeof(::internal::PostgresColumnFixedData) > end)
770+
column.name.assign(iter, nameEnd);
771+
772+
// +1 offset because the nameEnd is currently at the null terminator of column name.
773+
if (end - nameEnd < static_cast<std::ptrdiff_t>(sizeof(::internal::PostgresColumnFixedData) + 1))
778774
{
779775
columns.push_back(column);
780776
break;
781777
}
782778

779+
iter = nameEnd + 1;
780+
783781
const auto* fixedData = reinterpret_cast<const ::internal::PostgresColumnFixedData*>(iter);
784782
column.tableOID = be32toh(fixedData->tableOID);
785783
column.columnIndex = be16toh(fixedData->columnIndex);

0 commit comments

Comments
 (0)