Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 244ee0c

Browse files
committed
Remove pstrdup() call from exec.c because DLLIMPORT flag on
CurrentMemoryContext caused compile problems. Recode to not make a copy of the PATH but copy parts out into MAXPGPATH variables.
1 parent fcbc95b commit 244ee0c

File tree

1 file changed

+25
-34
lines changed

1 file changed

+25
-34
lines changed

src/port/exec.c

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/port/exec.c,v 1.14 2004/05/24 20:23:50 momjian Exp $
10+
* $PostgreSQL: pgsql/src/port/exec.c,v 1.15 2004/05/24 22:35:37 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -28,12 +28,6 @@
2828

2929
#define _(x) gettext(x)
3030

31-
#ifdef FRONTEND
32-
#undef pstrdup
33-
#define pstrdup(p) strdup(p)
34-
#define pfree(p) free(p)
35-
#endif
36-
3731
/* $PATH (or %PATH%) path separator */
3832
#ifdef WIN32
3933
#define PATHSEP ';'
@@ -185,11 +179,8 @@ validate_exec(const char *path)
185179
int
186180
find_my_exec(const char *argv0, char *retpath)
187181
{
188-
char cwd[MAXPGPATH];
189-
char *p;
190-
char *path,
191-
*startp,
192-
*endp;
182+
char cwd[MAXPGPATH], test_path[MAXPGPATH];
183+
char *path;
193184

194185
if (!getcwd(cwd, MAXPGPATH))
195186
cwd[0] = '\0';
@@ -205,9 +196,9 @@ find_my_exec(const char *argv0, char *retpath)
205196
* it).
206197
*/
207198
/* Does argv0 have a separator? */
208-
if ((p = last_path_separator(argv0)))
199+
if ((path = last_path_separator(argv0)))
209200
{
210-
if (*++p == '\0')
201+
if (*++path == '\0')
211202
{
212203
log_error("argv[0] ends with a path separator \"%s\"", argv0);
213204
return -1;
@@ -245,41 +236,41 @@ find_my_exec(const char *argv0, char *retpath)
245236
* Second try: since no explicit path was supplied, the user must have
246237
* been relying on PATH. We'll use the same PATH.
247238
*/
248-
if ((p = getenv("PATH")) && *p)
239+
if ((path = getenv("PATH")) && *path)
249240
{
250-
path = pstrdup(p); /* make a modifiable copy */
251-
for (startp = path, endp = strchr(path, PATHSEP);
252-
startp && *startp;
253-
startp = endp + 1, endp = strchr(startp, PATHSEP))
241+
char *startp = NULL, *endp = NULL;
242+
243+
do
254244
{
255-
if (startp == endp) /* it's a "::" */
256-
continue;
257-
if (endp)
258-
*endp = '\0';
245+
if (!startp)
246+
startp = path;
247+
else
248+
startp = endp + 1;
259249

260-
if (is_absolute_path(startp))
261-
snprintf(retpath, MAXPGPATH, "%s/%s", startp, argv0);
250+
endp = strchr(startp, PATHSEP);
251+
if (!endp)
252+
endp = startp + strlen(startp); /* point to end */
253+
254+
StrNCpy(test_path, startp, Min(endp - startp + 1, MAXPGPATH));
255+
256+
if (is_absolute_path(test_path))
257+
snprintf(retpath, MAXPGPATH, "%s/%s", test_path, argv0);
262258
else
263-
snprintf(retpath, MAXPGPATH, "%s/%s/%s", cwd, startp, argv0);
259+
snprintf(retpath, MAXPGPATH, "%s/%s/%s", cwd, test_path, argv0);
264260

265261
canonicalize_path(retpath);
266262
switch (validate_exec(retpath))
267263
{
268264
case 0: /* found ok */
269265
win32_make_absolute(retpath);
270-
pfree(path);
271266
return 0;
272267
case -1: /* wasn't even a candidate, keep looking */
273-
break;
268+
continue;
274269
case -2: /* found but disqualified */
275270
log_error("could not read binary \"%s\"", retpath);
276-
pfree(path);
277-
return -1;
271+
continue;
278272
}
279-
if (!endp) /* last one */
280-
break;
281-
}
282-
pfree(path);
273+
} while (*endp);
283274
}
284275

285276
log_error("could not find a \"%s\" to execute", argv0);

0 commit comments

Comments
 (0)