File tree Expand file tree Collapse file tree 3 files changed +14
-22
lines changed Expand file tree Collapse file tree 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 @@ -371,9 +371,9 @@ bpchar_name(PG_FUNCTION_ARGS)
371
371
len = VARSIZE_ANY_EXHDR (s );
372
372
s_data = VARDATA_ANY (s );
373
373
374
- /* Truncate to max length for a Name */
374
+ /* Truncate oversize input */
375
375
if (len >= NAMEDATALEN )
376
- len = NAMEDATALEN - 1 ;
376
+ len = pg_mbcliplen ( s_data , len , NAMEDATALEN - 1 ) ;
377
377
378
378
/* Remove trailing blanks */
379
379
while (len > 0 )
@@ -383,16 +383,10 @@ bpchar_name(PG_FUNCTION_ARGS)
383
383
len -- ;
384
384
}
385
385
386
- result = (NameData * ) palloc (NAMEDATALEN );
386
+ /* We use palloc0 here to ensure result is zero-padded */
387
+ result = (Name ) palloc0 (NAMEDATALEN );
387
388
memcpy (NameStr (* result ), s_data , len );
388
389
389
- /* Now null pad to full length... */
390
- while (len < NAMEDATALEN )
391
- {
392
- * (NameStr (* result ) + len ) = '\0' ;
393
- len ++ ;
394
- }
395
-
396
390
PG_RETURN_NAME (result );
397
391
}
398
392
Original file line number Diff line number Diff line change @@ -2145,18 +2145,12 @@ text_name(PG_FUNCTION_ARGS)
2145
2145
2146
2146
/* Truncate oversize input */
2147
2147
if (len >= NAMEDATALEN )
2148
- len = NAMEDATALEN - 1 ;
2148
+ len = pg_mbcliplen ( VARDATA_ANY ( s ), len , NAMEDATALEN - 1 ) ;
2149
2149
2150
- result = (Name ) palloc (NAMEDATALEN );
2150
+ /* We use palloc0 here to ensure result is zero-padded */
2151
+ result = (Name ) palloc0 (NAMEDATALEN );
2151
2152
memcpy (NameStr (* result ), VARDATA_ANY (s ), len );
2152
2153
2153
- /* now null pad to full length... */
2154
- while (len < NAMEDATALEN )
2155
- {
2156
- * (NameStr (* result ) + len ) = '\0' ;
2157
- len ++ ;
2158
- }
2159
-
2160
2154
PG_RETURN_NAME (result );
2161
2155
}
2162
2156
You can’t perform that action at this time.
0 commit comments