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

Commit610d33c

Browse files
author
Neil Conway
committed
This patch makes some of the memory manipulation performed by psql a
little more sane. Some parts of the code was using a static functionxmalloc() that did safe memory allocation (where "safe" means "bailout on OOM"), but most of it was just invoking calloc() or malloc()directly. Now almost everything invokes xmalloc() or xcalloc().
1 parentcb3dc82 commit610d33c

File tree

13 files changed

+125
-238
lines changed

13 files changed

+125
-238
lines changed

‎src/bin/psql/command.c

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.109 2004/01/09 21:12:20 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.110 2004/01/24 19:38:49 neilc Exp $
77
*/
88
#include"postgres_fe.h"
99
#include"command.h"
@@ -1156,13 +1156,7 @@ scan_option(char **string, enum option_type type, char *quote, bool semicolon)
11561156
/* Copy the option */
11571157
token_len=cp-&options_string[pos];
11581158

1159-
return_val=malloc(token_len+1);
1160-
if (!return_val)
1161-
{
1162-
psql_error("out of memory\n");
1163-
exit(EXIT_FAILURE);
1164-
}
1165-
1159+
return_val=xmalloc(token_len+1);
11661160
memcpy(return_val,&options_string[pos],token_len);
11671161
return_val[token_len]='\0';
11681162

@@ -1235,7 +1229,7 @@ scan_option(char **string, enum option_type type, char *quote, bool semicolon)
12351229
*
12361230
* Replaces \n, \t, and the like.
12371231
*
1238-
* The return value is malloc()'ed.
1232+
* The return value is malloc'ed.
12391233
*/
12401234
staticchar*
12411235
unescape(constunsignedchar*source,size_tlen)
@@ -1251,12 +1245,7 @@ unescape(const unsigned char *source, size_t len)
12511245

12521246
length=Min(len,strlen(source))+1;
12531247

1254-
tmp=destination=malloc(length);
1255-
if (!tmp)
1256-
{
1257-
psql_error("out of memory\n");
1258-
exit(EXIT_FAILURE);
1259-
}
1248+
tmp=destination=xmalloc(length);
12601249

