[cli] update CLI DNS callbacks to output error for long/invalid names (#11436)

This commit updates the DNS callbacks `HandleDnsBrowseResponse()`,
`HandleDnsServiceResponse()`, `HandleDnsRecordResponse()`, etc.,
to output an error if the query name is invalid or too long.

This change replaces previous `IgnoreError()` calls with specific
error handling code for these cases. This should help address CLI
Fuzzer test failures where long or invalid names might be generated
as CLI input.
This commit is contained in:
Abtin Keshavarzian
2025-04-23 13:23:55 -07:00
committed by GitHub
parent 20aefc2215
commit b0176443cc
+58 -55
View File
@@ -573,28 +573,29 @@ void Dns::HandleDnsAddressResponse(otError aError, const otDnsAddressResponse *a
void Dns::HandleDnsAddressResponse(otError aError, const otDnsAddressResponse *aResponse) void Dns::HandleDnsAddressResponse(otError aError, const otDnsAddressResponse *aResponse)
{ {
otError error;
char hostName[OT_DNS_MAX_NAME_SIZE]; char hostName[OT_DNS_MAX_NAME_SIZE];
otIp6Address address; otIp6Address address;
uint32_t ttl; uint32_t ttl;
uint16_t index = 0;
IgnoreError(otDnsAddressResponseGetHostName(aResponse, hostName, sizeof(hostName))); SuccessOrExit(error = otDnsAddressResponseGetHostName(aResponse, hostName, sizeof(hostName)));
OutputFormat("DNS response for %s - ", hostName); OutputFormat("DNS response for %s - ", hostName);
if (aError == OT_ERROR_NONE) error = aError;
{ SuccessOrExit(error);
uint16_t index = 0;
while (otDnsAddressResponseGetAddress(aResponse, index, &address, &ttl) == OT_ERROR_NONE) while (otDnsAddressResponseGetAddress(aResponse, index, &address, &ttl) == OT_ERROR_NONE)
{ {
OutputIp6Address(address); OutputIp6Address(address);
OutputFormat(" TTL:%lu ", ToUlong(ttl)); OutputFormat(" TTL:%lu ", ToUlong(ttl));
index++; index++;
}
} }
OutputNewLine(); OutputNewLine();
OutputResult(aError);
exit:
OutputResult(error);
} }
#if OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE #if OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
@@ -630,39 +631,39 @@ void Dns::HandleDnsBrowseResponse(otError aError, const otDnsBrowseResponse *aRe
void Dns::HandleDnsBrowseResponse(otError aError, const otDnsBrowseResponse *aResponse) void Dns::HandleDnsBrowseResponse(otError aError, const otDnsBrowseResponse *aResponse)
{ {
Error error;
char name[OT_DNS_MAX_NAME_SIZE]; char name[OT_DNS_MAX_NAME_SIZE];
char label[OT_DNS_MAX_LABEL_SIZE]; char label[OT_DNS_MAX_LABEL_SIZE];
uint8_t txtBuffer[kMaxTxtDataSize]; uint8_t txtBuffer[kMaxTxtDataSize];
otDnsServiceInfo serviceInfo; otDnsServiceInfo serviceInfo;
uint16_t index = 0;
IgnoreError(otDnsBrowseResponseGetServiceName(aResponse, name, sizeof(name))); SuccessOrExit(error = otDnsBrowseResponseGetServiceName(aResponse, name, sizeof(name)));
OutputLine("DNS browse response for %s", name); OutputLine("DNS browse response for %s", name);
if (aError == OT_ERROR_NONE) error = aError;
SuccessOrExit(error);
while (otDnsBrowseResponseGetServiceInstance(aResponse, index, label, sizeof(label)) == OT_ERROR_NONE)
{ {
uint16_t index = 0; OutputLine("%s", label);
index++;
while (otDnsBrowseResponseGetServiceInstance(aResponse, index, label, sizeof(label)) == OT_ERROR_NONE) serviceInfo.mHostNameBuffer = name;
serviceInfo.mHostNameBufferSize = sizeof(name);
serviceInfo.mTxtData = txtBuffer;
serviceInfo.mTxtDataSize = sizeof(txtBuffer);
if (otDnsBrowseResponseGetServiceInfo(aResponse, label, &serviceInfo) == OT_ERROR_NONE)
{ {
OutputLine("%s", label); OutputDnsServiceInfo(kIndentSize, serviceInfo);
index++;
serviceInfo.mHostNameBuffer = name;
serviceInfo.mHostNameBufferSize = sizeof(name);
serviceInfo.mTxtData = txtBuffer;
serviceInfo.mTxtDataSize = sizeof(txtBuffer);
if (otDnsBrowseResponseGetServiceInfo(aResponse, label, &serviceInfo) == OT_ERROR_NONE)
{
OutputDnsServiceInfo(kIndentSize, serviceInfo);
}
OutputNewLine();
} }
OutputNewLine();
} }
OutputResult(aError); exit:
OutputResult(error);
} }
void Dns::HandleDnsServiceResponse(otError aError, const otDnsServiceResponse *aResponse, void *aContext) void Dns::HandleDnsServiceResponse(otError aError, const otDnsServiceResponse *aResponse, void *aContext)
@@ -672,30 +673,31 @@ void Dns::HandleDnsServiceResponse(otError aError, const otDnsServiceResponse *a
void Dns::HandleDnsServiceResponse(otError aError, const otDnsServiceResponse *aResponse) void Dns::HandleDnsServiceResponse(otError aError, const otDnsServiceResponse *aResponse)
{ {
otError error;
char name[OT_DNS_MAX_NAME_SIZE]; char name[OT_DNS_MAX_NAME_SIZE];
char label[OT_DNS_MAX_LABEL_SIZE]; char label[OT_DNS_MAX_LABEL_SIZE];
uint8_t txtBuffer[kMaxTxtDataSize]; uint8_t txtBuffer[kMaxTxtDataSize];
otDnsServiceInfo serviceInfo; otDnsServiceInfo serviceInfo;
IgnoreError(otDnsServiceResponseGetServiceName(aResponse, label, sizeof(label), name, sizeof(name))); SuccessOrExit(error = otDnsServiceResponseGetServiceName(aResponse, label, sizeof(label), name, sizeof(name)));
OutputLine("DNS service resolution response for %s for service %s", label, name); OutputLine("DNS service resolution response for %s for service %s", label, name);
if (aError == OT_ERROR_NONE) error = aError;
{ SuccessOrExit(error);
serviceInfo.mHostNameBuffer = name;
serviceInfo.mHostNameBufferSize = sizeof(name);
serviceInfo.mTxtData = txtBuffer;
serviceInfo.mTxtDataSize = sizeof(txtBuffer);
if (otDnsServiceResponseGetServiceInfo(aResponse, &serviceInfo) == OT_ERROR_NONE) serviceInfo.mHostNameBuffer = name;
{ serviceInfo.mHostNameBufferSize = sizeof(name);
OutputDnsServiceInfo(/* aIndentSize */ 0, serviceInfo); serviceInfo.mTxtData = txtBuffer;
OutputNewLine(); serviceInfo.mTxtDataSize = sizeof(txtBuffer);
}
if (otDnsServiceResponseGetServiceInfo(aResponse, &serviceInfo) == OT_ERROR_NONE)
{
OutputDnsServiceInfo(/* aIndentSize */ 0, serviceInfo);
OutputNewLine();
} }
OutputResult(aError); exit:
OutputResult(error);
} }
#endif // OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE #endif // OPENTHREAD_CONFIG_DNS_CLIENT_SERVICE_DISCOVERY_ENABLE
@@ -708,15 +710,16 @@ void Dns::HandleDnsRecordResponse(otError aError, const otDnsRecordResponse *aRe
void Dns::HandleDnsRecordResponse(otError aError, const otDnsRecordResponse *aResponse) void Dns::HandleDnsRecordResponse(otError aError, const otDnsRecordResponse *aResponse)
{ {
otError error;
char name[OT_DNS_MAX_NAME_SIZE]; char name[OT_DNS_MAX_NAME_SIZE];
uint8_t data[kMaxRrDataSize]; uint8_t data[kMaxRrDataSize];
otDnsRecordInfo recordInfo; otDnsRecordInfo recordInfo;
IgnoreError(otDnsRecordResponseGetQueryName(aResponse, name, sizeof(name))); SuccessOrExit(error = otDnsRecordResponseGetQueryName(aResponse, name, sizeof(name)));
OutputLine("DNS query response for %s ", name); OutputLine("DNS query response for %s ", name);
SuccessOrExit(aError); error = aError;
SuccessOrExit(error);
for (uint16_t index = 0;; index++) for (uint16_t index = 0;; index++)
{ {
@@ -724,17 +727,17 @@ void Dns::HandleDnsRecordResponse(otError aError, const otDnsRecordResponse *aRe
recordInfo.mNameBuffer = name; recordInfo.mNameBuffer = name;
recordInfo.mNameBufferSize = sizeof(name); recordInfo.mNameBufferSize = sizeof(name);
recordInfo.mDataBuffer = data; recordInfo.mDataBuffer = data;
recordInfo.mDataBufferSize = sizeof(name); recordInfo.mDataBufferSize = sizeof(data);
aError = otDnsRecordResponseGetRecordInfo(aResponse, index, &recordInfo); error = otDnsRecordResponseGetRecordInfo(aResponse, index, &recordInfo);
if (aError == OT_ERROR_NOT_FOUND) if (error == OT_ERROR_NOT_FOUND)
{ {
aError = OT_ERROR_NONE; error = OT_ERROR_NONE;
ExitNow(); ExitNow();
} }
SuccessOrExit(aError); SuccessOrExit(error);
OutputLine("%u)", index); OutputLine("%u)", index);
OutputLine(kIndentSize, "RecordType:%u, RecordLength:%u, TTL:%lu, Section:%s", recordInfo.mRecordType, OutputLine(kIndentSize, "RecordType:%u, RecordLength:%u, TTL:%lu, Section:%s", recordInfo.mRecordType,
@@ -752,7 +755,7 @@ void Dns::HandleDnsRecordResponse(otError aError, const otDnsRecordResponse *aRe
} }
exit: exit:
OutputResult(aError); OutputResult(error);
} }
const char *Dns::RecordSectionToString(otDnsRecordSection aSection) const char *Dns::RecordSectionToString(otDnsRecordSection aSection)