From 8c4a7a045adf19a92b417050134254351d833384 Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Sun, 5 Jan 2014 20:40:58 +0000
Subject: [PATCH] [Fix] Fixed some valgrind errors.

---
 README           | 3 ++-
 src/llua_space.c | 5 +++--
 src/player.c     | 8 +++++---
 3 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/README b/README
index f60e579..8d975c8 100644
--- a/README
+++ b/README
@@ -76,7 +76,8 @@ Keys:
 
       KEY_IDENTIFIER is the indetifier given below:
       KEY_TYPE can be one of keyboard, jaxis or jbutton.
-      KEY_NUMBER is the number of the key (found with xev)
+      KEY_NUMBER is the number of the key (found with xev usually, just
+        convert the keysym from hex to base 10).
       KEY_REVERSE is whether it is reversed or not, which
         is  only useful in the case of jaxis.
       MOD_IDENTIFIER is the modifier to take into account, can be on of:
diff --git a/src/llua_space.c b/src/llua_space.c
index d6ed420..9d2c20d 100644
--- a/src/llua_space.c
+++ b/src/llua_space.c
@@ -235,10 +235,11 @@ static int planetL_get(lua_State* L) {
 
   /* Get a planet from faction list. */
   else if(lua_istable(L, 1)) {
+    /* Get table length and preallocate. */
+    nfactions = (int) lua_objlen(L, 1);
+    factions = malloc(sizeof(int) * nfactions);
     /* Load up the table. */
     lua_pushnil(L);
-    nfactions = (int)lua_gettop(L);
-    factions = malloc(sizeof(int) * nfactions);
     i = 0;
     while(lua_next(L, -2) != 0) {
       f = lua_tofaction(L, -1);
diff --git a/src/player.c b/src/player.c
index addc9de..cd310df 100644
--- a/src/player.c
+++ b/src/player.c
@@ -1688,6 +1688,7 @@ void player_land(void) {
   int i;
   int tp;
   double td, d;
+  Planet* planet;
 
   if(landed) {
     /* Player is already landed. */
@@ -1701,8 +1702,8 @@ void player_land(void) {
     return;
   }
 
-  Planet* planet = cur_system->planets[planet_target];
   if(planet_target >= 0) {
+    planet = cur_system->planets[planet_target];
     if(!planet_hasService(planet, PLANET_SERVICE_LAND)) {
       player_message("You can't land here.");
       return;
@@ -1741,8 +1742,9 @@ void player_land(void) {
     td = -1; /* Temp distance. */
     tp = -1; /* Temp planet. */
     for(i = 0; i < cur_system->nplanets; i++) {
-      d = vect_dist(&player->solid->pos, &cur_system->planets[i]->pos);
-      if(planet_hasService(cur_system->planets[i], PLANET_SERVICE_LAND) &&
+      planet = cur_system->planets[i];
+      d = vect_dist(&player->solid->pos, &planet->pos);
+      if(planet_hasService(planet, PLANET_SERVICE_LAND) &&
          ((tp == -1) || ((td == -1) || (td > d)))) {
         tp = i;
         td = d;