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

Commit0b9466f

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 parentb1abfec commit0b9466f

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-
intcmdend;
273+
intcmdend=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+
intnameend=strcspn(p+1,":");
296+
char*name=pnstrdup(p+1,nameend);
299297
constchar*val;
300-
intnameend;
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
returnpg_strdup(in);
143143
}
144144

145+
char*
146+
pnstrdup(constchar*in,Sizesize)
147+
{
148+
char*tmp;
149+
intlen;
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+
returntmp;
170+
}
171+
145172
void*
146173
repalloc(void*pointer,Sizesize)
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
externchar*pstrdup(constchar*in);
34+
externchar*pnstrdup(constchar*in,Sizesize);
3435
externvoid*palloc(Sizesize);
3536
externvoid*palloc0(Sizesize);
3637
externvoid*palloc_extended(Sizesize,intflags);

‎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-
staticchar*
179-
ecpg_strndup(constchar*str,size_tlen)
180-
{
181-
size_treal_len=strlen(str);
182-
intuse_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-
returnnew;
195-
}
196-
197178
int
198179
deccvasc(constchar*cp,intlen,decimal*np)
199180
{
@@ -205,7 +186,7 @@ deccvasc(const char *cp, int len, decimal *np)
205186
if (risnull(CSTRINGTYPE,cp))
206187
return0;
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)

[8]ページ先頭

©2009-2025 Movatter.jp