Skip to content

Commit

Permalink
Merge pull request #336 from algorandfoundation/fix/byte_comparison
Browse files Browse the repository at this point in the history
fix: throw error on byte comparison
  • Loading branch information
joe-p authored Jan 13, 2024
2 parents 85325d2 + c7be109 commit 2c357d0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/lib/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as ts from 'ts-morph';
// eslint-disable-next-line camelcase
import { sha512_256 } from 'js-sha512';
import path from 'path';
import { error } from 'console';
import langspec from '../static/langspec.json';
import { VERSION } from '../version';
import { optimizeTeal } from './optimize';
Expand Down Expand Up @@ -4107,6 +4108,12 @@ export default class Compiler {
const leftTypeStr = typeInfoToABIString(leftType);
const rightTypeStr = typeInfoToABIString(rightType);

if (!isNumeric(leftType) && !leftTypeStr.match(/\d+$/) && (operator.startsWith('<') || operator.startsWith('>'))) {
throw Error(
'TEALScript only supports number comparison. If you want to compare these values as numbers, use btobigint'
);
}

const isMathOp = ['+', '-', '*', '/', '%', 'exp'].includes(operator);

if (leftNode.isKind(ts.SyntaxKind.NumericLiteral)) {
Expand Down
7 changes: 7 additions & 0 deletions tests/contracts/math_compile_errors.algo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ class Uint8Exp extends Contract {
return a ** b;
}
}

// eslint-disable-next-line no-unused-vars
class BytesComparison extends Contract {
bytesComparison(a: string, b: string): boolean {
return a <= b;
}
}
20 changes: 20 additions & 0 deletions tests/math.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,25 @@ describe('Math', function () {

expect(msg).toMatch('Exponent operator only supported for uintN <= 64, got uint256 and uint256');
});

test('bytesComparison', async function () {
let msg: string;
try {
await compileAndCreate(
algosdk.generateAccount(),
'tests/contracts/math_compile_errors.algo.ts',
ARTIFACTS_DIR,
'BytesComparison'
);
msg = 'No error';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
msg = e.message;
}

expect(msg).toMatch(
'TEALScript only supports number comparison. If you want to compare these values as numbers, use btobigint'
);
});
});
});

0 comments on commit 2c357d0

Please sign in to comment.