R
Roman Mashak
Hello, All!
I have this small piece of code, where segmentation fault happenes only upon
runnin code. No problems during debug (JFI I'm using gdb-6.3):
----
struct host_info {
char *host;
int port;
char *path;
int is_ftp;
char *user;
};
....
int parse_url(char *url, struct host_info *h)
{
char *cp, *sp, *up, *pp,*ptr;
if (strncmp(url, "ftp://", 6) == 0) {
h->port = 21;
h->host = url + 6;
h->is_ftp = 1;
} else {
return -1;
}
sp = strchr(h->host, '/');
if (sp) {
*sp++ = '\0'; /* XXX */
h->path = sp;
} else
h->path = strdup("");
up = strrchr(h->host, '@');
if (up != NULL) {
h->user = h->host;
*up++ = '\0';
h->host = up;
} else
h->user = NULL;
pp = h->host;
cp = strchr(pp, ':');
if (cp != NULL) {
*cp++ = '\0';
h->port = htons(atoi(cp));
}
return 0;
}
-----
The problem happenes at 'XXX' mark. I also examined source code with
'splint', which gave me some hints:
---
Implicitly only storage h->path (type char *) not released before assignment
(sp aliases h->host): h->path = sp A memory leak has been detected.
Only-qualified storage is not released before the last reference to it is
lost.
Implicitly only storage h->path (type char *) not released before
assignment: h->path = strdup("")
---
I'm still confused. What can be the problem?
Thank you.
With best regards, Roman Mashak. E-mail: (e-mail address removed)
I have this small piece of code, where segmentation fault happenes only upon
runnin code. No problems during debug (JFI I'm using gdb-6.3):
----
struct host_info {
char *host;
int port;
char *path;
int is_ftp;
char *user;
};
....
int parse_url(char *url, struct host_info *h)
{
char *cp, *sp, *up, *pp,*ptr;
if (strncmp(url, "ftp://", 6) == 0) {
h->port = 21;
h->host = url + 6;
h->is_ftp = 1;
} else {
return -1;
}
sp = strchr(h->host, '/');
if (sp) {
*sp++ = '\0'; /* XXX */
h->path = sp;
} else
h->path = strdup("");
up = strrchr(h->host, '@');
if (up != NULL) {
h->user = h->host;
*up++ = '\0';
h->host = up;
} else
h->user = NULL;
pp = h->host;
cp = strchr(pp, ':');
if (cp != NULL) {
*cp++ = '\0';
h->port = htons(atoi(cp));
}
return 0;
}
-----
The problem happenes at 'XXX' mark. I also examined source code with
'splint', which gave me some hints:
---
Implicitly only storage h->path (type char *) not released before assignment
(sp aliases h->host): h->path = sp A memory leak has been detected.
Only-qualified storage is not released before the last reference to it is
lost.
Implicitly only storage h->path (type char *) not released before
assignment: h->path = strdup("")
---
I'm still confused. What can be the problem?
Thank you.
With best regards, Roman Mashak. E-mail: (e-mail address removed)