freed from malloc and free

FossilOrigin-Name: f7310807426396321ac7af203206bfe465a547182ef0f8ab74d4c2272782fa1d
This commit is contained in:
7u83@mail.ru 2018-03-12 11:25:47 +00:00
parent d6133e0434
commit 835dc82e6f
1 changed files with 8 additions and 9 deletions

View File

@ -8,25 +8,25 @@
#include "sock.h" #include "sock.h"
/** /**
* Convert a string to an sockaddr struct. * Convert a string to a sockaddr struct.
* The string can contain an ipv4 or ipv6 address, including a port number. * The string can contain an ipv4 or ipv6 address, including a port number.
* @param s address string * @param s address string, the address string MUST not be longer
* than 128 characters
* @param saout output buffer * @param saout output buffer
* @return 1 on success, otherwise no success * @return 1 on success, otherwise no success, consult errno!
*/ */
int sock_strtoaddr(const char * s,struct sockaddr * saout){ int sock_strtoaddr(const char * s,struct sockaddr * saout){
char *ips,*ps; char ips[128],*ps;
struct in_addr ia; struct in_addr ia;
int port; int port;
#ifdef WITH_IPV6 #ifndef WITHOUT_IPV6
struct in6_addr ia6; struct in6_addr ia6;
#endif #endif
int rc; int rc;
/* copy the string */ /* copy the string */
ips = malloc(strlen(s)+1);
strcpy(ips,s); strcpy(ips,s);
/* search for a collon to separate the port */ /* search for a collon to separate the port */
@ -62,10 +62,9 @@ int sock_strtoaddr(const char * s,struct sockaddr * saout){
sa->sin_family = AF_INET; sa->sin_family = AF_INET;
sa->sin_addr=ia; sa->sin_addr=ia;
sa->sin_port=htons(port); sa->sin_port=htons(port);
} }
#ifdef WITH_IPV6 #ifndef WITHOUT_IPV6
if (rc==0){ if (rc==0){
strcpy(ips,s); strcpy(ips,s);
@ -99,12 +98,12 @@ int sock_strtoaddr(const char * s,struct sockaddr * saout){
} }
#endif #endif
if (rc!=1){ if (rc!=1){
if (rc!=-1) if (rc!=-1)
errno=EINVAL; errno=EINVAL;
} }
free (ips);
return rc; return rc;
} }