Skip to content

Commit 0b9466f

Browse files
committed
Offer pnstrdup to frontend code
We already had it on the backend. Frontend can also use it now. Discussion: https://postgr.es/m/20191204144021.GA17976@alvherre.pgsql
1 parent b1abfec commit 0b9466f

File tree

6 files changed

+36
-35
lines changed

6 files changed

+36
-35
lines changed

src/bin/pg_waldump/pg_waldump.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ split_path(const char *path, char **dir, char **fname)
114114
/* directory path */
115115
if (sep != NULL)
116116
{
117-
*dir = pg_strdup(path);
118-
(*dir)[(sep - path) + 1] = '\0'; /* no strndup */
117+
*dir = pnstrdup(path, sep - path);
119118
*fname = pg_strdup(sep + 1);
120119
}
121120
/* local directory */

src/bin/psql/prompt.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,10 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
270270
/* execute command */
271271
case '`':
272272
{
273-
FILE *fd;
274-
char *file = pg_strdup(p + 1);
275-
int cmdend;
273+
int cmdend = strcspn(p + 1, "`");
274+
char *file = pnstrdup(p + 1, cmdend);
275+
FILE *fd = popen(file, "r");
276276

277-
cmdend = strcspn(file, "`");
278-
file[cmdend] = '\0';
279-
fd = popen(file, "r");
280277
if (fd)
281278
{
282279
if (fgets(buf, sizeof(buf), fd) == NULL)
@@ -295,13 +292,10 @@ get_prompt(promptStatus_t status, ConditionalStack cstack)
295292
/* interpolate variable */
296293
case ':':
297294
{
298-
char *name;
295+
int nameend = strcspn(p + 1, ":");
296+
char *name = pnstrdup(p + 1, nameend);
299297
const char *val;
300-
int nameend;
301298

302-
name = pg_strdup(p + 1);
303-
nameend = strcspn(name, ":");
304-
name[nameend] = '\0';
305299
val = GetVariable(pset.vars, name);
306300
if (val)
307301
strlcpy(buf, val, sizeof(buf));

src/bin/scripts/common.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,7 @@ splitTableColumnsSpec(const char *spec, int encoding,
353353
else
354354
cp += PQmblen(cp, encoding);
355355
}
356-
*table = pg_strdup(spec);
357-
(*table)[cp - spec] = '\0'; /* no strndup */
356+
*table = pnstrdup(spec, cp - spec);
358357
*columns = cp;
359358
}
360359

src/common/fe_memutils.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,33 @@ pstrdup(const char *in)
142142
return pg_strdup(in);
143143
}
144144

145+
char *
146+
pnstrdup(const char *in, Size size)
147+
{
148+
char *tmp;
149+
int len;
150+
151+
if (!in)
152+
{
153+
fprintf(stderr,
154+
_("cannot duplicate null pointer (internal error)\n"));
155+
exit(EXIT_FAILURE);
156+
}
157+
158+
len = strnlen(in, size);
159+
tmp = malloc(len + 1);
160+
if (tmp == NULL)
161+
{
162+
fprintf(stderr, _("out of memory\n"));
163+
exit(EXIT_FAILURE);
164+
}
165+
166+
memcpy(tmp, in, len);
167+
tmp[len] = '\0';
168+
169+
return tmp;
170+
}
171+
145172
void *
146173
repalloc(void *pointer, Size size)
147174
{

src/include/common/fe_memutils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extern void pg_free(void *pointer);
3131

3232
/* Equivalent functions, deliberately named the same as backend functions */
3333
extern char *pstrdup(const char *in);
34+
extern char *pnstrdup(const char *in, Size size);
3435
extern void *palloc(Size size);
3536
extern void *palloc0(Size size);
3637
extern void *palloc_extended(Size size, int flags);

src/interfaces/ecpg/compatlib/informix.c

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -175,25 +175,6 @@ deccopy(decimal *src, decimal *target)
175175
memcpy(target, src, sizeof(decimal));
176176
}
177177

178-
static char *
179-
ecpg_strndup(const char *str, size_t len)
180-
{
181-
size_t real_len = strlen(str);
182-
int use_len = (int) ((real_len > len) ? len : real_len);
183-
184-
char *new = malloc(use_len + 1);
185-
186-
if (new)
187-
{
188-
memcpy(new, str, use_len);
189-
new[use_len] = '\0';
190-
}
191-
else
192-
errno = ENOMEM;
193-
194-
return new;
195-
}
196-
197178
int
198179
deccvasc(const char *cp, int len, decimal *np)
199180
{
@@ -205,7 +186,7 @@ deccvasc(const char *cp, int len, decimal *np)
205186
if (risnull(CSTRINGTYPE, cp))
206187
return 0;
207188

208-
str = ecpg_strndup(cp, len); /* decimal_in always converts the complete
189+
str = pnstrdup(cp, len); /* decimal_in always converts the complete
209190
* string */
210191
if (!str)
211192
ret = ECPG_INFORMIX_NUM_UNDERFLOW;

0 commit comments

Comments
 (0)