aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2015-05-17 17:23:18 -0400
committerFranklin Wei <git@fwei.tk>2015-05-17 17:23:18 -0400
commit8e9a52ceae1f2e0fac76d06ff5e6a2170dcdd1bb (patch)
tree11868a8886f35f802c14f091d28b69c4d9abedc4
parentc13b48095f91b6c277854b9970c59c82943af74f (diff)
downloadmarket-sim-8e9a52ceae1f2e0fac76d06ff5e6a2170dcdd1bb.zip
market-sim-8e9a52ceae1f2e0fac76d06ff5e6a2170dcdd1bb.tar.gz
market-sim-8e9a52ceae1f2e0fac76d06ff5e6a2170dcdd1bb.tar.bz2
market-sim-8e9a52ceae1f2e0fac76d06ff5e6a2170dcdd1bb.tar.xz
fix some memory leaks
-rw-r--r--Makefile2
-rw-r--r--src/buy.c41
-rw-r--r--src/sell.c5
-rw-r--r--src/util.c2
4 files changed, 27 insertions, 23 deletions
diff --git a/Makefile b/Makefile
index e49c926..69d2366 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ CC = cc
SRC := $(wildcard src/*.c)
OBJ := $(SRC:.c=.o)
-CFLAGS = -Isrc/ -O3 -g -Wall -fsanitize=address -std=c99
+CFLAGS = -Isrc/ -O3 -g -Wall -std=c99
HEADERS := $(wildcard src/*.h)
diff --git a/src/buy.c b/src/buy.c
index a63161f..267df73 100644
--- a/src/buy.c
+++ b/src/buy.c
@@ -14,6 +14,7 @@ void buy_handler(struct player_t *player)
if(!get_stock_info(sym, &price, &name))
{
printf("Failed to get query information for '%s'.\n", sym);
+ free(sym);
return;
}
@@ -45,36 +46,32 @@ void buy_handler(struct player_t *player)
{
printf("Confirmed.\n");
- struct stock_t *stock;
+ struct stock_t *stock = find_stock(player, sym);
/* add the stock to the portfolio or increase the count of a stock */
- for(uint i = 0; i < player->portfolio_len; ++i)
+ if(stock)
{
- if(strcmp(player->portfolio[i].symbol, sym) == 0)
- {
- stock = player->portfolio + i;
- stock->count += count;
- stock->current_price.cents = price.cents;
- goto done;
- }
+ stock->count += count;
+ stock->current_price.cents = price.cents;
}
+ else
+ {
+ /* stock is not in portfolio yet, add it */
- /* not found, add a new one */
- player->portfolio_len += 1;
- player->portfolio = realloc(player->portfolio, player->portfolio_len * sizeof(struct stock_t));
- player->need_to_free_portfolio = true;
-
- stock = player->portfolio + player->portfolio_len - 1;
+ player->portfolio_len += 1;
+ player->portfolio = realloc(player->portfolio, player->portfolio_len * sizeof(struct stock_t));
+ player->need_to_free_portfolio = true;
- memset(stock, 0, sizeof(struct stock_t));
+ stock = player->portfolio + player->portfolio_len - 1;
- stock->symbol = sym;
- stock->fullname = name;
- stock->count = count;
- stock->current_price.cents = price.cents;
+ memset(stock, 0, sizeof(struct stock_t));
- done:
+ stock->symbol = sym;
+ stock->fullname = name;
+ stock->count = count;
+ stock->current_price.cents = price.cents;
+ }
player->cash.cents -= cost;
@@ -87,4 +84,6 @@ void buy_handler(struct player_t *player)
{
printf("Not confirmed.\n");
}
+
+ free(response);
}
diff --git a/src/sell.c b/src/sell.c
index ae5fe0e..d390419 100644
--- a/src/sell.c
+++ b/src/sell.c
@@ -14,14 +14,17 @@ void sell_handler(struct player_t *player)
if(!stock)
{
printf("Couldn't find '%s' in portfolio.\n", sym);
+ free(sym);
return;
}
+ free(sym);
+
printf("Updating prices...\n");
get_stock_info(stock->symbol, &stock->current_price, &stock->fullname);
- printf("You currently own %llu shares of '%s' (%s) valued at $%llu.%02llu each.\n",
+ printf("You currently own %llu share(s) of '%s' (%s) valued at $%llu.%02llu each.\n",
stock->count, stock->fullname, stock->symbol, stock->current_price.cents / 100, stock->current_price.cents % 100);
printf("How many shares do you wish to sell? ");
diff --git a/src/util.c b/src/util.c
index 8f82976..0740a09 100644
--- a/src/util.c
+++ b/src/util.c
@@ -263,5 +263,7 @@ ullong read_int(void)
ullong ret = -1;
sscanf(str, "%llu", &ret);
+ free(str);
+
return ret;
}