Validate RAM amount in ns.purchaseServer()

This commit is contained in:
nickofolas
2022-01-03 17:00:32 -06:00
parent dfae337d26
commit 23b21e63a0
2 changed files with 2 additions and 2 deletions

View File

@ -1639,7 +1639,7 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS {
const cost = getPurchaseServerCost(ram);
if (cost === Infinity) {
workerScript.log("purchaseServer", () => `Invalid argument: ram='${ram}' must be power of 2`);
workerScript.log("purchaseServer", () => `Invalid argument: ram='${ram}' must be a positive power of 2`);
return "";
}

View File

@ -20,7 +20,7 @@ import { isPowerOfTwo } from "../utils/helpers/isPowerOfTwo";
*/
export function getPurchaseServerCost(ram: number): number {
const sanitizedRam = Math.round(ram);
if (isNaN(sanitizedRam) || !isPowerOfTwo(sanitizedRam)) {
if (isNaN(sanitizedRam) || !isPowerOfTwo(sanitizedRam) || !(Math.sign(sanitizedRam) === 1)) {
return Infinity;
}