12611250
for (p=source;p-source< (int)len&&*p;p+=PQmblen(p,pset.encoding))
12621251
{
@@ -1537,9 +1526,7 @@ editFile(const char *fname)
15371526
if (!editorName)
15381527
editorName=DEFAULT_EDITOR;
15391528

1540-
sys=malloc(strlen(editorName)+strlen(fname)+10+1);
1541-
if (!sys)
1542-
return false;
1529+
sys=xmalloc(strlen(editorName)+strlen(fname)+10+1);
15431530
sprintf(sys,
15441531
#ifndefWIN32
15451532
"exec "
@@ -1959,15 +1946,7 @@ do_shell(const char *command)
19591946
if (shellName==NULL)
19601947
shellName=DEFAULT_SHELL;
19611948

1962-
sys=malloc(strlen(shellName)+16);
1963-
if (!sys)
1964-
{
1965-
psql_error("out of memory\n");
1966-
if (pset.cur_cmd_interactive)
1967-
return false;
1968-
else
1969-
exit(EXIT_FAILURE);
1970-
}
1949+
sys=xmalloc(strlen(shellName)+16);
19711950
sprintf(sys,
19721951
#ifndefWIN32
19731952
"exec "

‎src/bin/psql/common.c

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.80 2004/01/20 23:48:56 tgl Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.81 2004/01/24 19:38:49 neilc Exp $
77
*/
88
#include"postgres_fe.h"
99
#include"common.h"
@@ -89,6 +89,43 @@ xstrdup(const char *string)
8989
returntmp;
9090
}
9191

92+
void*
93+
xmalloc(size_tsize)
94+
{
95+
void*tmp;
96+
97+
tmp=malloc(size);
98+
if (!tmp)
99+
{
100+
psql_error("out of memory\n");
101+
exit(EXIT_FAILURE);
102+
}
103+
returntmp;
104+
}
105+
106+
void*
107+
xmalloc_zero(size_tsize)
108+
{
109+
void*tmp;
110+
111+
tmp=xmalloc(size);
112+
memset(tmp,0,size);
113+
returntmp;
114+
}
115+
116+
void*
117+
xcalloc(size_tnmemb,size_tsize)
118+
{
119+
void*tmp;
120+
121+
tmp=calloc(nmemb,size);
122+
if (!tmp)
123+
{
124+
psql_error("out of memory");
125+
exit(EXIT_FAILURE);
126+
}
127+
returntmp;
128+
}
92129

93130

94131
/*
@@ -854,12 +891,7 @@ expand_tilde(char **filename)
854891
{
855892
char*newfn;
856893

857-
newfn=malloc(strlen(home)+strlen(p)+1);
858-
if (!newfn)
859-
{
860-
psql_error("out of memory\n");
861-
exit(EXIT_FAILURE);
862-
}
894+
newfn=xmalloc(strlen(home)+strlen(p)+1);
863895
strcpy(newfn,home);
864896
strcat(newfn,p);
865897

‎src/bin/psql/common.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/common.h,v 1.32 2004/01/09 21:12:20 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/common.h,v 1.33 2004/01/24 19:38:49 neilc Exp $
77
*/
88
#ifndefCOMMON_H
99
#defineCOMMON_H
@@ -20,7 +20,15 @@
2020
#definepsql_assert(p)
2121
#endif
2222

23+
/*
24+
* Safer versions of some standard C library functions. If an
25+
* out-of-memory condition occurs, these functions will bail out
26+
* safely; therefore, their return value is guaranteed to be non-NULL.
27+
*/
2328
externchar*xstrdup(constchar*string);
29+
externvoid*xmalloc(size_tsize);
30+
externvoid*xmalloc_zero(size_tsize);
31+
externvoid*xcalloc(size_tnmemb,size_tsize);
2432

2533
externboolsetQFout(constchar*fname);
2634

‎src/bin/psql/copy.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.37 2004/01/20 23:48:56 tgl Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.38 2004/01/24 19:38:49 neilc Exp $
77
*/
88
#include"postgres_fe.h"
99
#include"copy.h"
@@ -83,12 +83,7 @@ xstrcat(char **var, const char *more)
8383
{
8484
char*newvar;
8585

86-
newvar= (char*)malloc(strlen(*var)+strlen(more)+1);
87-
if (!newvar)
88-
{
89-
psql_error("out of memory\n");
90-
exit(EXIT_FAILURE);
91-
}
86+
newvar=xmalloc(strlen(*var)+strlen(more)+1);
9287
strcpy(newvar,*var);
9388
strcat(newvar,more);
9489
free(*var);
@@ -112,11 +107,7 @@ parse_slash_copy(const char *args)
112107
returnNULL;
113108
}
114109

115-
if (!(result=calloc(1,sizeof(structcopy_options))))
116-
{
117-
psql_error("out of memory\n");
118-
exit(EXIT_FAILURE);
119-
}
110+
result=xcalloc(1,sizeof(structcopy_options));
120111

121112
token=strtokx(line,whitespace,".,()","\"",
122113
0, false,pset.encoding);

‎src/bin/psql/describe.c

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.92 2004/01/11 19:10:49dennis Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.93 2004/01/24 19:38:49neilc Exp $
77
*/
88
#include"postgres_fe.h"
99
#include"describe.h"
@@ -39,21 +39,6 @@ static void processNamePattern(PQExpBuffer buf, const char *pattern,
3939
constchar*schemavar,constchar*namevar,
4040
constchar*altnamevar,constchar*visibilityrule);
4141

42-
43-
staticvoid*
44-
xmalloc(size_tsize)
45-
{
46-
void*tmp;
47-
48-
tmp=malloc(size);
49-
if (!tmp)
50-
{
51-
psql_error("out of memory\n");
52-
exit(EXIT_FAILURE);
53-
}
54-
returntmp;
55-
}
56-
5742
staticvoid*
5843
xmalloczero(size_tsize)
5944
{

‎src/bin/psql/input.c

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/input.c,v 1.32 2003/11/29 19:52:06 pgsql Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/input.c,v 1.33 2004/01/24 19:38:49 neilc Exp $
77
*/
88
#include"postgres_fe.h"
99
#include"input.h"
@@ -83,7 +83,7 @@ gets_basic(const char prompt[])
8383
* gets_interactive()
8484
*
8585
* Gets a line of interactive input, using readline of desired.
86-
* The result ismalloced.
86+
* The result ismalloc'ed.
8787
*/
8888
char*
8989
gets_interactive(constchar*prompt)
@@ -113,7 +113,7 @@ gets_interactive(const char *prompt)
113113
else
114114
{
115115
free(prev_hist);
116-
prev_hist=strdup(s);
116+
prev_hist=xstrdup(s);
117117
add_history(s);
118118
}
119119
}
@@ -183,15 +183,13 @@ initializeInput(int flags)
183183
home=getenv("HOME");
184184
if (home)
185185
{
186-
char*psql_history= (char*)malloc(strlen(home)+1+
187-
strlen(PSQLHISTORY)+1);
188-
189-
if (psql_history)
190-
{
191-
sprintf(psql_history,"%s/%s",home,PSQLHISTORY);
192-
read_history(psql_history);
193-
free(psql_history);
194-
}
186+
char*psql_history;
187+
188+
psql_history=xmalloc(strlen(home)+1+
189+
strlen(PSQLHISTORY)+1);
190+
sprintf(psql_history,"%s/%s",home,PSQLHISTORY);
191+
read_history(psql_history);
192+
free(psql_history);
195193
}
196194
}
197195
#endif
@@ -234,26 +232,24 @@ finishInput(int exitstatus, void *arg)
234232
if (useHistory)
235233
{
236234
char*home;
237-
char*psql_history;
238235

239236
home=getenv("HOME");
240237
if (home)
241238
{
242-
psql_history= (char*)malloc(strlen(home)+1+
243-
strlen(PSQLHISTORY)+1);
244-
if (psql_history)
245-
{
246-
inthist_size;
239+
char*psql_history;
240+
inthist_size;
241+
242+
psql_history=xmalloc(strlen(home)+1+
243+
strlen(PSQLHISTORY)+1);
247244

248-
hist_size=GetVariableNum(pset.vars,"HISTSIZE",-1,-1, true);
245+
hist_size=GetVariableNum(pset.vars,"HISTSIZE",-1,-1, true);
249246

250-
if (hist_size >=0)
251-
stifle_history(hist_size);
247+
if (hist_size >=0)
248+
stifle_history(hist_size);
252249

253-
sprintf(psql_history,"%s/%s",home,PSQLHISTORY);
254-
write_history(psql_history);
255-
free(psql_history);
256-
}
250+
sprintf(psql_history,"%s/%s",home,PSQLHISTORY);
251+
write_history(psql_history);
252+
free(psql_history);
257253
}
258254
}
259255
#endif

‎src/bin/psql/mainloop.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/mainloop.c,v 1.59 2004/01/21 22:05:44 tgl Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/mainloop.c,v 1.60 2004/01/24 19:38:49 neilc Exp $
77
*/
88
#include"postgres_fe.h"
99
#include"mainloop.h"
@@ -332,13 +332,7 @@ MainLoop(FILE *source)
332332
/* It is a variable, perform substitution */
333333
out_length=strlen(value);
334334

335-
new=malloc(len+out_length-in_length+1);
336-
if (!new)
337-
{
338-
psql_error("out of memory\n");
339-
exit(EXIT_FAILURE);
340-
}
341-
335+
new=xmalloc(len+out_length-in_length+1);
342336
sprintf(new,"%.*s%s%s",i,line,value,
343337
&line[i+thislen+in_length]);
344338

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp