MISC: Enforce stricter param check on ns.getBitNodeMultipliers and ns.hacknet.spendHashes (#2085)

This commit is contained in:
catloversg
2025-04-15 06:20:36 +07:00
committed by GitHub
parent 44b8baa8ad
commit 31e6e4d74b
4 changed files with 9 additions and 18 deletions

View File

@@ -18,7 +18,7 @@ spendHashes(upgName: string, upgTarget?: string, count?: number): boolean;
| --- | --- | --- |
| upgName | string | Name of the upgrade of Hacknet Node. |
| upgTarget | string | _(Optional)_ Object to which upgrade applies. Required for certain upgrades. |
| count | number | _(Optional)_ Number of upgrades to buy at once. Defaults to 1 if not specified. For compatibility reasons, upgTarget must be specified, even if it is not used, in order to specify count. |
| count | number | _(Optional)_ Number of upgrades to buy at once. Must be a non-negative integer. Defaults to 1 if not specified. For compatibility reasons, upgTarget must be specified, even if it is not used, in order to specify count. |
**Returns:**

View File

@@ -330,7 +330,7 @@ export const ns: InternalAPI<NSFull> = {
(_host, _multiplier, _cores = 1) => {
const host = helpers.string(ctx, "hostname", _host);
const mult = helpers.number(ctx, "multiplier", _multiplier);
const cores = helpers.number(ctx, "cores", _cores);
const cores = helpers.positiveInteger(ctx, "cores", _cores);
// Check argument validity
const server = helpers.getServer(ctx, host);
@@ -339,13 +339,9 @@ export const ns: InternalAPI<NSFull> = {
helpers.log(ctx, () => `${host} is not a hackable server. Returning 0.`);
return 0;
}
if (mult < 1 || !isFinite(mult)) {
if (!Number.isFinite(mult) || mult < 1) {
throw helpers.errorMessage(ctx, `Invalid argument: multiplier must be finite and >= 1, is ${mult}.`);
}
// TODO 2.3: Add assertion function for positive integer, there are a lot of places everywhere that can use this
if (!Number.isInteger(cores) || cores < 1) {
throw helpers.errorMessage(ctx, `Cores should be a positive integer. Cores provided: ${cores}`);
}
return numCycleForGrowth(server, mult, cores);
},
@@ -986,15 +982,11 @@ export const ns: InternalAPI<NSFull> = {
if (!canAccessBitNodeFeature(5)) {
throw helpers.errorMessage(ctx, "Requires Source-File 5 to run.");
}
// TODO v3.0: check n and lvl with helpers.number() and Number.isInteger().
const n = Math.round(helpers.number(ctx, "n", _n));
const lvl = Math.round(helpers.number(ctx, "lvl", _lvl));
const n = helpers.positiveInteger(ctx, "n", _n);
const lvl = helpers.positiveInteger(ctx, "lvl", _lvl);
if (!validBitNodes.includes(n)) {
throw new Error(`Invalid BitNode: ${n}.`);
}
if (lvl < 1) {
throw new Error("SF level must be greater than or equal to 1.");
}
return Object.assign({}, getBitNodeMultipliers(n, lvl));
},

View File

@@ -201,10 +201,9 @@ export function NetscriptHacknet(): InternalAPI<IHacknet> {
(_upgName, _upgTarget = "", _count = 1) => {
const upgName = helpers.string(ctx, "upgName", _upgName);
const upgTarget = helpers.string(ctx, "upgTarget", _upgTarget);
const count = Math.floor(helpers.number(ctx, "count", _count));
// TODO (3.0.0): use helpers.positiveInteger
if (!(count >= 0)) {
throw helpers.errorMessage(ctx, "count may not be negative");
const count = helpers.integer(ctx, "count", _count);
if (count < 0) {
throw helpers.errorMessage(ctx, "Count must be a non-negative integer.");
}
if (!hasHacknetServers()) {
return false;

View File

@@ -3110,7 +3110,7 @@ export interface Hacknet {
* ```
* @param upgName - Name of the upgrade of Hacknet Node.
* @param upgTarget - Object to which upgrade applies. Required for certain upgrades.
* @param count - Number of upgrades to buy at once. Defaults to 1 if not specified.
* @param count - Number of upgrades to buy at once. Must be a non-negative integer. Defaults to 1 if not specified.
* For compatibility reasons, upgTarget must be specified, even if it is not used, in order to specify count.
* @returns True if the upgrade is successfully purchased, and false otherwise.
*/