Skip to content

Commit

Permalink
Merge pull request #52 from jet2jet/feature/add_nullish_check
Browse files Browse the repository at this point in the history
Add nullish check to indexed access
  • Loading branch information
jet2jet committed Apr 19, 2024
2 parents 906487c + 9de6142 commit 7c6a390
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 73 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main-ci.yml
Expand Up @@ -30,6 +30,8 @@ jobs:
- name: Install
if: steps.node_modules_cache.outputs.cache-hit != 'true'
run: npm ci
- name: Type check
run: npm run typecheck
- name: Lint check
run: npm run lint
- name: Test
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -54,7 +54,8 @@
"test:lint": "npm run lint",
"test:win-x86": "npm run test-base -- --config ./jest.config.win-x86.js",
"test:win-x64": "npm run test-base -- --config ./jest.config.win-x64.js",
"version": "node ./tools/updateVersion.js ./src/main/version.ts && git add -A ./src/main/version.ts"
"version": "node ./tools/updateVersion.js ./src/main/version.ts && git add -A ./src/main/version.ts",
"typecheck": "tsc --project tsconfig.json --noEmit && echo \"Done\"."
},
"dependencies": {
"pe-library": "^1.0.0"
Expand Down
5 changes: 2 additions & 3 deletions src/main/resource/IconGroupEntry.ts
Expand Up @@ -87,8 +87,7 @@ function findUnusedIconID(
.map((e) => e.id)
.sort((a, b) => a - b);
let idCurrent = 1;
for (let i = 0; i < filteredIDs.length; ++i) {
const id = filteredIDs[i];
for (const id of filteredIDs) {
if (idCurrent < id) {
return {
id: idCurrent,
Expand Down Expand Up @@ -300,7 +299,7 @@ export default class IconGroupEntry {
// remove unused icon data
for (let i = destEntries.length - 1; i >= 0; --i) {
const e = destEntries[i];
if (e.type === 3) {
if (e != null && e.type === 3) {
// RT_ICON
if (!isIconUsed(e, destEntries, entry)) {
destEntries.splice(i, 1);
Expand Down
12 changes: 6 additions & 6 deletions src/main/resource/StringTable.ts
Expand Up @@ -61,7 +61,7 @@ export default class StringTable {
}
const entryIndex = id >> 4;
const entryPos = id & 15;
const e = this.items[entryIndex] as StringTableItem | undefined;
const e = this.items[entryIndex];
return e?.get(entryPos) ?? null;
}
/**
Expand All @@ -75,7 +75,7 @@ export default class StringTable {
}
const entryIndex = id >> 4;
const entryPos = id & 15;
let e = this.items[entryIndex] as StringTableItem | undefined;
let e = this.items[entryIndex];
if (!e) {
this.items[entryIndex] = e = new StringTableItem();
}
Expand Down Expand Up @@ -110,10 +110,10 @@ export default class StringTable {
// first try -- replace same type and same language
for (let i = 0; i < dest.length; ++i) {
const e = dest[i];
if (e.type === 6 && e.lang === this.lang) {
if (e != null && e.type === 6 && e.lang === this.lang) {
for (let j = dest.length - 1; j >= i; --j) {
const e2 = dest[j];
if (e2.type === 6 && e2.lang === this.lang) {
if (e2 != null && e2.type === 6 && e2.lang === this.lang) {
dest.splice(j, 1);
}
}
Expand All @@ -125,7 +125,7 @@ export default class StringTable {
// second try -- add entries next to previous language
for (let i = 0; i < dest.length; ++i) {
const e = dest[i];
if (e.type === 6 && e.lang < this.lang) {
if (e != null && e.type === 6 && e.lang < this.lang) {
const f = dest.splice.bind(dest, i + 1, 0);
f(...entries);
return;
Expand All @@ -134,7 +134,7 @@ export default class StringTable {
// third try -- add entries next to the last 'String' entry
for (let i = dest.length - 1; i >= 0; --i) {
const e = dest[i];
if (e.type === 6) {
if (e != null && e.type === 6) {
const f = dest.splice.bind(dest, i + 1, 0);
f(...entries);
return;
Expand Down
20 changes: 13 additions & 7 deletions src/main/resource/StringTableItem.ts
Expand Up @@ -35,7 +35,8 @@ export default class StringTableItem {
}

public get(index: number): string | null {
return this._a[index] || null;
const value = this._a[index];
return value != null && value !== '' ? value : null;
}
public getAll(): Array<string | null> {
return this._a.map((s) => s || null);
Expand All @@ -46,8 +47,11 @@ export default class StringTableItem {
public calcByteLength(): number {
let len = 0;
for (let i = 0; i < 16; ++i) {
const item = this._a[i];
len += 2;
len += 2 * this._a[i].length; // UTF-16 length
if (item != null) {
len += 2 * item.length; // UTF-16 length
}
}
// 16 alignment
return Math.floor((len + 15) / 16) * 16;
Expand All @@ -57,13 +61,15 @@ export default class StringTableItem {
let len = 0;
for (let i = 0; i < 16; ++i) {
const s = this._a[i];
const l = s.length > 4097 ? 4097 : s.length;
const l = s == null ? 0 : s.length > 4097 ? 4097 : s.length;
out.setUint16(len, l, true);
len += 2;
for (let j = 0; j < l; ++j) {
// output as UTF-16
out.setUint16(len, s.charCodeAt(j), true);
len += 2;
if (s != null) {
for (let j = 0; j < l; ++j) {
// output as UTF-16
out.setUint16(len, s.charCodeAt(j), true);
len += 2;
}
}
}
// 16 alignment
Expand Down
57 changes: 43 additions & 14 deletions src/main/resource/VersionInfo.ts
Expand Up @@ -204,7 +204,10 @@ function parseStringFileInfo(
} else {
// merge values
for (const key in table.values) {
a[0].values[key] = table.values[key];
const value = table.values[key];
if (value != null) {
a[0]!.values[key] = value;
}
}
}
offset = roundUp(childData[0], 4);
Expand Down Expand Up @@ -358,6 +361,9 @@ function generateStringTable(table: VersionStringTable): ArrayBuffer {
const keys = Object.keys(table.values);
size = keys.reduce((prev, key) => {
const value = table.values[key];
if (value == null) {
return prev;
}
const childHeaderSize = roundUp(6 + 2 * (key.length + 1), 4);
const newSize = roundUp(
prev + childHeaderSize + 2 * (value.length + 1),
Expand Down Expand Up @@ -389,6 +395,9 @@ function generateStringTable(table: VersionStringTable): ArrayBuffer {

keys.forEach((key) => {
const value = table.values[key];
if (value == null) {
return;
}
const childHeaderSize = roundUp(6 + 2 * (key.length + 1), 4);
const newSize = roundUp(childHeaderSize + 2 * (value.length + 1), 4);
if (offset + newSize <= 65532) {
Expand Down Expand Up @@ -558,7 +567,7 @@ function parseVersionArguments(
.split('.')
.map((token) => clampInt(Number(token), 0, 65535))
// add zeros for missing fields
.concat(0, 0, 0);
.concat(0, 0, 0) as [number, number, number, number];
lang = arg2;
} else {
major = clampInt(Number(arg1), 0, 65535);
Expand Down Expand Up @@ -636,11 +645,13 @@ export default class VersionInfo {
vi.data.lang = lang;
// copy all specified values
// (if unspecified, use default value set by `createFixedInfo`)
for (const fixedInfoKey in fixedInfo!) {
for (const _fixedInfoKey in fixedInfo!) {
const fixedInfoKey = _fixedInfoKey as keyof VersionFixedInfo;
if (fixedInfoKey in fixedInfo) {
(vi.data.fixedInfo as any)[fixedInfoKey] = (fixedInfo as any)[
fixedInfoKey
];
const value = fixedInfo[fixedInfoKey];
if (value != null) {
vi.data.fixedInfo[fixedInfoKey] = value;
}
}
}
vi.data.strings = strings!.map(
Expand Down Expand Up @@ -712,7 +723,7 @@ export default class VersionInfo {
e.lang === language.lang && e.codepage === language.codepage
)
.map((e) => e.values);
return a.length > 0 ? a[0] : {};
return a.length > 0 ? a[0]! : {};
}

/**
Expand Down Expand Up @@ -751,10 +762,13 @@ export default class VersionInfo {
};
this.data.strings.push(table);
} else {
table = a[0];
table = a[0]!;
}
for (const key in values) {
table.values[key] = values[key];
const value = values[key];
if (value != null) {
table.values[key] = value;
}
}

if (addToAvailableLanguage) {
Expand Down Expand Up @@ -805,13 +819,18 @@ export default class VersionInfo {
const len = strings.length;
for (let i = 0; i < len; ++i) {
const e = strings[i];
if (e.lang === language.lang && e.codepage === language.codepage) {
if (
e != null &&
e.lang === language.lang &&
e.codepage === language.codepage
) {
strings.splice(i, 1);
if (removeFromAvailableLanguage) {
const translations = this.data.translations;
for (let j = 0; j < translations.length; j++) {
const t = translations[j];
if (
t != null &&
t.lang === language.lang &&
t.codepage === language.codepage
) {
Expand Down Expand Up @@ -840,7 +859,11 @@ export default class VersionInfo {
const len = strings.length;
for (let i = 0; i < len; ++i) {
const e = strings[i];
if (e.lang === language.lang && e.codepage === language.codepage) {
if (
e != null &&
e.lang === language.lang &&
e.codepage === language.codepage
) {
try {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete e.values[key];
Expand All @@ -855,6 +878,7 @@ export default class VersionInfo {
for (let j = 0; j < translations.length; j++) {
const t = translations[j];
if (
t != null &&
t.lang === language.lang &&
t.codepage === language.codepage
) {
Expand Down Expand Up @@ -896,7 +920,12 @@ export default class VersionInfo {
const len = entries.length;
for (let i = 0; i < len; ++i) {
const e = entries[i];
if (e.type === 16 && e.id === res.id && e.lang === res.lang) {
if (
e != null &&
e.type === 16 &&
e.id === res.id &&
e.lang === res.lang
) {
entries[i] = res;
return;
}
Expand All @@ -915,10 +944,10 @@ export default class VersionInfo {
}
// second, use lang value for propName if there is only one language
const a = this.data.strings
.filter((e) => propName in e.values)
.filter((e) => propName in e.values && e.values[propName] != null)
.map((e) => e.lang);
if (a.length === 1) {
return a[0];
return a[0]!;
}
// use English language
return 1033;
Expand Down
15 changes: 9 additions & 6 deletions src/main/sign/certUtil.ts
Expand Up @@ -16,13 +16,16 @@ export function calculateDERLength(
offset: number
): [number, number] {
let actualLength = 0;
if (data[offset] < 0x80) {
actualLength = data[offset];
const value = data[offset];
if (value == null) {
throw new Error('Invalid "offset" value');
} else if (value < 0x80) {
actualLength = value;
++offset;
} else if (data[offset] === 0x80) {
} else if (value === 0x80) {
throw new Error('Not supported certificate data (variable length)');
} else {
let c = data[offset] & 0x7f;
let c = value & 0x7f;
++offset;
while (c--) {
if (offset >= data.length) {
Expand All @@ -31,7 +34,7 @@ export function calculateDERLength(
);
}
actualLength <<= 8;
actualLength |= data[offset];
actualLength |= data[offset]!;
++offset;
}
}
Expand Down Expand Up @@ -153,7 +156,7 @@ export function pickIssuerAndSerialNumberDERFromCert(
if (bin.length === 0) {
throw new Error('No data is specified.');
}
return pickIssuerAndSerialNumberDERFromCert(bin[0]);
return pickIssuerAndSerialNumberDERFromCert(bin[0]!);
}
const ub = toUint8Array(bin);
if (ub.length < 2) {
Expand Down
7 changes: 5 additions & 2 deletions src/main/sign/data/ObjectIdentifier.ts
Expand Up @@ -15,13 +15,16 @@ export default class ObjectIdentifier implements DERObject {
public toDER(): number[] {
const id = this.value;
const r: number[] = [];
if (id.length < 2) {
throw new Error("Unexpected 'value' field");
}

// first byte will be (x * 40 + y) for 'x.y.****'
r.push(id[0] * 40 + id[1]);
r.push(id[0]! * 40 + id[1]!);

for (let i = 2; i < id.length; ++i) {
// store as variable-length value
let val = id[i];
let val = id[i]!;
let isFirst = true;
const insertPos = r.length;
while (true) {
Expand Down
9 changes: 4 additions & 5 deletions src/main/sign/timestamp.ts
Expand Up @@ -61,17 +61,16 @@ export function pickSignedDataFromTimestampResponse(
'Invalid or unexpected timestamp response (invalid PKIStatusInfo.status)'
);
}
switch (ub[offset + 2]) {
const status = ub[offset + 2];
switch (status) {
case 0: // granted
case 1: // grantedWithMods
break;
case 2: // rejection
case 3: // waiting
case 4: // revocationWarning
case 5: /* revocationNotification */ {
let msg: string = `Timestamp response has error status ${
ub[offset + 2]
}`;
let msg: string = `Timestamp response has error status ${status}`;
// PKIStatusInfo.statusString
if (offset + 3 < timeStampTokenOffset && ub[offset + 3] === 0x30) {
[len, offset] = calculateDERLength(ub, offset + 4);
Expand Down Expand Up @@ -110,7 +109,7 @@ export function pickSignedDataFromTimestampResponse(
}
default:
throw new Error(
`Unexpected PKIStatusInfo.status: ${ub[offset + 2]}`
`Unexpected PKIStatusInfo.status: ${status ?? '(unknown)'}`
);
}
// TimeStampToken ::= ContentInfo
Expand Down
4 changes: 2 additions & 2 deletions src/main/util/functions.ts
Expand Up @@ -151,7 +151,7 @@ export function binaryToString(bin: ArrayBuffer | ArrayBufferView): string {
if (typeof decodeURIComponent !== 'undefined') {
let s = '';
for (let i = 0; i < view.length; ++i) {
const c = view[i];
const c = view[i]!;
if (c < 16) {
s += '%0' + c.toString(16);
} else {
Expand All @@ -162,7 +162,7 @@ export function binaryToString(bin: ArrayBuffer | ArrayBufferView): string {
} else {
let s = '';
for (let i = 0; i < view.length; ++i) {
const c = view[i];
const c = view[i]!;
s += String.fromCharCode(c);
}
return s;
Expand Down

0 comments on commit 7c6a390

Please sign in to comment.