fix failure edge case and PATH_MAX

This commit is contained in:
ahgamut 2021-07-04 06:28:32 +05:30
parent 8c9750fb30
commit 1b9b3864b6
3 changed files with 11 additions and 6 deletions

View file

@ -46,7 +46,11 @@ struct servent *getservbyname(const char *name, const char *proto) {
} }
p = LookupServicesByName(name, &localproto); p = LookupServicesByName(name, &localproto);
if (p == -1) return NULL; if (p == -1) {
// localproto got alloc'd during the lookup?
if (!proto && localproto != proto) free(localproto);
return NULL;
}
ptr0->s_port = p; ptr0->s_port = p;
if (ptr0->s_name) free(ptr0->s_name); if (ptr0->s_name) free(ptr0->s_name);
@ -55,7 +59,6 @@ struct servent *getservbyname(const char *name, const char *proto) {
if (ptr0->s_proto) free(ptr0->s_proto); if (ptr0->s_proto) free(ptr0->s_proto);
ptr0->s_proto = strdup(localproto); ptr0->s_proto = strdup(localproto);
// localproto got alloc'd during the lookup
if (!proto && localproto != proto) free(localproto); if (!proto && localproto != proto) free(localproto);
return ptr0; return ptr0;

View file

@ -44,8 +44,11 @@ struct servent *getservbyport(int port, const char *proto) {
ptr1 = &se1; ptr1 = &se1;
} }
if (LookupServicesByPort(port, &localproto, name, sizeof(name)) == -1) if (LookupServicesByPort(port, &localproto, name, sizeof(name)) == -1) {
// localproto got alloc'd during the lookup?
if (!proto && localproto != proto) free(localproto);
return NULL; return NULL;
}
ptr1->s_port = port; ptr1->s_port = port;
if (ptr1->s_name) free(ptr1->s_name); if (ptr1->s_name) free(ptr1->s_name);
@ -54,7 +57,6 @@ struct servent *getservbyport(int port, const char *proto) {
if (ptr1->s_proto) free(ptr1->s_proto); if (ptr1->s_proto) free(ptr1->s_proto);
ptr1->s_proto = strdup(localproto); ptr1->s_proto = strdup(localproto);
// localproto got alloc'd during the lookup
if (!proto && localproto != proto) free(localproto); if (!proto && localproto != proto) free(localproto);
return ptr1; return ptr1;

View file

@ -75,7 +75,7 @@ int LookupServicesByPort(const int servport, char **servproto, char *buf,
size_t bufsize) { size_t bufsize) {
FILE *f; FILE *f;
char *line; char *line;
char pathbuf[512]; char pathbuf[PATH_MAX];
const char *path; const char *path;
size_t linesize; size_t linesize;
int count, found; int count, found;
@ -138,7 +138,7 @@ int LookupServicesByPort(const int servport, char **servproto, char *buf,
int LookupServicesByName(const char *servname, char **servproto) { int LookupServicesByName(const char *servname, char **servproto) {
FILE *f; FILE *f;
char *line; char *line;
char pathbuf[512]; char pathbuf[PATH_MAX];
const char *path; const char *path;
size_t linesize; size_t linesize;
int count, found, result; int count, found, result;