Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb9d232b

Browse files
committed
Use "ssize_t" not "long" in max_stack_depth-related code.
This change adapts these functions to the machine's address widthwithout depending on "long" to be the right size. (It isn't onWin64, for example.) While it seems unlikely anyone would careto run with a stack depth limit exceeding 2GB, this is part of ageneral push to avoid using type "long" to represent memory sizes.It's convenient to use ssize_t rather than the perhaps-more-obviouschoice of size_t/Size, because the code involved depends on workingwith a signed data type. Our MAX_KILOBYTES limit already ensuresthat ssize_t will be sufficient to represent the maximum value ofmax_stack_depth.Extracted from a larger patch by Vladlen, plus additional hackeryby me.Author: Vladlen Popolitov <v.popolitov@postgrespro.ru>Author: Tom Lane <tgl@sss.pgh.pa.us>Discussion:https://postgr.es/m/1a01f0-66ec2d80-3b-68487680@27595217
1 parentb9aa416 commitb9d232b

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

‎src/backend/utils/misc/guc.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,7 +1589,7 @@ static void
15891589
InitializeGUCOptionsFromEnvironment(void)
15901590
{
15911591
char*env;
1592-
longstack_rlimit;
1592+
ssize_tstack_rlimit;
15931593

15941594
env=getenv("PGPORT");
15951595
if (env!=NULL)
@@ -1613,7 +1613,7 @@ InitializeGUCOptionsFromEnvironment(void)
16131613
stack_rlimit=get_stack_depth_rlimit();
16141614
if (stack_rlimit>0)
16151615
{
1616-
longnew_limit= (stack_rlimit-STACK_DEPTH_SLOP) /1024L;
1616+
ssize_tnew_limit= (stack_rlimit-STACK_DEPTH_SLOP) /1024;
16171617

16181618
if (new_limit>100)
16191619
{
@@ -1627,7 +1627,7 @@ InitializeGUCOptionsFromEnvironment(void)
16271627
new_limit=2048;
16281628
source=PGC_S_DYNAMIC_DEFAULT;
16291629
}
1630-
snprintf(limbuf,sizeof(limbuf),"%ld",new_limit);
1630+
snprintf(limbuf,sizeof(limbuf),"%d", (int)new_limit);
16311631
SetConfigOption("max_stack_depth",limbuf,
16321632
PGC_POSTMASTER,source);
16331633
}

‎src/backend/utils/misc/stack_depth.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
intmax_stack_depth=100;
2727

2828
/* max_stack_depth converted to bytes for speed of checking */
29-
staticlongmax_stack_depth_bytes=100*1024L;
29+
staticssize_tmax_stack_depth_bytes=100*(ssize_t)1024;
3030

3131
/*
3232
* Stack base pointer -- initialized by set_stack_base(), which
@@ -109,12 +109,12 @@ bool
109109
stack_is_too_deep(void)
110110
{
111111
charstack_top_loc;
112-
longstack_depth;
112+
ssize_tstack_depth;
113113

114114
/*
115115
* Compute distance from reference point to my local variables
116116
*/
117-
stack_depth= (long) (stack_base_ptr-&stack_top_loc);
117+
stack_depth= (ssize_t) (stack_base_ptr-&stack_top_loc);
118118

119119
/*
120120
* Take abs value, since stacks grow up on some machines, down on others
@@ -141,13 +141,13 @@ stack_is_too_deep(void)
141141
bool
142142
check_max_stack_depth(int*newval,void**extra,GucSourcesource)
143143
{
144-
longnewval_bytes=*newval*1024L;
145-
longstack_rlimit=get_stack_depth_rlimit();
144+
ssize_tnewval_bytes=*newval*(ssize_t)1024;
145+
ssize_tstack_rlimit=get_stack_depth_rlimit();
146146

147147
if (stack_rlimit>0&&newval_bytes>stack_rlimit-STACK_DEPTH_SLOP)
148148
{
149-
GUC_check_errdetail("\"max_stack_depth\" must not exceed %ldkB.",
150-
(stack_rlimit-STACK_DEPTH_SLOP) /1024L);
149+
GUC_check_errdetail("\"max_stack_depth\" must not exceed %zdkB.",
150+
(stack_rlimit-STACK_DEPTH_SLOP) /1024);
151151
GUC_check_errhint("Increase the platform's stack depth limit via \"ulimit -s\" or local equivalent.");
152152
return false;
153153
}
@@ -158,7 +158,7 @@ check_max_stack_depth(int *newval, void **extra, GucSource source)
158158
void
159159
assign_max_stack_depth(intnewval,void*extra)
160160
{
161-
longnewval_bytes=newval*1024L;
161+
ssize_tnewval_bytes=newval*(ssize_t)1024;
162162

163163
max_stack_depth_bytes=newval_bytes;
164164
}
@@ -167,12 +167,16 @@ assign_max_stack_depth(int newval, void *extra)
167167
* Obtain platform stack depth limit (in bytes)
168168
*
169169
* Return -1 if unknown
170+
*
171+
* Note: we choose to use ssize_t not size_t as the result type because
172+
* callers compute values that could theoretically go negative,
173+
* such as "result - STACK_DEPTH_SLOP".
170174
*/
171-
long
175+
ssize_t
172176
get_stack_depth_rlimit(void)
173177
{
174178
#if defined(HAVE_GETRLIMIT)
175-
staticlongval=0;
179+
staticssize_tval=0;
176180

177181
/* This won't change after process launch, so check just once */
178182
if (val==0)
@@ -182,10 +186,10 @@ get_stack_depth_rlimit(void)
182186
if (getrlimit(RLIMIT_STACK,&rlim)<0)
183187
val=-1;
184188
elseif (rlim.rlim_cur==RLIM_INFINITY)
185-
val=LONG_MAX;
189+
val=SSIZE_MAX;
186190
/* rlim_cur is probably of an unsigned type, so check for overflow */
187-
elseif (rlim.rlim_cur >=LONG_MAX)
188-
val=LONG_MAX;
191+
elseif (rlim.rlim_cur >=SSIZE_MAX)
192+
val=SSIZE_MAX;
189193
else
190194
val=rlim.rlim_cur;
191195
}

‎src/include/miscadmin.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,15 +293,15 @@ extern PGDLLIMPORT bool VacuumCostActive;
293293
externPGDLLIMPORTintmax_stack_depth;
294294

295295
/* Required daylight between max_stack_depth and the kernel limit, in bytes */
296-
#defineSTACK_DEPTH_SLOP (512 *1024L)
296+
#defineSTACK_DEPTH_SLOP (512 *1024)
297297

298298
typedefchar*pg_stack_base_t;
299299

300300
externpg_stack_base_tset_stack_base(void);
301301
externvoidrestore_stack_base(pg_stack_base_tbase);
302302
externvoidcheck_stack_depth(void);
303303
externboolstack_is_too_deep(void);
304-
externlongget_stack_depth_rlimit(void);
304+
externssize_tget_stack_depth_rlimit(void);
305305

306306
/* in tcop/utility.c */
307307
externvoidPreventCommandIfReadOnly(constchar*cmdname);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp