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

Commit8d0df20

Browse files
committed
Fix write-past-buffer-end in ldapServiceLookup().
The code to assemble ldap_get_values_len's output into a single stringwrote the terminating null one byte past where it should. Fix that,and make some other cosmetic adjustments to make the code a trifle morereadable and more in line with usual Postgres coding style.Also, free the "result" string when done with it, to avoid a permanentmemory leak.Bug report and patch by Albe Laurenz, cosmetic adjustments by me.
1 parentc6eb574 commit8d0df20

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

‎src/interfaces/libpq/fe-connect.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3596,25 +3596,26 @@ ldapServiceLookup(const char *purl, PQconninfoOption *options,
35963596
return1;
35973597
}
35983598

3599-
/* concatenate values to a single string */
3600-
for (size=0,i=0;values[i]!=NULL;++i)
3599+
/* concatenate values into a single string with newline terminators */
3600+
size=1;/* for the trailing null */
3601+
for (i=0;values[i]!=NULL;i++)
36013602
size+=values[i]->bv_len+1;
3602-
if ((result=malloc(size+1))==NULL)
3603+
if ((result=malloc(size))==NULL)
36033604
{
36043605
printfPQExpBuffer(errorMessage,
36053606
libpq_gettext("out of memory\n"));
36063607
ldap_value_free_len(values);
36073608
ldap_unbind(ld);
36083609
return3;
36093610
}
3610-
for (p=result,i=0;values[i]!=NULL;++i)
3611+
p=result;
3612+
for (i=0;values[i]!=NULL;i++)
36113613
{
3612-
strncpy(p,values[i]->bv_val,values[i]->bv_len);
3614+
memcpy(p,values[i]->bv_val,values[i]->bv_len);
36133615
p+=values[i]->bv_len;
36143616
*(p++)='\n';
3615-
if (values[i+1]==NULL)
3616-
*(p+1)='\0';
36173617
}
3618+
*p='\0';
36183619

36193620
ldap_value_free_len(values);
36203621
ldap_unbind(ld);
@@ -3643,6 +3644,7 @@ ldapServiceLookup(const char *purl, PQconninfoOption *options,
36433644
printfPQExpBuffer(errorMessage,libpq_gettext(
36443645
"missing \"=\" after \"%s\" in connection info string\n"),
36453646
optname);
3647+
free(result);
36463648
return3;
36473649
}
36483650
elseif (*p=='=')
@@ -3661,6 +3663,7 @@ ldapServiceLookup(const char *purl, PQconninfoOption *options,
36613663
printfPQExpBuffer(errorMessage,libpq_gettext(
36623664
"missing \"=\" after \"%s\" in connection info string\n"),
36633665
optname);
3666+
free(result);
36643667
return3;
36653668
}
36663669
break;
@@ -3724,6 +3727,7 @@ ldapServiceLookup(const char *purl, PQconninfoOption *options,
37243727
printfPQExpBuffer(errorMessage,
37253728
libpq_gettext("invalid connection option \"%s\"\n"),
37263729
optname);
3730+
free(result);
37273731
return1;
37283732
}
37293733
optname=NULL;
@@ -3732,6 +3736,8 @@ ldapServiceLookup(const char *purl, PQconninfoOption *options,
37323736
oldstate=state;
37333737
}
37343738

3739+
free(result);
3740+
37353741
if (state==5||state==6)
37363742
{
37373743
printfPQExpBuffer(errorMessage,libpq_gettext(

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp