From 0eb98d24befc871179e42303a239816b1adaa73f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Florian=20M=C3=A4rkl?= <info@florianmaerkl.de>
Date: Tue, 17 Mar 2020 13:31:28 +0100
Subject: [PATCH] Fix all Warnings for GCC 9 (#16223)

---
 libr/anal/p/anal_ppc_cs.c        | 22 -------------
 libr/bin/format/dmp/dmp64.c      | 56 --------------------------------
 libr/core/cmd_write.c            |  2 +-
 libr/debug/dreg.c                |  4 +--
 libr/debug/p/debug_native.c      |  2 +-
 libr/include/r_vector.h          |  6 ++--
 libr/io/p/io_debug.c             |  2 ++
 shlr/grub/fs/hfsplus.c           |  2 +-
 shlr/grub/fs/jfs.c               |  4 +--
 shlr/grub/fs/xfs.c               | 10 +++---
 shlr/grub/include/grub/charset.h |  2 +-
 shlr/grub/include/grub/hfs.h     |  6 +++-
 shlr/grub/include/grub/types.h   | 10 ++++++
 shlr/grub/partmap/sun.c          |  2 +-
 shlr/grub/partmap/sunpc.c        |  2 +-
 test/unit/test_anal_function.c   |  2 +-
 test/unit/test_buf.c             |  2 --
 17 files changed, 36 insertions(+), 100 deletions(-)

diff --git a/libr/anal/p/anal_ppc_cs.c b/libr/anal/p/anal_ppc_cs.c
index 36d9aa7b13..d271573d5b 100644
--- a/libr/anal/p/anal_ppc_cs.c
+++ b/libr/anal/p/anal_ppc_cs.c
@@ -67,27 +67,6 @@ static const char* cmask32(const char *mb_c, const char *me_c) {
 	return cmask;
 }
 
-static const char *getreg(struct Getarg *gop, int n) {
-	cs_insn *insn = gop->insn;
-	csh handle = gop->handle;
-
-	if (n < 0 || n >= 8) {
-		return NULL;
-	}
-	cs_ppc_op op = INSOP (n);
-	switch (op.type) {
-	case PPC_OP_REG:
-		return cs_reg_name (handle, op.reg);
-	case PPC_OP_MEM:
-		return cs_reg_name (handle, op.mem.base);
-	case PPC_OP_INVALID:
-	case PPC_OP_IMM:
-	case PPC_OP_CRX: // Condition Register field
-		return NULL;
-	}
-	return NULL;
-}
-
 static char *getarg2(struct Getarg *gop, int n, const char *setstr) {
 	cs_insn *insn = gop->insn;
 	csh handle = gop->handle;
@@ -223,7 +202,6 @@ static void opex(RStrBuf *buf, csh handle, cs_insn *insn) {
 #define PPCSPR(n) getspr(&gop, n)
 #define ARG(n) getarg2(&gop, n, "")
 #define ARG2(n,m) getarg2(&gop, n, m)
-#define REG2(n) getreg(&gop, n)
 
 static int set_reg_profile(RAnal *anal) {
 	const char *p = NULL;
diff --git a/libr/bin/format/dmp/dmp64.c b/libr/bin/format/dmp/dmp64.c
index 700ffe893d..85f4be4990 100644
--- a/libr/bin/format/dmp/dmp64.c
+++ b/libr/bin/format/dmp/dmp64.c
@@ -67,62 +67,6 @@ static int r_bin_dmp64_init_header(struct r_bin_dmp64_obj_t *obj) {
 	return true;
 }
 
-static int findPage(const dmp_page_desc *a, const dmp_page_desc *b) {
-	return a && b &&
-		a->start >= b->start &&
-		a->start < (b->start + PAGE_SIZE) ? 0 : 1;
-}
-
-static ut64 read_pte(struct r_bin_dmp64_obj_t *obj, ut64 vaddr) {
-	ut64 res = 0;
-	dmp_page_desc *target = R_NEW0 (dmp_page_desc);
-	target->start = vaddr;
-	RListIter *target_iter = r_list_find (obj->pages, target, (RListComparator)findPage);
-	if (target_iter) {
-		dmp_page_desc *page = r_list_iter_get_data (target_iter);
-		res = r_buf_read_le64_at (obj->b, page->file_offset + vaddr - page->start);
-	} else {
-		eprintf ("Warning: Page not present in the dump file.\n");
-	}
-	free (target);
-	return res;
-}
-
-static ut64 vtop(struct r_bin_dmp64_obj_t *obj, ut64 vaddr) {
-	ut64 pml4e_addr = (obj->dtb & 0xffffffffff000) |
-				((vaddr & 0xff8000000000) >> 36);
-	ut64 pml4e_value = read_pte (obj, pml4e_addr);
-	if (!(pml4e_value & 1)) {
-		eprintf ("Warning: Invalid PML4E\n");
-		return 0;
-	}
-	ut64 pdpte_addr = ((pml4e_value & 0xffffffffff000) |
-				((vaddr & 0x7FC0000000) >> 27));
-	ut64 pdpte_value = read_pte (obj, pdpte_addr);
-	if (!(pdpte_value & 1)) {
-		eprintf ("Warning: Invalid PDPTE\n");
-		return 0;
-	}
-	ut64 pde_addr = ((pdpte_value & 0xffffffffff000) |
-				((vaddr & 0x3fe00000) >> 18));
-	ut64 pde_value = read_pte (obj, pde_addr);
-	if (!(pde_value & 1)) {
-		eprintf ("Warning: Invalid PDE\n");
-		return 0;
-	}
-	ut64 pte_addr = (pde_value & 0xffffffffff000) |
-				((vaddr & 0x1ff000) >> 9);
-	ut64 pte_value = read_pte (obj, pte_addr);
-	if (!(pte_value & 1)) {
-		eprintf ("Warning: Invalid PTE\n");
-		return 0;
-	}
-	ut64 paddr = (pte_value & 0xffffffffff000) |
-				(vaddr & 0xfff);
-
-	return paddr;
-}
-
 static int r_bin_dmp64_init_bmp_pages(struct r_bin_dmp64_obj_t *obj) {
 	int i;
 	if (!obj->bmp_header) {
diff --git a/libr/core/cmd_write.c b/libr/core/cmd_write.c
index f8b1e4d09f..f5433ba129 100644
--- a/libr/core/cmd_write.c
+++ b/libr/core/cmd_write.c
@@ -405,7 +405,7 @@ static void cmd_write_op (RCore *core, const char *input) {
 		case 'D': // "wopD"
 			{
 				char *sp = strchr (input, ' ');
-				len = (int)(sp)?  r_num_math (core->num, sp + 1): core->blocksize;
+				len = sp?  r_num_math (core->num, sp + 1): core->blocksize;
 			}
 			if (len > 0) {
 				/* XXX This seems to fail at generating long patterns (wopD 512K) */
diff --git a/libr/debug/dreg.c b/libr/debug/dreg.c
index 6dea7b2b9c..444fc4de32 100644
--- a/libr/debug/dreg.c
+++ b/libr/debug/dreg.c
@@ -83,7 +83,7 @@ R_API int r_debug_reg_sync(RDebug *dbg, int type, int write) {
 }
 
 R_API int r_debug_reg_list(RDebug *dbg, int type, int size, int rad, const char *use_color) {
-	int delta, from, to, cols, n = 0;
+	int delta, cols, n = 0;
 	const char *fmt, *fmt2, *kwhites;
 	RPrint *pr = NULL;
 	int colwidth = 20;
@@ -125,8 +125,6 @@ R_API int r_debug_reg_list(RDebug *dbg, int type, int size, int rad, const char
 	}
 	// with the new field "arena" into reg items why need
 	// to get all arenas.
-	from = 0;
-	to = R_REG_TYPE_LAST;
 
 	int itmidx = -1;
 	dbg->creg = NULL;
diff --git a/libr/debug/p/debug_native.c b/libr/debug/p/debug_native.c
index 6614826a74..795fb1ed48 100644
--- a/libr/debug/p/debug_native.c
+++ b/libr/debug/p/debug_native.c
@@ -91,7 +91,7 @@ R_API RList *r_w32_dbg_maps(RDebug *);
 /* begin of debugger code */
 #if DEBUGGER
 
-#if !__WINDOWS__ || (!__APPLE__ && defined(WAIT_ON_ALL_CHILDREN))
+#if !__WINDOWS__ && !(__linux__ && !defined(WAIT_ON_ALL_CHILDREN))
 static int r_debug_handle_signals(RDebug *dbg) {
 #if __KFBSD__
 	return bsd_handle_signals (dbg);
diff --git a/libr/include/r_vector.h b/libr/include/r_vector.h
index a01d81c965..fa4200cf2d 100644
--- a/libr/include/r_vector.h
+++ b/libr/include/r_vector.h
@@ -2,6 +2,7 @@
 #define R2_VECTOR_H
 
 #include <r_types.h>
+#include <r_util/r_assert.h>
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -69,6 +70,7 @@ R_API void r_vector_free(RVector *vec);
 R_API RVector *r_vector_clone(RVector *vec);
 
 static inline bool r_vector_empty(const RVector *vec) {
+	r_return_val_if_fail (vec, false);
 	return vec->len == 0;
 }
 
@@ -122,11 +124,11 @@ R_API void *r_vector_shrink(RVector *vec);
  * }
  */
 #define r_vector_foreach(vec, it) \
-	if ((vec) && (vec)->a) \
+	if (!r_vector_empty (vec)) \
 		for (it = (void *)(vec)->a; (char *)it != (char *)(vec)->a + ((vec)->len * (vec)->elem_size); it = (void *)((char *)it + (vec)->elem_size))
 
 #define r_vector_enumerate(vec, it, i) \
-	if ((vec) && (vec)->a) \
+	if (!r_vector_empty (vec)) \
 		for (it = (void *)(vec)->a, i = 0; i < (vec)->len; it = (void *)((char *)it + (vec)->elem_size), i++)
 
 
diff --git a/libr/io/p/io_debug.c b/libr/io/p/io_debug.c
index 01398887e0..4a3e1f3180 100644
--- a/libr/io/p/io_debug.c
+++ b/libr/io/p/io_debug.c
@@ -219,6 +219,7 @@ static void trace_me (void) {
 }
 #endif
 
+#if __APPLE__ && !__POWERPC__
 static void handle_posix_error(int err) {
 	switch (err) {
 	case 0:
@@ -236,6 +237,7 @@ static void handle_posix_error(int err) {
 		break;
 	}
 }
+#endif
 
 static RRunProfile* _get_run_profile(RIO *io, int bits, char **argv) {
 	char *expr = NULL;
diff --git a/shlr/grub/fs/hfsplus.c b/shlr/grub/fs/hfsplus.c
index d529f395e8..651a5de7e9 100644
--- a/shlr/grub/fs/hfsplus.c
+++ b/shlr/grub/fs/hfsplus.c
@@ -122,7 +122,7 @@ struct grub_hfsplus_catkey
   grub_uint16_t keylen;
   grub_uint32_t parent;
   grub_uint16_t namelen;
-  grub_uint16_t name[30];
+  grub_unaligned_uint16_t name[30];
 });
 
 /* The on disk layout of an extent overflow file key.  */
diff --git a/shlr/grub/fs/jfs.c b/shlr/grub/fs/jfs.c
index e032041ca3..3586fd1a75 100644
--- a/shlr/grub/fs/jfs.c
+++ b/shlr/grub/fs/jfs.c
@@ -147,7 +147,7 @@ struct grub_jfs_leaf_dirent
 
   /* The size of the name.  */
   grub_uint8_t len;
-  grub_uint16_t namepart[11];
+  grub_unaligned_uint16_t namepart[11];
   grub_uint32_t index;
 });
 
@@ -159,7 +159,7 @@ struct grub_jfs_leaf_next_dirent
 {
   grub_uint8_t next;
   grub_uint8_t len;
-  grub_uint16_t namepart[15];
+  grub_unaligned_uint16_t namepart[15];
 });
 
 R_PACKED (
diff --git a/shlr/grub/fs/xfs.c b/shlr/grub/fs/xfs.c
index 776dd310c4..781109e07b 100644
--- a/shlr/grub/fs/xfs.c
+++ b/shlr/grub/fs/xfs.c
@@ -84,7 +84,7 @@ struct grub_xfs_dir2_entry
   grub_uint8_t len;
 });
 
-typedef grub_uint32_t grub_xfs_extent[4];
+typedef grub_unaligned_uint32_t grub_xfs_extent[4];
 
 R_PACKED (
 struct grub_xfs_btree_node
@@ -102,7 +102,7 @@ struct grub_xfs_btree_root
 {
   grub_uint16_t level;
   grub_uint16_t numrecs;
-  grub_uint64_t keys[1];
+  grub_unaligned_uint64_t keys[1];
 });
 
 R_PACKED (
@@ -248,7 +248,7 @@ grub_xfs_read_block (grub_fshelp_node_t node, grub_disk_addr_t fileblock)
 
   if (node->inode.format == XFS_INODE_FORMAT_BTREE)
     {
-      grub_uint64_t *keys;
+      grub_unaligned_uint64_t *keys;
 
       leaf = grub_malloc (node->data->sblock.bsize);
       if (leaf == 0)
@@ -555,11 +555,11 @@ grub_xfs_iterate_dir (grub_fshelp_node_t dir,
 			  - (int) sizeof (struct grub_xfs_dir2_entry)))
 	      {
 		struct grub_xfs_dir2_entry *direntry;
-		grub_uint16_t *freetag;
+		grub_unaligned_uint16_t *freetag;
 		char *filename;
 
 		direntry = (struct grub_xfs_dir2_entry *) &dirblock[pos];
-		freetag = (grub_uint16_t *) direntry;
+		freetag = (grub_unaligned_uint16_t *) direntry;
 
 		if (*freetag == 0XFFFF)
 		  {
diff --git a/shlr/grub/include/grub/charset.h b/shlr/grub/include/grub/charset.h
index fc050da24f..4e572a91d4 100644
--- a/shlr/grub/include/grub/charset.h
+++ b/shlr/grub/include/grub/charset.h
@@ -49,7 +49,7 @@ grub_utf8_to_utf16 (grub_uint16_t *dest, grub_size_t destsize,
 
 /* Convert UTF-16 to UTF-8.  */
 static inline grub_uint8_t *
-grub_utf16_to_utf8 (grub_uint8_t *dest, grub_uint16_t *src,
+grub_utf16_to_utf8 (grub_uint8_t *dest, grub_unaligned_uint16_t *src,
 		    grub_size_t size)
 {
   grub_uint32_t code_high = 0;
diff --git a/shlr/grub/include/grub/hfs.h b/shlr/grub/include/grub/hfs.h
index 5c9d1f713f..4ee30dccbe 100644
--- a/shlr/grub/include/grub/hfs.h
+++ b/shlr/grub/include/grub/hfs.h
@@ -33,7 +33,11 @@ struct grub_hfs_extent
 };
 
 /* HFS stores extents in groups of 3.  */
-typedef struct grub_hfs_extent grub_hfs_datarecord_t[3];
+typedef struct grub_hfs_extent grub_hfs_datarecord_t[3]
+#ifdef __GNUC__
+  __attribute__((aligned(1)))
+#endif
+;
 
 /* The HFS superblock (The official name is `Master Directory
    Block').  */
diff --git a/shlr/grub/include/grub/types.h b/shlr/grub/include/grub/types.h
index 2f5e7ea55d..b16dad09b0 100644
--- a/shlr/grub/include/grub/types.h
+++ b/shlr/grub/include/grub/types.h
@@ -119,6 +119,16 @@ typedef grub_uint64_t	grub_off_t;
 /* The type for representing a disk block address.  */
 typedef grub_uint64_t	grub_disk_addr_t;
 
+#ifdef __GNUC__
+typedef grub_uint64_t grub_unaligned_uint64_t __attribute__((aligned(1)));
+typedef grub_uint32_t grub_unaligned_uint32_t __attribute__((aligned(1)));
+typedef grub_uint16_t grub_unaligned_uint16_t __attribute__((aligned(1)));
+#else
+typedef grub_uint64_t grub_unaligned_uint64_t;
+typedef grub_uint32_t grub_unaligned_uint32_t;
+typedef grub_uint16_t grub_unaligned_uint16_t;
+#endif
+
 /* Byte-orders.  */
 #ifdef _MSC_VER
 __inline grub_uint16_t grub_swap_bytes16 (grub_uint16_t x)
diff --git a/shlr/grub/partmap/sun.c b/shlr/grub/partmap/sun.c
index ce36a17408..5556b42912 100644
--- a/shlr/grub/partmap/sun.c
+++ b/shlr/grub/partmap/sun.c
@@ -75,7 +75,7 @@ struct grub_partition_map grub_sun_partition_map;
 static int
 grub_sun_is_valid (struct grub_sun_block *label)
 {
-  grub_uint16_t *pos;
+  grub_unaligned_uint16_t *pos;
   grub_uint16_t sum = 0;
 
   for (pos = (grub_uint16_t *) label;
diff --git a/shlr/grub/partmap/sunpc.c b/shlr/grub/partmap/sunpc.c
index 6e7916544e..4a0885ab7c 100644
--- a/shlr/grub/partmap/sunpc.c
+++ b/shlr/grub/partmap/sunpc.c
@@ -56,7 +56,7 @@ struct grub_partition_map grub_sun_pc_partition_map;
 static int
 grub_sun_is_valid (struct grub_sun_pc_block *label)
 {
-  grub_uint16_t *pos;
+  grub_unaligned_uint16_t *pos;
   grub_uint16_t sum = 0;
 
   for (pos = (grub_uint16_t *) label;
diff --git a/test/unit/test_anal_function.c b/test/unit/test_anal_function.c
index 1239dc09cc..cfad24f92f 100644
--- a/test/unit/test_anal_function.c
+++ b/test/unit/test_anal_function.c
@@ -52,7 +52,7 @@ bool test_r_anal_function_relocate() {
 	assert_invariants (anal);
 	RAnalFunction *fb = r_anal_create_function (anal, "do_something_else", 0xdeadbeef, 0, NULL);
 	assert_invariants (anal);
-	RAnalFunction *fc = r_anal_create_function (anal, "do_something_different", 0xc0ffee, 0, NULL);
+	r_anal_create_function (anal, "do_something_different", 0xc0ffee, 0, NULL);
 	assert_invariants (anal);
 
 	bool success = r_anal_function_relocate (fa, fb->addr);
diff --git a/test/unit/test_buf.c b/test/unit/test_buf.c
index 6bde4a7a40..7f5a5fb1d3 100644
--- a/test/unit/test_buf.c
+++ b/test/unit/test_buf.c
@@ -299,7 +299,6 @@ bool test_r_buf_format(void) {
 }
 
 bool test_r_buf_with_buf(void) {
-	char filename[] = "r2-XXXXXX";
 	const char *content = "Something To\nSay Here..";
 	const int length = 23;
 	RBuffer *buf = r_buf_new_with_bytes ((ut8 *)content, length);
@@ -318,7 +317,6 @@ bool test_r_buf_with_buf(void) {
 }
 
 bool test_r_buf_slice(void) {
-	char filename[] = "r2-XXXXXX";
 	const char *content = "AAAAAAAAAASomething To\nSay Here..BBBBBBBBBB";
 	const int length = strlen (content);
 	RBuffer *buf = r_buf_new_with_bytes ((ut8 *)content, length);