Skip to content

Commit ead196e

Browse files
authored
Enable noUncheckedIndexAccess (#1065)
One of the two most important TypeScript rules, imo. Accessing indexed types (maps, arrays, etc.) in JavaScript doesn't ever throw an out of bounds error, but it might return `undefined` - and TypeScript by default doesn't check against this because so much JavaScript code in the world is written sloppily enough that checking this by default would make it hard to port to TypeScript.
1 parent 1459012 commit ead196e

File tree

13 files changed

+69
-54
lines changed

13 files changed

+69
-54
lines changed

src/core/Ros.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,11 @@ export default class Ros extends EventEmitter<
646646
const arrayLen = theType.fieldarraylen[i];
647647
const fieldName = theType.fieldnames[i];
648648
const fieldType = theType.fieldtypes[i];
649+
if (fieldName === undefined || fieldType === undefined) {
650+
throw new Error(
651+
"Received mismatched type definition vector lengths!",
652+
);
653+
}
649654
if (!fieldType.includes("/")) {
650655
// check the fieldType includes '/' or not
651656
if (arrayLen === -1) {
@@ -677,7 +682,11 @@ export default class Ros extends EventEmitter<
677682
return typeDefDict;
678683
};
679684

680-
return decodeTypeDefsRec(defs[0], defs);
685+
if (defs[0]) {
686+
return decodeTypeDefsRec(defs[0], defs);
687+
} else {
688+
return {};
689+
}
681690
}
682691

683692
/**

src/urdf/UrdfBox.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default class UrdfBox {
2222
const size: Optional<string[]> = xml
2323
.getAttribute(UrdfAttrs.Size)
2424
?.split(" ");
25-
if (size?.length !== 3) {
25+
if (!(size?.[0] && size[1] && size[2])) {
2626
return;
2727
}
2828

src/urdf/UrdfColor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default class UrdfColor {
3333
const rgba: Optional<string[]> = xml
3434
.getAttribute(UrdfAttrs.Rgba)
3535
?.split(" ");
36-
if (rgba?.length !== 4) {
36+
if (!(rgba?.[0] && rgba[1] && rgba[2] && rgba[3])) {
3737
return;
3838
}
3939

src/urdf/UrdfJoint.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ export default class UrdfJoint {
3131
this.type = xml.getAttribute(UrdfAttrs.Type);
3232

3333
const parents = xml.getElementsByTagName(UrdfAttrs.Parent);
34-
if (parents.length > 0) {
34+
if (parents[0]) {
3535
this.parent = parents[0].getAttribute(UrdfAttrs.Link);
3636
}
3737

3838
const children = xml.getElementsByTagName(UrdfAttrs.Child);
39-
if (children.length > 0) {
39+
if (children[0]) {
4040
this.child = children[0].getAttribute(UrdfAttrs.Link);
4141
}
4242

4343
const limits = xml.getElementsByTagName(UrdfAttrs.Limit);
44-
if (limits.length > 0) {
44+
if (limits[0]) {
4545
this.minval = parseFloat(
4646
limits[0].getAttribute(UrdfAttrs.Lower) ?? "NaN",
4747
);
@@ -52,12 +52,12 @@ export default class UrdfJoint {
5252

5353
// Origin
5454
const origins = xml.getElementsByTagName(UrdfAttrs.Origin);
55-
if (origins.length > 0) {
55+
if (origins[0]) {
5656
this.origin = parseUrdfOrigin(origins[0]);
5757
}
5858

5959
const axis = xml.getElementsByTagName(UrdfAttrs.Axis);
60-
if (axis.length > 0) {
60+
if (axis[0]) {
6161
const xyzValue = axis[0].getAttribute(UrdfAttrs.Xyz)?.split(" ");
6262
if (!xyzValue || xyzValue.length !== 3) {
6363
throw new Error(

src/urdf/UrdfMaterial.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ export default class UrdfMaterial {
2121

2222
// Texture
2323
const textures = xml.getElementsByTagName(UrdfAttrs.Texture);
24-
if (textures.length > 0) {
24+
if (textures[0]) {
2525
this.textureFilename = textures[0].getAttribute(UrdfAttrs.Filename);
2626
}
2727

2828
// Color
2929
const colors = xml.getElementsByTagName(UrdfAttrs.Color);
30-
if (colors.length > 0) {
30+
if (colors[0]) {
3131
// Parse the RBGA string
3232
this.color = new UrdfColor({
3333
xml: colors[0],

src/urdf/UrdfMesh.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default class UrdfMesh {
2424
const scale: Optional<string[]> = xml
2525
.getAttribute(UrdfAttrs.Scale)
2626
?.split(" ");
27-
if (scale?.length !== 3) {
27+
if (!(scale?.[0] && scale[1] && scale[2])) {
2828
return;
2929
}
3030

src/urdf/UrdfModel.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ export default class UrdfModel {
7272
break;
7373
}
7474

75-
if (this.materials[material.name].isLink()) {
76-
this.materials[material.name].assign(material);
75+
const existingMaterial = this.materials[material.name];
76+
if (existingMaterial?.isLink()) {
77+
existingMaterial.assign(material);
7778
} else {
7879
console.warn(`Material ${material.name} is not unique.`);
7980
}
@@ -95,8 +96,9 @@ export default class UrdfModel {
9596
continue;
9697
}
9798

98-
if (Object.hasOwn(this.materials, mat.name)) {
99-
item.material = this.materials[mat.name];
99+
const material = this.materials[mat.name];
100+
if (material) {
101+
item.material = material;
100102
} else {
101103
this.materials[mat.name] = mat;
102104
}

src/urdf/UrdfUtils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function parseUrdfOrigin(originElement: Element): Pose {
1111
.getAttribute(UrdfAttrs.Xyz)
1212
?.split(" ");
1313
let position: Vector3 = new Vector3();
14-
if (xyz?.length === 3) {
14+
if (xyz?.[0] && xyz[1] && xyz[2]) {
1515
position = new Vector3({
1616
x: parseFloat(xyz[0]),
1717
y: parseFloat(xyz[1]),
@@ -22,7 +22,7 @@ export function parseUrdfOrigin(originElement: Element): Pose {
2222
// Check the RPY
2323
const rpy = originElement.getAttribute(UrdfAttrs.Rpy)?.split(" ");
2424
let orientation = new Quaternion();
25-
if (rpy?.length === 3) {
25+
if (rpy?.[0] && rpy[1] && rpy[2]) {
2626
// Convert from RPY
2727
const roll = parseFloat(rpy[0]);
2828
const pitch = parseFloat(rpy[1]);

src/urdf/UrdfVisual.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,19 @@ export default class UrdfVisual {
6363

6464
// Origin
6565
const origins = xml.getElementsByTagName(UrdfAttrs.Origin);
66-
if (origins.length > 0) {
66+
if (origins[0]) {
6767
this.origin = parseUrdfOrigin(origins[0]);
6868
}
6969

7070
// Geometry
7171
const geoms = xml.getElementsByTagName(UrdfAttrs.Geometry);
72-
if (geoms.length > 0) {
72+
if (geoms[0]) {
7373
this.geometry = parseUrdfGeometry(geoms[0]);
7474
}
7575

7676
// Material
7777
const materials = xml.getElementsByTagName(UrdfAttrs.Material);
78-
if (materials.length > 0) {
78+
if (materials[0]) {
7979
this.material = new UrdfMaterial({
8080
xml: materials[0],
8181
});

src/util/cborTypedArrayTags.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ function decodeUint64LE(bytes: Uint8Array) {
2929
const si = i * 2;
3030
const lo = uint32View[si];
3131
const hi = uint32View[si + 1];
32+
if (lo === undefined || hi === undefined) {
33+
throw new Error("Invalid byte array");
34+
}
3235
arr[i] = lo + UPPER32 * hi;
3336
}
3437

@@ -55,6 +58,9 @@ function decodeInt64LE(bytes: Uint8Array) {
5558
const si = i * 2;
5659
const lo = uint32View[si];
5760
const hi = int32View[si + 1];
61+
if (lo === undefined || hi === undefined) {
62+
throw new Error("Invalid byte array");
63+
}
5864
arr[i] = lo + UPPER32 * hi;
5965
}
6066

@@ -120,12 +126,12 @@ export default function cborTypedArrayTagger(
120126
data: Uint8Array<ArrayBuffer>,
121127
tag: number,
122128
) {
123-
if (tag in nativeArrayTypes) {
124-
const arrayType = nativeArrayTypes[tag];
129+
const arrayType = nativeArrayTypes[tag];
130+
if (arrayType) {
125131
return decodeNativeArray(data, arrayType);
126132
}
127133
if (tag in conversionArrayTypes) {
128-
return conversionArrayTypes[tag](data);
134+
return conversionArrayTypes[tag]?.(data);
129135
}
130136
return data;
131137
}

0 commit comments

Comments
 (0)