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);
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;
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);
ptr0->s_proto = strdup(localproto);
// localproto got alloc'd during the lookup
if (!proto && localproto != proto) free(localproto);
return ptr0;

View file

@ -44,8 +44,11 @@ struct servent *getservbyport(int port, const char *proto) {
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;
}
ptr1->s_port = port;
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);
ptr1->s_proto = strdup(localproto);
// localproto got alloc'd during the lookup
if (!proto && localproto != proto) free(localproto);
return ptr1;

View file

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