@@ -781,13 +781,21 @@ MemoryContextAlloc(MemoryContext context, Size size)
781
781
context -> isReset = false;
782
782
783
783
ret = context -> methods -> alloc (context , size );
784
- if (ret == NULL )
784
+ if (unlikely ( ret == NULL ) )
785
785
{
786
786
MemoryContextStats (TopMemoryContext );
787
+
788
+ /*
789
+ * Here, and elsewhere in this module, we show the target context's
790
+ * "name" but not its "ident" (if any) in user-visible error messages.
791
+ * The "ident" string might contain security-sensitive data, such as
792
+ * values in SQL commands.
793
+ */
787
794
ereport (ERROR ,
788
795
(errcode (ERRCODE_OUT_OF_MEMORY ),
789
796
errmsg ("out of memory" ),
790
- errdetail ("Failed on request of size %zu." , size )));
797
+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
798
+ size , context -> name )));
791
799
}
792
800
793
801
VALGRIND_MEMPOOL_ALLOC (context , ret , size );
@@ -816,13 +824,14 @@ MemoryContextAllocZero(MemoryContext context, Size size)
816
824
context -> isReset = false;
817
825
818
826
ret = context -> methods -> alloc (context , size );
819
- if (ret == NULL )
827
+ if (unlikely ( ret == NULL ) )
820
828
{
821
829
MemoryContextStats (TopMemoryContext );
822
830
ereport (ERROR ,
823
831
(errcode (ERRCODE_OUT_OF_MEMORY ),
824
832
errmsg ("out of memory" ),
825
- errdetail ("Failed on request of size %zu." , size )));
833
+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
834
+ size , context -> name )));
826
835
}
827
836
828
837
VALGRIND_MEMPOOL_ALLOC (context , ret , size );
@@ -853,13 +862,14 @@ MemoryContextAllocZeroAligned(MemoryContext context, Size size)
853
862
context -> isReset = false;
854
863
855
864
ret = context -> methods -> alloc (context , size );
856
- if (ret == NULL )
865
+ if (unlikely ( ret == NULL ) )
857
866
{
858
867
MemoryContextStats (TopMemoryContext );
859
868
ereport (ERROR ,
860
869
(errcode (ERRCODE_OUT_OF_MEMORY ),
861
870
errmsg ("out of memory" ),
862
- errdetail ("Failed on request of size %zu." , size )));
871
+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
872
+ size , context -> name )));
863
873
}
864
874
865
875
VALGRIND_MEMPOOL_ALLOC (context , ret , size );
@@ -888,15 +898,16 @@ MemoryContextAllocExtended(MemoryContext context, Size size, int flags)
888
898
context -> isReset = false;
889
899
890
900
ret = context -> methods -> alloc (context , size );
891
- if (ret == NULL )
901
+ if (unlikely ( ret == NULL ) )
892
902
{
893
903
if ((flags & MCXT_ALLOC_NO_OOM ) == 0 )
894
904
{
895
905
MemoryContextStats (TopMemoryContext );
896
906
ereport (ERROR ,
897
907
(errcode (ERRCODE_OUT_OF_MEMORY ),
898
908
errmsg ("out of memory" ),
899
- errdetail ("Failed on request of size %zu." , size )));
909
+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
910
+ size , context -> name )));
900
911
}
901
912
return NULL ;
902
913
}
@@ -914,26 +925,28 @@ palloc(Size size)
914
925
{
915
926
/* duplicates MemoryContextAlloc to avoid increased overhead */
916
927
void * ret ;
928
+ MemoryContext context = CurrentMemoryContext ;
917
929
918
- AssertArg (MemoryContextIsValid (CurrentMemoryContext ));
919
- AssertNotInCriticalSection (CurrentMemoryContext );
930
+ AssertArg (MemoryContextIsValid (context ));
931
+ AssertNotInCriticalSection (context );
920
932
921
933
if (!AllocSizeIsValid (size ))
922
934
elog (ERROR , "invalid memory alloc request size %zu" , size );
923
935
924
- CurrentMemoryContext -> isReset = false;
936
+ context -> isReset = false;
925
937
926
- ret = CurrentMemoryContext -> methods -> alloc (CurrentMemoryContext , size );
927
- if (ret == NULL )
938
+ ret = context -> methods -> alloc (context , size );
939
+ if (unlikely ( ret == NULL ) )
928
940
{
929
941
MemoryContextStats (TopMemoryContext );
930
942
ereport (ERROR ,
931
943
(errcode (ERRCODE_OUT_OF_MEMORY ),
932
944
errmsg ("out of memory" ),
933
- errdetail ("Failed on request of size %zu." , size )));
945
+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
946
+ size , context -> name )));
934
947
}
935
948
936
- VALGRIND_MEMPOOL_ALLOC (CurrentMemoryContext , ret , size );
949
+ VALGRIND_MEMPOOL_ALLOC (context , ret , size );
937
950
938
951
return ret ;
939
952
}
@@ -943,26 +956,28 @@ palloc0(Size size)
943
956
{
944
957
/* duplicates MemoryContextAllocZero to avoid increased overhead */
945
958
void * ret ;
959
+ MemoryContext context = CurrentMemoryContext ;
946
960
947
- AssertArg (MemoryContextIsValid (CurrentMemoryContext ));
948
- AssertNotInCriticalSection (CurrentMemoryContext );
961
+ AssertArg (MemoryContextIsValid (context ));
962
+ AssertNotInCriticalSection (context );
949
963
950
964
if (!AllocSizeIsValid (size ))
951
965
elog (ERROR , "invalid memory alloc request size %zu" , size );
952
966
953
- CurrentMemoryContext -> isReset = false;
967
+ context -> isReset = false;
954
968
955
- ret = CurrentMemoryContext -> methods -> alloc (CurrentMemoryContext , size );
956
- if (ret == NULL )
969
+ ret = context -> methods -> alloc (context , size );
970
+ if (unlikely ( ret == NULL ) )
957
971
{
958
972
MemoryContextStats (TopMemoryContext );
959
973
ereport (ERROR ,
960
974
(errcode (ERRCODE_OUT_OF_MEMORY ),
961
975
errmsg ("out of memory" ),
962
- errdetail ("Failed on request of size %zu." , size )));
976
+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
977
+ size , context -> name )));
963
978
}
964
979
965
- VALGRIND_MEMPOOL_ALLOC (CurrentMemoryContext , ret , size );
980
+ VALGRIND_MEMPOOL_ALLOC (context , ret , size );
966
981
967
982
MemSetAligned (ret , 0 , size );
968
983
@@ -974,31 +989,33 @@ palloc_extended(Size size, int flags)
974
989
{
975
990
/* duplicates MemoryContextAllocExtended to avoid increased overhead */
976
991
void * ret ;
992
+ MemoryContext context = CurrentMemoryContext ;
977
993
978
- AssertArg (MemoryContextIsValid (CurrentMemoryContext ));
979
- AssertNotInCriticalSection (CurrentMemoryContext );
994
+ AssertArg (MemoryContextIsValid (context ));
995
+ AssertNotInCriticalSection (context );
980
996
981
997
if (((flags & MCXT_ALLOC_HUGE ) != 0 && !AllocHugeSizeIsValid (size )) ||
982
998
((flags & MCXT_ALLOC_HUGE ) == 0 && !AllocSizeIsValid (size )))
983
999
elog (ERROR , "invalid memory alloc request size %zu" , size );
984
1000
985
- CurrentMemoryContext -> isReset = false;
1001
+ context -> isReset = false;
986
1002
987
- ret = CurrentMemoryContext -> methods -> alloc (CurrentMemoryContext , size );
988
- if (ret == NULL )
1003
+ ret = context -> methods -> alloc (context , size );
1004
+ if (unlikely ( ret == NULL ) )
989
1005
{
990
1006
if ((flags & MCXT_ALLOC_NO_OOM ) == 0 )
991
1007
{
992
1008
MemoryContextStats (TopMemoryContext );
993
1009
ereport (ERROR ,
994
1010
(errcode (ERRCODE_OUT_OF_MEMORY ),
995
1011
errmsg ("out of memory" ),
996
- errdetail ("Failed on request of size %zu." , size )));
1012
+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
1013
+ size , context -> name )));
997
1014
}
998
1015
return NULL ;
999
1016
}
1000
1017
1001
- VALGRIND_MEMPOOL_ALLOC (CurrentMemoryContext , ret , size );
1018
+ VALGRIND_MEMPOOL_ALLOC (context , ret , size );
1002
1019
1003
1020
if ((flags & MCXT_ALLOC_ZERO ) != 0 )
1004
1021
MemSetAligned (ret , 0 , size );
@@ -1038,13 +1055,14 @@ repalloc(void *pointer, Size size)
1038
1055
Assert (!context -> isReset );
1039
1056
1040
1057
ret = context -> methods -> realloc (context , pointer , size );
1041
- if (ret == NULL )
1058
+ if (unlikely ( ret == NULL ) )
1042
1059
{
1043
1060
MemoryContextStats (TopMemoryContext );
1044
1061
ereport (ERROR ,
1045
1062
(errcode (ERRCODE_OUT_OF_MEMORY ),
1046
1063
errmsg ("out of memory" ),
1047
- errdetail ("Failed on request of size %zu." , size )));
1064
+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
1065
+ size , context -> name )));
1048
1066
}
1049
1067
1050
1068
VALGRIND_MEMPOOL_CHANGE (context , pointer , ret , size );
@@ -1072,13 +1090,14 @@ MemoryContextAllocHuge(MemoryContext context, Size size)
1072
1090
context -> isReset = false;
1073
1091
1074
1092
ret = context -> methods -> alloc (context , size );
1075
- if (ret == NULL )
1093
+ if (unlikely ( ret == NULL ) )
1076
1094
{
1077
1095
MemoryContextStats (TopMemoryContext );
1078
1096
ereport (ERROR ,
1079
1097
(errcode (ERRCODE_OUT_OF_MEMORY ),
1080
1098
errmsg ("out of memory" ),
1081
- errdetail ("Failed on request of size %zu." , size )));
1099
+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
1100
+ size , context -> name )));
1082
1101
}
1083
1102
1084
1103
VALGRIND_MEMPOOL_ALLOC (context , ret , size );
@@ -1106,13 +1125,14 @@ repalloc_huge(void *pointer, Size size)
1106
1125
Assert (!context -> isReset );
1107
1126
1108
1127
ret = context -> methods -> realloc (context , pointer , size );
1109
- if (ret == NULL )
1128
+ if (unlikely ( ret == NULL ) )
1110
1129
{
1111
1130
MemoryContextStats (TopMemoryContext );
1112
1131
ereport (ERROR ,
1113
1132
(errcode (ERRCODE_OUT_OF_MEMORY ),
1114
1133
errmsg ("out of memory" ),
1115
- errdetail ("Failed on request of size %zu." , size )));
1134
+ errdetail ("Failed on request of size %zu in memory context \"%s\"." ,
1135
+ size , context -> name )));
1116
1136
}
1117
1137
1118
1138
VALGRIND_MEMPOOL_CHANGE (context , pointer , ret , size );
0 commit comments