diff mbox series

Remove uninitialized_var() macro from the emulator.

Message ID c3894e4c-4c4d-63dd-85d0-439fd5de30a6@fourwalledcubicle.com (mailing list archive)
State New, archived
Headers show
Series Remove uninitialized_var() macro from the emulator. | expand

Commit Message

Dean Camera Nov. 21, 2020, 8:14 a.m. UTC
Several emulator modules contain a uninitialized_var(x) macro, whose 
entire purpose appears to be confusing old compilers into not generating 
valid warnings about uninitialized variables being used.

This patch removes these variables, and adds error handling for the 
enums for cases which are not explicitly handled. It's better to error 
out with a message and known state on unidentified input, than to carry 
on with potentially uninitialized garbage (this can also lead to 
incorrect optimizations on more modern compilers, which have flow 
control analysis built into the optimizer).
---
  emulator/serial.c | 8 ++++----
  emulator/server.c | 9 ++++++---
  emulator/vhci.c   | 9 +++++----
  3 files changed, 15 insertions(+), 11 deletions(-)


@@ -107,6 +105,9 @@ struct vhci *vhci_open(enum vhci_type type)
  		btdev_type = BTDEV_TYPE_AMP;
  		ctrl_type = HCI_AMP;
  		break;
+	default:
+		printf("Unknown vhci type %d\n", type);
+		return NULL;
  	}

  	vhci = malloc(sizeof(*vhci));
diff mbox series

Patch

diff --git a/emulator/serial.c b/emulator/serial.c
index b44af0dcc..04ce26bf0 100644
--- a/emulator/serial.c
+++ b/emulator/serial.c
@@ -32,8 +32,6 @@ 
  #include "btdev.h"
  #include "serial.h"

-#define uninitialized_var(x) x = x
-
  struct serial {
  	enum serial_type type;
  	uint16_t id;
@@ -147,7 +145,7 @@  again:

  static void open_pty(struct serial *serial)
  {
-	enum btdev_type uninitialized_var(type);
+	enum btdev_type type;

  	serial->fd = posix_openpt(O_RDWR | O_NOCTTY);
  	if (serial->fd < 0) {
@@ -186,6 +184,9 @@  static void open_pty(struct serial *serial)
  	case SERIAL_TYPE_AMP:
  		type = BTDEV_TYPE_AMP;
  		break;
+	default:
+		printf("Unknown serial type %d\n", serial->type);
+		return;
  	}

  	serial->btdev = btdev_create(type, serial->id);
@@ -210,7 +211,6 @@  static void open_pty(struct serial *serial)
  struct serial *serial_open(enum serial_type type)
  {
  	struct serial *serial;
-	enum btdev_type uninitialized_var(dev_type);

  	serial = malloc(sizeof(*serial));
  	if (!serial)
diff --git a/emulator/server.c b/emulator/server.c
index 3b07a7156..3643b4139 100644
--- a/emulator/server.c
+++ b/emulator/server.c
@@ -33,8 +33,6 @@ 
  #include "btdev.h"
  #include "server.h"

-#define uninitialized_var(x) x = x
-
  struct server {
  	enum server_type type;
  	uint16_t id;
@@ -197,7 +195,7 @@  static void server_accept_callback(int fd, uint32_t 
events, void *user_data)
  {
  	struct server *server = user_data;
  	struct client *client;
-	enum btdev_type uninitialized_var(type);
+	enum btdev_type type;

  	if (events & (EPOLLERR | EPOLLHUP)) {
  		mainloop_remove_fd(server->fd);
@@ -231,6 +229,11 @@  static void server_accept_callback(int fd, uint32_t 
events, void *user_data)
  		break;
  	case SERVER_TYPE_MONITOR:
  		goto done;
+	default:
+		printf("Unknown btdev type %d\n", server->type);
+		close(client->fd);
+		free(client);
+		return;
  	}

  	client->btdev = btdev_create(type, server->id);
diff --git a/emulator/vhci.c b/emulator/vhci.c
index 84e16330f..0227bc091 100644
--- a/emulator/vhci.c
+++ b/emulator/vhci.c
@@ -29,8 +29,6 @@ 
  #include "btdev.h"
  #include "vhci.h"

-#define uninitialized_var(x) x = x
-
  struct vhci {
  	enum vhci_type type;
  	int fd;
@@ -85,8 +83,8 @@  static void vhci_read_callback(int fd, uint32_t 
events, void *user_data)
  struct vhci *vhci_open(enum vhci_type type)
  {
  	struct vhci *vhci;
-	enum btdev_type uninitialized_var(btdev_type);
-	unsigned char uninitialized_var(ctrl_type);
+	enum btdev_type btdev_type;
+	unsigned char ctrl_type;
  	unsigned char setup_cmd[2];
  	static uint8_t id = 0x23;