Skip to content

Commit

Permalink
fix parse_json() memory leak
Browse files Browse the repository at this point in the history
version bump

more code documentation

fixed license message in --version
  • Loading branch information
Marco Scannadinari committed Oct 14, 2013
1 parent 4607dd2 commit dcba372
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 19 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ RESET="\033[0m"
INDENT=@echo -n " "
NEWL=@echo

BTCWATCH_VERSION=0.0.2
BTCWATCH_VERSION=0.0.3

ifdef $(CC)
MCC=$(CC)
Expand Down Expand Up @@ -183,5 +183,7 @@ distclean: clean
$(foreach i, $(wildcard config.*), $(INDENT) ${\n} rm -rf $(i) ${\n})
$(INDENT)
rm -rf autom4te.cache
$(INDENT)
rm -rf prefix.txt
$(NEWL)

8 changes: 7 additions & 1 deletion src/include/btcapi.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013 Marco Scannadinari
Copyright (C) 2013 Marco Scannadinari.
This file is part of btcwatch.
Expand Down Expand Up @@ -32,6 +32,7 @@ typedef struct {
} currcy_t;

// struct containing current exchange information

typedef struct {
float buy;
currcy_t currcy;
Expand All @@ -43,15 +44,20 @@ typedef struct {
extern rates_t btcrates;

// poplates the global btcrates struct

int fill_rates(const char *const currency, btcerr_t *const api_err);

// uses libcURL to access a Bitcoin API, calls write_data, then returns a JSON string

char *get_json(const char *currency, btcerr_t *const api_err);


// uses jansson to parse the JSON string and returns a rates_t containing exchange information

int parse_json(const char *const json, btcerr_t *const api_err);

// libcURL callback function that copies the buffer to a local string

size_t write_data(
char *buffer,
size_t size,
Expand Down
2 changes: 1 addition & 1 deletion src/include/cmdlineutils.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013 Marco Scannadinari
Copyright (C) 2013 Marco Scannadinari.
This file is part of btcwatch.
Expand Down
12 changes: 7 additions & 5 deletions src/lib/btcapi.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013 Marco Scannadinari
Copyright (C) 2013 Marco Scannadinari.
This file is part of btcwatch.
Expand All @@ -17,7 +17,7 @@
along with btcwatch. If not, see <http://www.gnu.org/licenses/>.
*/

#define API_URL_CURRCY_POS 32
#define API_URL_CURRCY_POS 32 // position to insert currency

#include <curl/curl.h>
#include <ctype.h>
Expand Down Expand Up @@ -217,6 +217,7 @@ char *get_json(const char *const currcy, btcerr_t *const api_err) {
#endif

// length check

if(strlen(currcy) != 3) {
#if DEBUG
btcdbg(__FILE__, __LINE__, "bad currency length");
Expand All @@ -229,19 +230,19 @@ char *get_json(const char *const currcy, btcerr_t *const api_err) {
}

// case correction

for(
uint_fast8_t i = 0;
i < (sizeof mod_currcy[0] * sizeof mod_currcy);
++i
) mod_currcy[i] = toupper(mod_currcy[i]);

mod_currcy[strlen(mod_currcy) + 1] = '\0';

#if DEBUG
btcdbg(__FILE__, __LINE__, "mod_currcy \"%s\" strlen: %d", mod_currcy, strlen(mod_currcy));
#endif

// validation

for(
uint_fast8_t i = 0;
i < ((sizeof currencies / sizeof currencies[0]));
Expand Down Expand Up @@ -324,7 +325,6 @@ int parse_json(const char *const json, btcerr_t *const api_err) {
root = json_loads(json, 0, &json_error);
if(!root) {
strcpy(api_err -> errstr, json_error.text);

return 0;
}

Expand All @@ -343,6 +343,8 @@ int parse_json(const char *const json, btcerr_t *const api_err) {
btcdbg(__FILE__, __LINE__, "sell %f", btcrates.sell);
#endif

json_decref(root);

return 1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib/btcdbg.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013 Marco Scannadinari
Copyright (C) 2013 Marco Scannadinari.
This file is part of btcwatch.
Expand Down
2 changes: 1 addition & 1 deletion src/lib/btcerr.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013 Marco Scannadinari
Copyright (C) 2013 Marco Scannadinari.
This file is part of btcwatch.
Expand Down
18 changes: 10 additions & 8 deletions src/lib/cmdlineutils.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013 Marco Scannadinari
Copyright (C) 2013 Marco Scannadinari.
This file is part of btcwatch.
Expand Down Expand Up @@ -43,21 +43,21 @@ noreturn void help(const char *const prog_name, const char *const optstring) {
" -s --sell print sell price\n"
" -v --verbose increase verbosity\n"
"\n"
"Report bugs to <marco@scannadinari.co.uk>\n"
"Report bugs to marco@scannadinari.co.uk\n"
"btcwatch home page: <https://github.com/marcoms/btcwatch/>"
);

exit(EXIT_SUCCESS);
}

void resetb(void) {
freopen(NULL, "a", stdout);
fwide(stdout, -1);
freopen(NULL, "a", stdout); // reopen stdout
fwide(stdout, -1); // set stdout to be byte-oriented
}

void resetw(void) {
freopen(NULL, "a", stdout);
fwide(stdout, 1);
freopen(NULL, "a", stdout); // ^
fwide(stdout, 1); // set stdout to be wide-oriented
}

noreturn void version(const char *const version) {
Expand All @@ -67,10 +67,12 @@ noreturn void version(const char *const version) {

printf("btcwatch %s\n", version);
puts(
"Copyright (C) 2007 Free Software Foundation, Inc.\n"
"Copyright (C) Marco Scannadinari.\n"
"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law."
"There is NO WARRANTY, to the extent permitted by law.\n"
"\n"
"Written by Marco Scannadinari."
);


Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013 Marco Scannadinari
Copyright (C) 2013 Marco Scannadinari.
This file is part of btcwatch.
Expand Down

0 comments on commit dcba372

Please sign in to comment.