Some memory issues

Hello all,

Just in case, I like to share some memory issues discovered by me.

  1. Resource leak (s)
    Location: ./platforms/nuttx/NuttX/apps/canutils/cansend/cansend.c
    Description: there is not releasing allocated resource at line 117
	/* open socket */
	if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
		perror("socket");
		return 1;
	}

	strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
	ifr.ifr_name[IFNAMSIZ - 1] = '\0';
	ifr.ifr_ifindex = if_nametoindex(ifr.ifr_name);
	if (!ifr.ifr_ifindex) {
		perror("if_nametoindex");
		
		// ERROR
		return 1;
	}

  1. Idempotent assignment
    Location: ./src/drivers/magnetometer/lsm9ds1_mag/LSM9DS1_MAG.cpp
    Description: Redundant assignment of ‘y’ to itself at line 179

// sensor Z is up (RHC), flip z for publication
// sensor X is aligned with -X of lsm9ds1 accel/gyro
x = (x == INT16_MIN) ? INT16_MAX : -x;
y = y; // Idempotent assignment
z = (z == INT16_MIN) ? INT16_MAX : -z;


  1. Dereferencing deallocated data
    Location: ./src./drivers/px4io/px4io.cpp

Description: dereferencing ‘interface’ after it is deallocated at line 3151

#ifdef PX4IO_SERIAL_BASE
interface = PX4IO_serial_interface();
#endif

if (interface != nullptr) {
goto got;
}

errx(1, “cannot alloc interface”);

got:

if (interface->init() != OK) {
delete interface;
errx(1, “interface init failed”);
}

// ERROR
return interface;


  1. Resource leak (file_buf)
    Location: ./src/drivers/tap_esc/tap_esc_uploader.cpp
    Description: there is not releasing allocated resource at line 1113

file_buf = new uint8_t[PROG_MULTI_MAX];

PX4_DEBUG(“verify…”);
lseek(_fw_fd, 0, SEEK_SET);

ret = get_device_info(esc_id, PROTO_GET_DEVICE, PROTO_DEVICE_FW_SIZE, fw_size_remote);

if (ret != OK) {
PX4_DEBUG(“could not read firmware size”);

  // ERROR
  return ret;

}

It would be nice if these memory issues will be resolved. Thanks!

@HyungsubKim Would you be able to make a PR with the fix?

@Jaeyoung-Lim
Sure! I like to make a PR!