]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
Improve block deserialisation
authorElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 20 Mar 2021 11:14:34 +0000 (12:14 +0100)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 20 Mar 2021 11:14:34 +0000 (12:14 +0100)
client.c
map.c
node.h
server.c

index b1f090c21354ab040cb70c6cb6d9344284b67f3f..14497c54d319a39a4c1e446b7d5e0a95ccfadbaa 100644 (file)
--- a/client.c
+++ b/client.c
@@ -34,7 +34,10 @@ int main(int argc, char **argv)
        Map *map = map_create(NULL);
 
        MapBlock *block = map_deserialize_block(sockfd);
-       map_create_block(map, (v3s32) {0, 0, 0}, block);
+       if (block)
+               map_create_block(map, (v3s32) {0, 0, 0}, block);
+       else
+               internal_error("invalid block recieved");
 
        MapNode node = map_get_node(map, (v3s32) {0, 0, 0});
        printf("%d\n", node.type);
diff --git a/map.c b/map.c
index 3d8df081ca67e47faa5f9bc737dc8f9ac1bdb894..a95238f991e874c39d3d1dd87d39071bb0693f39 100644 (file)
--- a/map.c
+++ b/map.c
@@ -96,9 +96,12 @@ MapBlock *map_deserialize_block(int fd)
        MapBlock *block = malloc(sizeof(MapBlock));
        ITERATE_MAPBLOCK {
                u32 encoded_type;
-               read(fd, &encoded_type, 4);
+               if (read(fd, &encoded_type, 4) == -1) {
+                       free(block);
+                       return NULL;
+               }
                Node type = be32toh(encoded_type);
-               if (type >= MAX_NODES)
+               if (type > NODE_INVALID)
                        type = NODE_INVALID;
                block->data[x][y][z] = map_node_create(type);
        }
diff --git a/node.h b/node.h
index 0f5954d4405d1b170a12283cd52128bbaa5e9a62..25f02156d81954a37c27a6c1ec5749af8e3fbe84 100644 (file)
--- a/node.h
+++ b/node.h
@@ -4,12 +4,11 @@
 typedef enum
 {
        NODE_UNLOADED,          // Used for nodes in unloaded blocks
-       NODE_INVALID,           // Used for invalid nodes sent by server
        NODE_AIR,
        NODE_GRASS,
        NODE_DIRT,
        NODE_STONE,
-       MAX_NODES,
+       NODE_INVALID,           // Used for invalid nodes sent by server (caused by outdated clients)
 } Node;
 
 #endif
index d6742080cbb6f05d670d6ad2f182263d0d9a25b1..5330b5004862974225256be2468a62b48da2b71c 100644 (file)
--- a/server.c
+++ b/server.c
@@ -34,8 +34,8 @@ int main(int argc, char **argv)
        Map *map = map_create(NULL);
        map_set_node(map, (v3s32) {0, 0, 0}, map_node_create(NODE_STONE));
 
-       struct sockaddr_in cli_addr_buf;
-       socklen_t cli_addrlen_buf;
+       struct sockaddr_in cli_addr_buf = {0};
+       socklen_t cli_addrlen_buf = 0;
 
        int fd = accept(sockfd, (struct sockaddr *) &cli_addr_buf, &cli_addrlen_buf);