Skip to content

Commit be57c2b

Browse files
committed
[RTL]
- Use ntstrsafe functions in RtlIpv4AddressToString*. Based on a patch by Mark Jansen. CORE-6490 svn path=/trunk/; revision=67056
1 parent c380c2c commit be57c2b

File tree

1 file changed

+89
-36
lines changed

1 file changed

+89
-36
lines changed

reactos/lib/rtl/network.c

Lines changed: 89 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/* INCLUDES *****************************************************************/
1010

1111
#include <rtl.h>
12-
12+
#include <ntstrsafe.h>
1313
#define NDEBUG
1414
#include <debug.h>
1515

@@ -106,18 +106,29 @@ PSTR
106106
NTAPI
107107
RtlIpv4AddressToStringA(
108108
_In_ const struct in_addr *Addr,
109-
_Out_writes_(16) PCHAR S)
109+
_Out_writes_(IPV4_ADDR_STRING_MAX_LEN) PCHAR S)
110110
{
111-
INT Length;
112-
113-
if (!S) return (PSTR)~0;
114-
115-
Length = sprintf(S, "%u.%u.%u.%u", Addr->S_un.S_un_b.s_b1,
116-
Addr->S_un.S_un_b.s_b2,
117-
Addr->S_un.S_un_b.s_b3,
118-
Addr->S_un.S_un_b.s_b4);
111+
NTSTATUS Status;
112+
PSTR End;
113+
114+
if (!S)
115+
return (PSTR)~0;
116+
117+
Status = RtlStringCchPrintfExA(S,
118+
IPV4_ADDR_STRING_MAX_LEN,
119+
&End,
120+
NULL,
121+
0,
122+
"%u.%u.%u.%u",
123+
Addr->S_un.S_un_b.s_b1,
124+
Addr->S_un.S_un_b.s_b2,
125+
Addr->S_un.S_un_b.s_b3,
126+
Addr->S_un.S_un_b.s_b4);
127+
ASSERT(Status == STATUS_SUCCESS);
128+
if (!NT_SUCCESS(Status))
129+
return (PSTR)~0;
119130

120-
return S + Length;
131+
return End;
121132
}
122133

123134
/*
@@ -131,23 +142,38 @@ RtlIpv4AddressToStringExA(
131142
_Out_writes_to_(*AddressStringLength, *AddressStringLength) PCHAR AddressString,
132143
_Inout_ PULONG AddressStringLength)
133144
{
134-
CHAR Buffer[IPV4_ADDR_STRING_MAX_LEN+IPV4_PORT_STRING_MAX_LEN];
145+
CHAR Buffer[IPV4_ADDR_STRING_MAX_LEN + IPV4_PORT_STRING_MAX_LEN];
146+
NTSTATUS Status;
135147
ULONG Length;
148+
PSTR End;
136149

137150
if (!Address || !AddressString || !AddressStringLength)
138151
return STATUS_INVALID_PARAMETER;
139152

140-
Length = sprintf(Buffer, "%u.%u.%u.%u", Address->S_un.S_un_b.s_b1,
141-
Address->S_un.S_un_b.s_b2,
142-
Address->S_un.S_un_b.s_b3,
143-
Address->S_un.S_un_b.s_b4);
144-
145-
if (Port) Length += sprintf(Buffer + Length, ":%u", WN2H(Port));
153+
Status = RtlStringCchPrintfExA(Buffer,
154+
RTL_NUMBER_OF(Buffer),
155+
&End,
156+
NULL,
157+
0,
158+
Port ? "%u.%u.%u.%u:%u"
159+
: "%u.%u.%u.%u",
160+
Address->S_un.S_un_b.s_b1,
161+
Address->S_un.S_un_b.s_b2,
162+
Address->S_un.S_un_b.s_b3,
163+
Address->S_un.S_un_b.s_b4,
164+
WN2H(Port));
165+
ASSERT(Status == STATUS_SUCCESS);
166+
if (!NT_SUCCESS(Status))
167+
return STATUS_INVALID_PARAMETER;
146168

169+
Length = End - Buffer;
147170
if (*AddressStringLength > Length)
148171
{
172+
Status = RtlStringCchCopyA(AddressString,
173+
*AddressStringLength,
174+
Buffer);
175+
ASSERT(Status == STATUS_SUCCESS);
149176
*AddressStringLength = Length + 1;
150-
strcpy(AddressString, Buffer);
151177
return STATUS_SUCCESS;
152178
}
153179

@@ -162,17 +188,29 @@ PWSTR
162188
NTAPI
163189
RtlIpv4AddressToStringW(
164190
_In_ const struct in_addr *Addr,
165-
_Out_writes_(16) PWCHAR S)
191+
_Out_writes_(IPV4_ADDR_STRING_MAX_LEN) PWCHAR S)
166192
{
167-
INT Length;
168-
169-
if (!S) return (PWSTR)~0;
193+
NTSTATUS Status;
194+
PWSTR End;
195+
196+
if (!S)
197+
return (PWSTR)~0;
198+
199+
Status = RtlStringCchPrintfExW(S,
200+
IPV4_ADDR_STRING_MAX_LEN,
201+
&End,
202+
NULL,
203+
0,
204+
L"%u.%u.%u.%u",
205+
Addr->S_un.S_un_b.s_b1,
206+
Addr->S_un.S_un_b.s_b2,
207+
Addr->S_un.S_un_b.s_b3,
208+
Addr->S_un.S_un_b.s_b4);
209+
ASSERT(Status == STATUS_SUCCESS);
210+
if (!NT_SUCCESS(Status))
211+
return (PWSTR)~0;
170212

171-
Length = swprintf(S, L"%u.%u.%u.%u", Addr->S_un.S_un_b.s_b1,
172-
Addr->S_un.S_un_b.s_b2,
173-
Addr->S_un.S_un_b.s_b3,
174-
Addr->S_un.S_un_b.s_b4);
175-
return S + Length;
213+
return End;
176214
}
177215

178216
/*
@@ -186,23 +224,38 @@ RtlIpv4AddressToStringExW(
186224
_Out_writes_to_(*AddressStringLength, *AddressStringLength) PWCHAR AddressString,
187225
_Inout_ PULONG AddressStringLength)
188226
{
189-
WCHAR Buffer[IPV4_ADDR_STRING_MAX_LEN+IPV4_PORT_STRING_MAX_LEN];
227+
WCHAR Buffer[IPV4_ADDR_STRING_MAX_LEN + IPV4_PORT_STRING_MAX_LEN];
228+
NTSTATUS Status;
190229
ULONG Length;
230+
PWSTR End;
191231

192232
if (!Address || !AddressString || !AddressStringLength)
193233
return STATUS_INVALID_PARAMETER;
194234

195-
Length = swprintf(Buffer, L"%u.%u.%u.%u", Address->S_un.S_un_b.s_b1,
196-
Address->S_un.S_un_b.s_b2,
197-
Address->S_un.S_un_b.s_b3,
198-
Address->S_un.S_un_b.s_b4);
199-
200-
if (Port) Length += swprintf(Buffer + Length, L":%u", WN2H(Port));
235+
Status = RtlStringCchPrintfExW(Buffer,
236+
RTL_NUMBER_OF(Buffer),
237+
&End,
238+
NULL,
239+
0,
240+
Port ? L"%u.%u.%u.%u:%u"
241+
: L"%u.%u.%u.%u",
242+
Address->S_un.S_un_b.s_b1,
243+
Address->S_un.S_un_b.s_b2,
244+
Address->S_un.S_un_b.s_b3,
245+
Address->S_un.S_un_b.s_b4,
246+
WN2H(Port));
247+
ASSERT(Status == STATUS_SUCCESS);
248+
if (!NT_SUCCESS(Status))
249+
return STATUS_INVALID_PARAMETER;
201250

251+
Length = End - AddressString;
202252
if (*AddressStringLength > Length)
203253
{
254+
Status = RtlStringCchCopyW(AddressString,
255+
*AddressStringLength,
256+
Buffer);
257+
ASSERT(Status == STATUS_SUCCESS);
204258
*AddressStringLength = Length + 1;
205-
wcscpy(AddressString, Buffer);
206259
return STATUS_SUCCESS;
207260
}
208261

0 commit comments

Comments
 (0)