diff --git a/Makefile b/Makefile index 929c380927cbc3ab677b77db1b823e08b13ed70d..9bce6ef63b419da11eceb82a7a16a61d4bd2bd0f 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC = gcc CFLAGS = -g -Wall LIBS = -ljson-c -lubox -lubus -luci -lcurl -lmobile -all: libmobile1 libmobile2# common dongle_apn dongle_pin dongle_network +all: libmobile1 libmobile2 common dongle_apn dongle_pin dongle_network #gcc -shared -o libhello.so -fPIC hello.c @@ -34,7 +34,7 @@ libmobile2: ${MOBJS} #DPOBJS = dongle_pin.o #DPSRCS = dongle_pin.c #dongle_pin: ${DPOBJS} -# ${CC} ${CFLAGS} ${DPOBJS} ${COBJS} -o dongle_pin ${LIBS} +# ${CC} ${CFLAGS} ${DPOBJS} ${COBJS} -o dongle_pin -L . ${LIBS} # #DNOBJS = dongle_network.o #DNSRCS = dongle_network.c diff --git a/libmobile.c b/libmobile.c index 0484c46fcb40e41ff889d96e0599a668b010f1bf..517c2ff9ae1290596b88b4f70fd74bd6d7443b1e 100644 --- a/libmobile.c +++ b/libmobile.c @@ -18,6 +18,7 @@ static int apn_profile_idx(struct json_object *apn_profiles, char *name); static int get_apn_profiles_len(void); static char *lexer(char **input, char *delimiter); static char *get_query_wrapper(char *vars); +static char *mobile_post_request(char *query); /** * Function: curl_cleaner @@ -50,18 +51,21 @@ static void curl_cleaner(CURLcode *curl) static size_t write_func(void *buffer, size_t size, size_t nmemb, void *userp) { struct string *str = (struct string *)userp; - size_t new_len = str->len + (size * nmemb); + size_t len = size * nmemb; + size_t new_len = str->len + len; - str->ptr = realloc(str->ptr, new_len + 1); - if (str->ptr == NULL) { + ptr = realloc(str->ptr, new_len + 1); + if (ptr == NULL) { + free(str->ptr); printf("not enough ptr (realloc returned NULL)\n"); return 0; - } - memcpy(str->ptr + str->len, buffer, size * nmemb); + } else //realloc succeeded + str->ptr = ptr; + memcpy(str->ptr + str->len, buffer, len); str->ptr[new_len] = '\0'; str->len = new_len; - return size * nmemb; + return len; } /** @@ -438,7 +442,7 @@ char *mobile_disable_pin(char *pin) return mobile_post_request(query); } -char *mobile_post_request(char *query) +static char *mobile_post_request(char *query) { CURL *curl; CURLcode res; @@ -478,6 +482,7 @@ char *mobile_get_request(char *vars) if (!query) goto fail; + // flow, calloc return code, gotos and frees str.ptr = calloc(1, 1); str.len = 0; curl = curl_easy_init();