@@ -123,7 +123,6 @@ int ossl_punycode_decode(const char *pEncoded, const size_t enc_len,
123
123
unsigned int bias = initial_bias ;
124
124
size_t processed_in = 0 , written_out = 0 ;
125
125
unsigned int max_out = * pout_length ;
126
-
127
126
unsigned int basic_count = 0 ;
128
127
unsigned int loop ;
129
128
@@ -185,7 +184,7 @@ int ossl_punycode_decode(const char *pEncoded, const size_t enc_len,
185
184
return 0 ;
186
185
187
186
memmove (pDecoded + i + 1 , pDecoded + i ,
188
- (written_out - i ) * sizeof * pDecoded );
187
+ (written_out - i ) * sizeof ( * pDecoded ) );
189
188
pDecoded [i ] = n ;
190
189
i ++ ;
191
190
written_out ++ ;
@@ -255,65 +254,61 @@ int ossl_a2ulabel(const char *in, char *out, size_t *outlen)
255
254
*/
256
255
char * outptr = out ;
257
256
const char * inptr = in ;
258
- size_t size = 0 ;
257
+ size_t size = 0 , maxsize ;
259
258
int result = 1 ;
260
-
259
+ unsigned int i , j ;
261
260
unsigned int buf [LABEL_BUF_SIZE ]; /* It's a hostname */
262
- if (out == NULL )
261
+
262
+ if (out == NULL ) {
263
263
result = 0 ;
264
+ maxsize = 0 ;
265
+ } else {
266
+ maxsize = * outlen ;
267
+ }
268
+
269
+ #define PUSHC (c ) \
270
+ do \
271
+ if (size++ < maxsize) \
272
+ *outptr++ = c; \
273
+ else \
274
+ result = 0; \
275
+ while (0)
264
276
265
277
while (1 ) {
266
278
char * tmpptr = strchr (inptr , '.' );
267
- size_t delta = ( tmpptr ) ? (size_t )(tmpptr - inptr ) : strlen (inptr );
279
+ size_t delta = tmpptr != NULL ? (size_t )(tmpptr - inptr ) : strlen (inptr );
268
280
269
281
if (strncmp (inptr , "xn--" , 4 ) != 0 ) {
270
- size += delta + 1 ;
271
-
272
- if (size >= * outlen - 1 )
273
- result = 0 ;
274
-
275
- if (result > 0 ) {
276
- memcpy (outptr , inptr , delta + 1 );
277
- outptr += delta + 1 ;
278
- }
282
+ for (i = 0 ; i < delta + 1 ; i ++ )
283
+ PUSHC (inptr [i ]);
279
284
} else {
280
285
unsigned int bufsize = LABEL_BUF_SIZE ;
281
- unsigned int i ;
282
286
283
287
if (ossl_punycode_decode (inptr + 4 , delta - 4 , buf , & bufsize ) <= 0 )
284
288
return -1 ;
285
289
286
290
for (i = 0 ; i < bufsize ; i ++ ) {
287
291
unsigned char seed [6 ];
288
292
size_t utfsize = codepoint2utf8 (seed , buf [i ]);
293
+
289
294
if (utfsize == 0 )
290
295
return -1 ;
291
296
292
- size += utfsize ;
293
- if (size >= * outlen - 1 )
294
- result = 0 ;
295
-
296
- if (result > 0 ) {
297
- memcpy (outptr , seed , utfsize );
298
- outptr += utfsize ;
299
- }
297
+ for (j = 0 ; j < utfsize ; j ++ )
298
+ PUSHC (seed [j ]);
300
299
}
301
300
302
- if (tmpptr != NULL ) {
303
- * outptr = '.' ;
304
- outptr ++ ;
305
- size ++ ;
306
- if (size >= * outlen - 1 )
307
- result = 0 ;
308
- }
301
+ PUSHC (tmpptr != NULL ? '.' : '\0' );
309
302
}
310
303
311
304
if (tmpptr == NULL )
312
305
break ;
313
306
314
307
inptr = tmpptr + 1 ;
315
308
}
309
+ #undef PUSHC
316
310
311
+ * outlen = size ;
317
312
return result ;
318
313
}
319
314
@@ -327,12 +322,11 @@ int ossl_a2ulabel(const char *in, char *out, size_t *outlen)
327
322
328
323
int ossl_a2ucompare (const char * a , const char * u )
329
324
{
330
- char a_ulabel [LABEL_BUF_SIZE ];
325
+ char a_ulabel [LABEL_BUF_SIZE + 1 ];
331
326
size_t a_size = sizeof (a_ulabel );
332
327
333
- if (ossl_a2ulabel (a , a_ulabel , & a_size ) <= 0 ) {
328
+ if (ossl_a2ulabel (a , a_ulabel , & a_size ) <= 0 )
334
329
return -1 ;
335
- }
336
330
337
- return ( strcmp (a_ulabel , u ) == 0 ) ? 0 : 1 ;
331
+ return strcmp (a_ulabel , u ) != 0 ;
338
332
}
0 commit comments