File tree 3 files changed +14
-22
lines changed
3 files changed +14
-22
lines changed Original file line number Diff line number Diff line change @@ -46,13 +46,17 @@ Datum
46
46
namein (PG_FUNCTION_ARGS )
47
47
{
48
48
char * s = PG_GETARG_CSTRING (0 );
49
- NameData * result ;
49
+ Name result ;
50
50
int len ;
51
51
52
52
len = strlen (s );
53
- len = pg_mbcliplen (s , len , NAMEDATALEN - 1 );
54
53
55
- result = (NameData * ) palloc0 (NAMEDATALEN );
54
+ /* Truncate oversize input */
55
+ if (len >= NAMEDATALEN )
56
+ len = pg_mbcliplen (s , len , NAMEDATALEN - 1 );
57
+
58
+ /* We use palloc0 here to ensure result is zero-padded */
59
+ result = (Name ) palloc0 (NAMEDATALEN );
56
60
memcpy (NameStr (* result ), s , len );
57
61
58
62
PG_RETURN_NAME (result );
Original file line number Diff line number Diff line change @@ -372,9 +372,9 @@ bpchar_name(PG_FUNCTION_ARGS)
372
372
len = VARSIZE_ANY_EXHDR (s );
373
373
s_data = VARDATA_ANY (s );
374
374
375
- /* Truncate to max length for a Name */
375
+ /* Truncate oversize input */
376
376
if (len >= NAMEDATALEN )
377
- len = NAMEDATALEN - 1 ;
377
+ len = pg_mbcliplen ( s_data , len , NAMEDATALEN - 1 ) ;
378
378
379
379
/* Remove trailing blanks */
380
380
while (len > 0 )
@@ -384,16 +384,10 @@ bpchar_name(PG_FUNCTION_ARGS)
384
384
len -- ;
385
385
}
386
386
387
- result = (NameData * ) palloc (NAMEDATALEN );
387
+ /* We use palloc0 here to ensure result is zero-padded */
388
+ result = (Name ) palloc0 (NAMEDATALEN );
388
389
memcpy (NameStr (* result ), s_data , len );
389
390
390
- /* Now null pad to full length... */
391
- while (len < NAMEDATALEN )
392
- {
393
- * (NameStr (* result ) + len ) = '\0' ;
394
- len ++ ;
395
- }
396
-
397
391
PG_RETURN_NAME (result );
398
392
}
399
393
Original file line number Diff line number Diff line change @@ -2256,18 +2256,12 @@ text_name(PG_FUNCTION_ARGS)
2256
2256
2257
2257
/* Truncate oversize input */
2258
2258
if (len >= NAMEDATALEN )
2259
- len = NAMEDATALEN - 1 ;
2259
+ len = pg_mbcliplen ( VARDATA_ANY ( s ), len , NAMEDATALEN - 1 ) ;
2260
2260
2261
- result = (Name ) palloc (NAMEDATALEN );
2261
+ /* We use palloc0 here to ensure result is zero-padded */
2262
+ result = (Name ) palloc0 (NAMEDATALEN );
2262
2263
memcpy (NameStr (* result ), VARDATA_ANY (s ), len );
2263
2264
2264
- /* now null pad to full length... */
2265
- while (len < NAMEDATALEN )
2266
- {
2267
- * (NameStr (* result ) + len ) = '\0' ;
2268
- len ++ ;
2269
- }
2270
-
2271
2265
PG_RETURN_NAME (result );
2272
2266
}
2273
2267
You can’t perform that action at this time.
0 commit comments