Skip to content

Commit

Permalink
fix constant overflow in 32bit architectures (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Jan 30, 2022
1 parent 8c7c9e1 commit d459710
Show file tree
Hide file tree
Showing 1,929 changed files with 1,976 additions and 1,966 deletions.
26 changes: 18 additions & 8 deletions cmd/dialect-import/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io/ioutil"
"math"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -63,7 +64,7 @@ import (
{{- range .Enum.Description }}
// {{ . }}
{{- end }}
type {{ .Enum.Name }} int
type {{ .Enum.Name }} uint32
const (
{{- $pn := .Enum.Name }}
Expand Down Expand Up @@ -183,7 +184,7 @@ func parseDescription(in string) []string {
}

type outEnumValue struct {
Value string
Value *uint32
Name string
Description []string
}
Expand Down Expand Up @@ -270,8 +271,17 @@ func processDefinition(
}

for _, val := range enum.Values {
tmp, err := strconv.ParseInt(val.Value, 10, 64)
if err != nil {
return nil, err
}
if tmp < 0 || tmp > int64(math.Pow(2, 32)) {
return nil, fmt.Errorf("enum values that overflow an uint32 are not supported")
}

v := uint32(tmp)
oute.Values = append(oute.Values, &outEnumValue{
Value: val.Value,
Value: &v,
Name: val.Name,
Description: parseDescription(val.Description),
})
Expand Down Expand Up @@ -522,13 +532,13 @@ func run() error {

// fill enum missing values
for _, enum := range enums {
nextVal := 0
nextVal := uint32(0)
for _, v := range enum.Values {
if v.Value != "" {
nextVal, _ = strconv.Atoi(v.Value)
nextVal++
if v.Value != nil {
nextVal = *v.Value + 1
} else {
v.Value = strconv.Itoa(nextVal)
n := nextVal
v.Value = &n
nextVal++
}
}
Expand Down
8 changes: 4 additions & 4 deletions node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
)

type (
MAV_TYPE int //nolint:revive
MAV_AUTOPILOT int //nolint:revive
MAV_MODE_FLAG int //nolint:revive
MAV_STATE int //nolint:revive
MAV_TYPE uint32 //nolint:revive
MAV_AUTOPILOT uint32 //nolint:revive
MAV_MODE_FLAG uint32 //nolint:revive
MAV_STATE uint32 //nolint:revive
)

type MessageHeartbeat struct {
Expand Down
8 changes: 4 additions & 4 deletions nodeheartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ func (h *nodeHeartbeat) run() {
select {
case <-ticker.C:
m := reflect.New(reflect.TypeOf(h.msgHeartbeat).Elem())
m.Elem().FieldByName("Type").SetInt(int64(h.n.conf.HeartbeatSystemType))
m.Elem().FieldByName("Autopilot").SetInt(int64(h.n.conf.HeartbeatAutopilotType))
m.Elem().FieldByName("BaseMode").SetInt(0)
m.Elem().FieldByName("Type").SetUint(uint64(h.n.conf.HeartbeatSystemType))
m.Elem().FieldByName("Autopilot").SetUint(uint64(h.n.conf.HeartbeatAutopilotType))
m.Elem().FieldByName("BaseMode").SetUint(0)
m.Elem().FieldByName("CustomMode").SetUint(0)
m.Elem().FieldByName("SystemStatus").SetInt(4) // MAV_STATE_ACTIVE
m.Elem().FieldByName("SystemStatus").SetUint(4) // MAV_STATE_ACTIVE
m.Elem().FieldByName("MavlinkVersion").SetUint(uint64(h.n.conf.Dialect.Version))
h.n.WriteMessageAll(m.Interface().(msg.Message))

Expand Down
2 changes: 1 addition & 1 deletion nodestreamrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (sr *nodeStreamRequest) run() {
func (sr *nodeStreamRequest) onEventFrame(evt *EventFrame) {
// message must be heartbeat and sender must be an ardupilot device
if evt.Message().GetID() != 0 ||
reflect.ValueOf(evt.Message()).Elem().FieldByName("Autopilot").Int() != 3 {
reflect.ValueOf(evt.Message()).Elem().FieldByName("Autopilot").Uint() != 3 {
return
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/dialect/decencoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
)

type (
MAV_TYPE int //nolint:revive
MAV_AUTOPILOT int //nolint:revive
MAV_MODE_FLAG int //nolint:revive
MAV_STATE int //nolint:revive
MAV_TYPE uint32 //nolint:revive
MAV_AUTOPILOT uint32 //nolint:revive
MAV_MODE_FLAG uint32 //nolint:revive
MAV_STATE uint32 //nolint:revive
)

type MessageHeartbeat struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_accelcal_vehicle_pos.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"errors"
)

type ACCELCAL_VEHICLE_POS int
type ACCELCAL_VEHICLE_POS uint32

const (
ACCELCAL_VEHICLE_POS_LEVEL ACCELCAL_VEHICLE_POS = 1
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_actuator_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Actuator configuration, used to change a setting on an actuator. Component information metadata can be used to know which outputs support which commands.
type ACTUATOR_CONFIGURATION int
type ACTUATOR_CONFIGURATION uint32

const (
// Do nothing.
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_actuator_output_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Actuator output function. Values greater or equal to 1000 are autopilot-specific.
type ACTUATOR_OUTPUT_FUNCTION int
type ACTUATOR_OUTPUT_FUNCTION uint32

const (
// No function (disabled).
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_adsb_altitude_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Enumeration of the ADSB altimeter types
type ADSB_ALTITUDE_TYPE int
type ADSB_ALTITUDE_TYPE uint32

const (
// Altitude reported from a Baro source using QNH reference
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_adsb_emitter_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// ADSB classification for the type of vehicle emitting the transponder signal
type ADSB_EMITTER_TYPE int
type ADSB_EMITTER_TYPE uint32

const (
ADSB_EMITTER_TYPE_NO_INFO ADSB_EMITTER_TYPE = 0
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_adsb_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// These flags indicate status such as data validity of each data source. Set = data valid
type ADSB_FLAGS int
type ADSB_FLAGS uint32

const (
ADSB_FLAGS_VALID_COORDS ADSB_FLAGS = 1
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_airspeed_sensor_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Types of airspeed sensor/data. May be be used in AIRSPEED message to estimate accuracy of indicated speed.
type AIRSPEED_SENSOR_TYPE int
type AIRSPEED_SENSOR_TYPE uint32

const (
// Airspeed sensor type unknown/not supplied.
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_ais_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// These flags are used in the AIS_VESSEL.fields bitmask to indicate validity of data in the other message fields. When set, the data is valid.
type AIS_FLAGS int
type AIS_FLAGS uint32

const (
// 1 = Position accuracy less than 10m, 0 = position accuracy greater than 10m.
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_ais_nav_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Navigational status of AIS vessel, enum duplicated from AIS standard, https://gpsd.gitlab.io/gpsd/AIVDM.html
type AIS_NAV_STATUS int
type AIS_NAV_STATUS uint32

const (
// Under way using engine.
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_ais_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Type of AIS vessel, enum duplicated from AIS standard, https://gpsd.gitlab.io/gpsd/AIVDM.html
type AIS_TYPE int
type AIS_TYPE uint32

const (
// Not available (default).
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_attitude_target_typemask.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Bitmap to indicate which dimensions should be ignored by the vehicle: a value of 0b00000000 indicates that none of the setpoint dimensions should be ignored.
type ATTITUDE_TARGET_TYPEMASK int
type ATTITUDE_TARGET_TYPEMASK uint32

const (
// Ignore body roll rate
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_autotune_axis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Enable axes that will be tuned via autotuning. Used in MAV_CMD_DO_AUTOTUNE_ENABLE.
type AUTOTUNE_AXIS int
type AUTOTUNE_AXIS uint32

const (
// Flight stack tunes axis according to its default settings.
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_camera_cap_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Camera capability flags (Bitmap)
type CAMERA_CAP_FLAGS int
type CAMERA_CAP_FLAGS uint32

const (
// Camera is able to record video
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_camera_feedback_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"errors"
)

type CAMERA_FEEDBACK_FLAGS int
type CAMERA_FEEDBACK_FLAGS uint32

const (
// Shooting photos, not video.
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_camera_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Camera Modes.
type CAMERA_MODE int
type CAMERA_MODE uint32

const (
// Camera is in image/photo capture mode.
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_camera_status_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"errors"
)

type CAMERA_STATUS_TYPES int
type CAMERA_STATUS_TYPES uint32

const (
// Camera heartbeat, announce camera component ID at 1Hz.
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_camera_tracking_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Camera tracking modes
type CAMERA_TRACKING_MODE int
type CAMERA_TRACKING_MODE uint32

const (
// Not tracking
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_camera_tracking_status_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Camera tracking status flags
type CAMERA_TRACKING_STATUS_FLAGS int
type CAMERA_TRACKING_STATUS_FLAGS uint32

const (
// Camera is not tracking
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_camera_tracking_target_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Camera tracking target data (shows where tracked target is within image)
type CAMERA_TRACKING_TARGET_DATA int
type CAMERA_TRACKING_TARGET_DATA uint32

const (
// No target data
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_camera_zoom_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Zoom types for MAV_CMD_SET_CAMERA_ZOOM
type CAMERA_ZOOM_TYPE int
type CAMERA_ZOOM_TYPE uint32

const (
// Zoom one step increment (-1 for wide, 1 for tele)
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_cellular_config_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Possible responses from a CELLULAR_CONFIG message.
type CELLULAR_CONFIG_RESPONSE int
type CELLULAR_CONFIG_RESPONSE uint32

const (
// Changes accepted.
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_cellular_network_failed_reason.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// These flags are used to diagnose the failure state of CELLULAR_STATUS
type CELLULAR_NETWORK_FAILED_REASON int
type CELLULAR_NETWORK_FAILED_REASON uint32

const (
// No error
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_cellular_network_radio_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Cellular network radio type
type CELLULAR_NETWORK_RADIO_TYPE int
type CELLULAR_NETWORK_RADIO_TYPE uint32

const (
CELLULAR_NETWORK_RADIO_TYPE_NONE CELLULAR_NETWORK_RADIO_TYPE = 0
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_cellular_status_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// These flags encode the cellular network status
type CELLULAR_STATUS_FLAG int
type CELLULAR_STATUS_FLAG uint32

const (
// State unknown or not reportable.
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_comp_metadata_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Supported component metadata types. These are used in the "general" metadata file returned by COMPONENT_INFORMATION to provide information about supported metadata types. The types are not used directly in MAVLink messages.
type COMP_METADATA_TYPE int
type COMP_METADATA_TYPE uint32

const (
// General information about the component. General metadata includes information about other COMP_METADATA_TYPEs supported by the component. This type must be supported and must be downloadable from vehicle.
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_copter_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// A mapping of copter flight modes for custom_mode field of heartbeat.
type COPTER_MODE int
type COPTER_MODE uint32

const (
COPTER_MODE_STABILIZE COPTER_MODE = 0
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_deepstall_stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Deepstall flight stage.
type DEEPSTALL_STAGE int
type DEEPSTALL_STAGE uint32

const (
// Flying to the landing point.
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_device_op_bustype.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Bus types for device operations.
type DEVICE_OP_BUSTYPE int
type DEVICE_OP_BUSTYPE uint32

const (
// I2C Device operation.
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_ekf_status_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Flags in EKF_STATUS message.
type EKF_STATUS_FLAGS int
type EKF_STATUS_FLAGS uint32

const (
// Set if EKF's attitude estimate is good.
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_esc_connection_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Indicates the ESC connection type.
type ESC_CONNECTION_TYPE int
type ESC_CONNECTION_TYPE uint32

const (
// Traditional PPM ESC.
Expand Down
2 changes: 1 addition & 1 deletion pkg/dialects/all/enum_esc_failure_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Flags to report ESC failures.
type ESC_FAILURE_FLAGS int
type ESC_FAILURE_FLAGS uint32

const (
// No ESC failure.
Expand Down
Loading

0 comments on commit d459710

Please sign in to comment.