7272# include <uriparser/Uri.h>
7373# include "UriCommon.h"
7474# include "UriMemory.h"
75+ # include "UriNormalize.h"
7576# include "UriCopy.h"
7677#endif
7778
@@ -102,14 +103,14 @@ static void URI_FUNC(PreventLeakageAfterCopy)(URI_TYPE(Uri) * uri,
102103
103104int URI_FUNC (CopyUriMm )(URI_TYPE (Uri ) * destUri ,
104105 const URI_TYPE (Uri ) * sourceUri , UriMemoryManager * memory ) {
105- if (sourceUri == NULL ) {
106+ unsigned int doneMask = URI_NORMALIZED ;
107+
108+ if (sourceUri == NULL || destUri == NULL ) {
106109 return URI_ERROR_NULL ;
107110 }
108111
109112 URI_CHECK_MEMORY_MANAGER (memory ); /* may return */
110113
111- unsigned int doneMask = URI_NORMALIZED ;
112-
113114 if (URI_FUNC (CopyRangeAsNeeded )(& destUri -> scheme , & sourceUri -> scheme , URI_FALSE , memory ) == URI_FALSE ) {
114115 return URI_ERROR_MALLOC ;
115116 }
@@ -141,8 +142,6 @@ int URI_FUNC(CopyUriMm)(URI_TYPE(Uri) * destUri,
141142 * (destUri -> hostData .ip4 ) = * (sourceUri -> hostData .ip4 );
142143 }
143144
144- doneMask |= URI_NORMALIZE_HOST ;
145-
146145 if (sourceUri -> hostData .ip6 == NULL ) {
147146 destUri -> hostData .ip6 = NULL ;
148147 } else {
@@ -166,47 +165,44 @@ int URI_FUNC(CopyUriMm)(URI_TYPE(Uri) * destUri,
166165
167166 doneMask |= URI_NORMALIZE_PORT ;
168167
169- if (sourceUri -> pathHead != NULL && sourceUri -> pathTail != NULL ) {
170- URI_TYPE (PathSegment ) * walker ;
171- URI_TYPE (PathSegment ) * walkerNew ;
172-
173- destUri -> pathHead = memory -> malloc (memory , sizeof (URI_TYPE (PathSegment )));
174- if (destUri -> pathHead == NULL ) {
175- URI_FUNC (PreventLeakageAfterCopy )(destUri , doneMask , memory );
176- return URI_ERROR_MALLOC ;
177- }
178-
179- if (URI_FUNC (CopyRangeAsNeeded )(& destUri -> pathHead -> text , & sourceUri -> pathHead -> text , URI_TRUE , memory ) == URI_FALSE ) {
180- URI_FUNC (PreventLeakageAfterCopy )(destUri , doneMask , memory );
181- memory -> free (memory , destUri -> pathHead );
182- return URI_ERROR_MALLOC ;
183- }
184- destUri -> pathHead -> reserved = NULL ;
168+ destUri -> pathHead = NULL ;
169+ destUri -> pathTail = NULL ;
185170
186- doneMask |= URI_NORMALIZE_PATH ;
171+ if (sourceUri -> pathHead != NULL ) {
172+ URI_TYPE (PathSegment ) * sourceWalker = sourceUri -> pathHead ;
173+ URI_TYPE (PathSegment ) * destPrev = NULL ;
187174
188- walker = sourceUri -> pathHead -> next ;
189- walkerNew = destUri -> pathHead ;
190- while (walker != NULL && (walker -> text .first != walker -> text .afterLast || walker -> text .first == URI_FUNC (SafeToPointTo ))) {
191- walkerNew -> next = memory -> malloc (memory , sizeof (URI_TYPE (PathSegment )));
192- if (walkerNew -> next == NULL ) {
175+ while (sourceWalker != NULL ) {
176+ URI_TYPE (PathSegment ) * destWalker = memory -> malloc (memory , sizeof (URI_TYPE (PathSegment )));
177+ if (destWalker == NULL ) {
193178 URI_FUNC (PreventLeakageAfterCopy )(destUri , doneMask , memory );
194179 return URI_ERROR_MALLOC ;
195180 }
196181
197- walkerNew = walkerNew -> next ;
198- if (URI_FUNC (CopyRangeAsNeeded )(& walkerNew -> text , & walker -> text , URI_TRUE , memory ) == URI_FALSE ) {
182+ destWalker -> text .first = NULL ;
183+ destWalker -> text .afterLast = NULL ;
184+ destWalker -> next = NULL ;
185+ destWalker -> reserved = NULL ;
186+
187+ if (destUri -> pathHead == NULL ) {
188+ destUri -> pathHead = destWalker ;
189+ doneMask |= URI_NORMALIZE_PATH ;
190+ }
191+
192+ if (URI_FUNC (CopyRangeAsNeeded )(& destWalker -> text , & sourceWalker -> text , URI_TRUE , memory ) == URI_FALSE ) {
199193 URI_FUNC (PreventLeakageAfterCopy )(destUri , doneMask , memory );
200194 return URI_ERROR_MALLOC ;
201195 }
202- walkerNew -> reserved = NULL ;
203- walker = walker -> next ;
196+
197+ if (destPrev != NULL ) {
198+ destPrev -> next = destWalker ;
199+ }
200+
201+ destPrev = destWalker ;
202+ sourceWalker = sourceWalker -> next ;
203+
204+ destUri -> pathTail = destPrev ;
204205 }
205- walkerNew -> next = NULL ;
206- destUri -> pathTail = walkerNew ;
207- } else {
208- destUri -> pathHead = NULL ;
209- destUri -> pathTail = NULL ;
210206 }
211207
212208 if (URI_FUNC (CopyRangeAsNeeded )(& destUri -> query , & sourceUri -> query , URI_FALSE , memory ) == URI_FALSE ) {
0 commit comments