From 69e5becafcb56b3422ea7fca24e5d814bc7a99e6 Mon Sep 17 00:00:00 2001 From: Pierre Wielders Date: Fri, 27 Sep 2024 20:47:02 +0200 Subject: [PATCH] [NETWORKCONTROL] Also cope with DNS files that have a size of 0.. If the /etc/resolve.conf file (typeically the DNS file) does not exist or is empty, in debug, an ASSERT fires in the DateElement since one triees to access an empty buffer. While fixing this also found that there is already a Search for pattern method in the DataElement, so start using it. Prune where you can prune ;-) --- .../NetworkControlImplementation.cpp | 66 ++++++------------- 1 file changed, 20 insertions(+), 46 deletions(-) diff --git a/NetworkControl/NetworkControlImplementation.cpp b/NetworkControl/NetworkControlImplementation.cpp index 74839eaa..a45c0a37 100644 --- a/NetworkControl/NetworkControlImplementation.cpp +++ b/NetworkControl/NetworkControlImplementation.cpp @@ -1315,66 +1315,40 @@ namespace Plugin if (file.IsValid() == false) { SYSLOG(Logging::Notification, (_T("DNS functionality could NOT be updated [%s]"), _dnsFile.c_str())); } else { - string data((_T("#++SECTION: ")) + _service->Callsign() + '\n'); + uint32_t offset = file.Size(); + string startMarker((_T("#++SECTION: ")) + _service->Callsign() + '\n'); string endMarker((_T("#--SECTION: ")) + _service->Callsign() + '\n'); - uint16_t start = 0; - uint16_t end = 0; - uint16_t offset = 1; - uint8_t index = 0; - - do { - index += offset - 1; - offset = 0; - - // Find the first comparible character && check the remainder in the next step... - while ((index < file.Size()) && (file[index] != data[offset])) { - index++; - } - while ((index < file.Size()) && (offset < data.length()) && (file[index] == data[offset])) { - index++; - offset++; - } - } while ((index < file.Size()) && (offset != data.length())); + if (offset > 0) { + uint32_t start = static_cast(file.Search(0, reinterpret_cast(startMarker.c_str()), startMarker.length())); - start = index - offset; - offset = 1; + if (start < file.Size()) { + // We found a start marker, see if we have an end marker. + uint32_t end = static_cast(file.Search(start + startMarker.length(), reinterpret_cast(endMarker.c_str()), endMarker.length())); - do { - index += offset - 1; - offset = 0; + if (end < file.Size()) { + end += endMarker.length(); + offset -= (end - start); - // Find the first comparible character && check the remainder in the next step... - while ((index < file.Size()) && (file[index] != endMarker[offset])) { - index++; - } - while ((index < file.Size()) && (offset < endMarker.length()) && (file[index] == endMarker[offset])) { - index++; - offset++; + if (offset > start) { + // We found the beginning and the end of the section in this file, Wipe it. + // move everything after de marker, over the marker sections. + ::memcpy(&(file[start]), &file[end], static_cast(file.Size()) - end); + } + } } - - } while ((index < file.Size()) && (offset != endMarker.length())); - - end = ((index != 0) && (index == file.Size())) ? index - 1 : index; - uint16_t reduction = end - start; - - if ((reduction != 0) && ((file.Size() - end) != 0)) { - // move everything after de marker, over the marker sections. - ::memcpy(&(file[start]), &file[end], static_cast(file.Size()) - end); } - offset = (static_cast(file.Size()) - reduction); - NameServers servers; DNS(servers); for (const Core::NodeId& entry : servers) { - data += string("nameserver ") + entry.HostAddress() + '\n'; + startMarker += string("nameserver ") + entry.HostAddress() + '\n'; } - data += endMarker; - file.Size(offset + data.length()); - ::memcpy(&(file[offset]), data.c_str(), data.length()); + startMarker += endMarker; + file.Size(offset + startMarker.length()); + ::memcpy(&(file[offset]), startMarker.c_str(), startMarker.length()); file.Sync(); SYSLOG(Logging::Startup, (_T("DNS functionality updated [%s]"), _dnsFile.c_str()));