Skip to content

Commit 11aa22a

Browse files
Only define DNS_ReadNameField when cache is used
1 parent 4b68d9a commit 11aa22a

File tree

1 file changed

+87
-85
lines changed

1 file changed

+87
-85
lines changed

source/FreeRTOS_DNS_Parser.c

Lines changed: 87 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#if ( ipconfigUSE_DNS != 0 )
4949

5050

51+
#if ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )
5152
/**
5253
* @brief Read the Name field out of a DNS response packet.
5354
*
@@ -56,110 +57,111 @@
5657
*
5758
* @return If a fully formed name was found, then return the number of bytes processed in pucByte.
5859
*/
59-
size_t DNS_ReadNameField( ParseSet_t * pxSet,
60-
size_t uxDestLen )
61-
{
62-
size_t uxNameLen = 0U;
63-
size_t uxIndex = 0U;
64-
size_t uxSourceLen = pxSet->uxSourceBytesRemaining;
65-
const uint8_t * pucByte = pxSet->pucByte;
66-
67-
/* uxCount gets the values from pucByte and counts down to 0.
68-
* No need to have a different type than that of pucByte */
69-
size_t uxCount;
70-
71-
if( uxSourceLen == ( size_t ) 0U )
60+
size_t DNS_ReadNameField( ParseSet_t * pxSet,
61+
size_t uxDestLen )
7262
{
73-
/* Return 0 value in case of error. */
74-
uxIndex = 0U;
75-
}
76-
77-
/* Determine if the name is the fully coded name, or an offset to the name
78-
* elsewhere in the message. */
79-
else if( ( pucByte[ uxIndex ] & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )
80-
{
81-
/* Jump over the two byte offset. */
82-
if( uxSourceLen > sizeof( uint16_t ) )
83-
{
84-
uxIndex += sizeof( uint16_t );
85-
}
86-
else
63+
size_t uxNameLen = 0U;
64+
size_t uxIndex = 0U;
65+
size_t uxSourceLen = pxSet->uxSourceBytesRemaining;
66+
const uint8_t * pucByte = pxSet->pucByte;
67+
68+
/* uxCount gets the values from pucByte and counts down to 0.
69+
* No need to have a different type than that of pucByte */
70+
size_t uxCount;
71+
72+
if( uxSourceLen == ( size_t ) 0U )
8773
{
74+
/* Return 0 value in case of error. */
8875
uxIndex = 0U;
8976
}
90-
}
91-
else
92-
{
93-
/* 'uxIndex' points to the full name. Walk over the string. */
94-
while( ( uxIndex < uxSourceLen ) && ( pucByte[ uxIndex ] != ( uint8_t ) 0x00U ) )
77+
78+
/* Determine if the name is the fully coded name, or an offset to the name
79+
* elsewhere in the message. */
80+
else if( ( pucByte[ uxIndex ] & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )
9581
{
96-
/* If this is not the first time through the loop, then add a
97-
* separator in the output. */
98-
if( ( uxNameLen > 0U ) )
99-
{
100-
/*
101-
* uxNameLen can never be greater than uxDestLen, since there are checks
102-
* outside this condition, so the check is removed.
103-
*/
104-
pxSet->pcName[ uxNameLen ] = '.';
105-
uxNameLen++;
106-
}
107-
108-
/* Process the first/next sub-string. */
109-
uxCount = ( size_t ) pucByte[ uxIndex ];
110-
111-
/* uxIndex should point to the first character now, unless uxCount
112-
* is an offset field. */
113-
uxIndex++;
114-
115-
if( ( uxIndex + uxCount ) > uxSourceLen )
82+
/* Jump over the two byte offset. */
83+
if( uxSourceLen > sizeof( uint16_t ) )
11684
{
117-
uxIndex = 0U;
118-
break;
85+
uxIndex += sizeof( uint16_t );
11986
}
120-
121-
if( ( uxNameLen + uxCount ) >= uxDestLen )
87+
else
12288
{
12389
uxIndex = 0U;
124-
break;
125-
}
126-
127-
while( uxCount-- != 0U )
128-
{
129-
/*
130-
* uxNameLen can never be greater than uxDestLen, since there are checks
131-
* outside this condition, so the check is removed.
132-
*/
133-
pxSet->pcName[ uxNameLen ] = ( char ) pucByte[ uxIndex ];
134-
uxNameLen++;
135-
uxIndex++;
13690
}
13791
}
138-
139-
/* Confirm that a fully formed name was found. */
140-
if( uxIndex > 0U )
92+
else
14193
{
142-
/* Here, there is no need to check for pucByte[ uxindex ] == 0 because:
143-
* When we break out of the above while loop, uxIndex is made 0 thereby
144-
* failing above check. Whenever we exit the loop otherwise, either
145-
* pucByte[ uxIndex ] == 0 (which makes the check here unnecessary) or
146-
* uxIndex >= uxSourceLen (which makes sure that we do not go in the 'if'
147-
* case).
148-
*/
149-
if( uxIndex < uxSourceLen )
94+
/* 'uxIndex' points to the full name. Walk over the string. */
95+
while( ( uxIndex < uxSourceLen ) && ( pucByte[ uxIndex ] != ( uint8_t ) 0x00U ) )
15096
{
151-
pxSet->pcName[ uxNameLen ] = '\0';
97+
/* If this is not the first time through the loop, then add a
98+
* separator in the output. */
99+
if( ( uxNameLen > 0U ) )
100+
{
101+
/*
102+
* uxNameLen can never be greater than uxDestLen, since there are checks
103+
* outside this condition, so the check is removed.
104+
*/
105+
pxSet->pcName[ uxNameLen ] = '.';
106+
uxNameLen++;
107+
}
108+
109+
/* Process the first/next sub-string. */
110+
uxCount = ( size_t ) pucByte[ uxIndex ];
111+
112+
/* uxIndex should point to the first character now, unless uxCount
113+
* is an offset field. */
152114
uxIndex++;
115+
116+
if( ( uxIndex + uxCount ) > uxSourceLen )
117+
{
118+
uxIndex = 0U;
119+
break;
120+
}
121+
122+
if( ( uxNameLen + uxCount ) >= uxDestLen )
123+
{
124+
uxIndex = 0U;
125+
break;
126+
}
127+
128+
while( uxCount-- != 0U )
129+
{
130+
/*
131+
* uxNameLen can never be greater than uxDestLen, since there are checks
132+
* outside this condition, so the check is removed.
133+
*/
134+
pxSet->pcName[ uxNameLen ] = ( char ) pucByte[ uxIndex ];
135+
uxNameLen++;
136+
uxIndex++;
137+
}
153138
}
154-
else
139+
140+
/* Confirm that a fully formed name was found. */
141+
if( uxIndex > 0U )
155142
{
156-
uxIndex = 0U;
143+
/* Here, there is no need to check for pucByte[ uxindex ] == 0 because:
144+
* When we break out of the above while loop, uxIndex is made 0 thereby
145+
* failing above check. Whenever we exit the loop otherwise, either
146+
* pucByte[ uxIndex ] == 0 (which makes the check here unnecessary) or
147+
* uxIndex >= uxSourceLen (which makes sure that we do not go in the 'if'
148+
* case).
149+
*/
150+
if( uxIndex < uxSourceLen )
151+
{
152+
pxSet->pcName[ uxNameLen ] = '\0';
153+
uxIndex++;
154+
}
155+
else
156+
{
157+
uxIndex = 0U;
158+
}
157159
}
158160
}
161+
162+
return uxIndex;
159163
}
160-
161-
return uxIndex;
162-
}
164+
#endif /* ipconfigUSE_DNS_CACHE || ipconfigDNS_USE_CALLBACKS */
163165

164166
/**
165167
* @brief Simple routine that jumps over the NAME field of a resource record.

0 commit comments

Comments
 (0)