Skip to content

Commit

Permalink
fix: dns_internal_normalize and tests for when it throws (#834)
Browse files Browse the repository at this point in the history
  • Loading branch information
novusnota authored Sep 15, 2024
1 parent 804c6ae commit 242fbd5
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Maps with `coins` as value type are now correctly handled in structs: PR [#821](https://github.com/tact-lang/tact/pull/821)
- Contract method calls in return statements: PR [#829](https://github.com/tact-lang/tact/pull/829)
- Disallow initializers for trait storage fields: PR [#831](https://github.com/tact-lang/tact/pull/831)
- Fix `dnsInternalNormalize()` in `@stdlib/dns` to throw on slices with references as expected: PR [#834](https://github.com/tact-lang/tact/pull/834)

## [1.4.4] - 2024-08-18

Expand Down
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
"src/test/e2e-emulated/strings.spec.ts",
"src/test/e2e-emulated/contracts/intrinsics.tact",
"src/test/e2e-emulated/contracts/strings.tact",
"src/test/e2e-emulated/contracts/dns.tact",
"src/test/compilation-fail/fail-const-eval.spec.ts",
"src/test/e2e-emulated/getter-names-conflict.spec.ts",
"src/test/exit-codes/contracts/compute-phase-errors.tact",
Expand Down
20 changes: 10 additions & 10 deletions src/imports/stdlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ files['libs/dns.fc'] =
'dXJuIGZhbHNlOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChjaGFySXNIeXBoZW4gJiBpc0ZpcnN0KSAgewogICAgICAgICAgICAgICAgcmV0dXJuIGZhbHNl' +
'OwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlzSHlwaGVuID0gY2hhcklzSHlwaGVuOwogICAgICAgICAgICBpc0ZpcnN0ID0gZmFsc2U7CiAgICAgICAgICAgIGNv' +
'dW50ZXIgPSBjb3VudGVyICsgMTsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gY291bnRlciA9PSAwICYgfiBpc0h5cGhlbjsKfQoKc2xpY2UgZG5zX2ludGVybmFs' +
'X25vcm1hbGl6ZShzbGljZSBzcmMpIGlubGluZV9yZWYgewogICAgdGhyb3dfdW5sZXNzKHNyYy5zbGljZV9yZWZzKCkgPT0gMCwgMTM0KTsgOzsgSW52YWxpZCBhcmd1' +
'bWVudCBlcnJvcgogICAgYnVpbGRlciB0YXJnZXQgPSBiZWdpbl9jZWxsKCk7CiAgICByZXBlYXQoc3JjLnNsaWNlX2JpdHMoKSAvIDgpIHsKICAgICAgICBpbnQgY2hh' +
'ciA9IHNyY35sb2FkX3VpbnQoOCk7CgogICAgICAgIDs7IGIgPT4gNgogICAgICAgIGlmIChjaGFyID09IDk4KSB7CiAgICAgICAgICAgIGNoYXIgPSA1NDsKICAgICAg' +
'ICB9CgogICAgICAgIDs7IGcsIHEgPT4gOQogICAgICAgIGlmICgoY2hhciA9PSAxMDMpIHwgKGNoYXIgPT0gMTEzKSkgewogICAgICAgICAgICBjaGFyID0gNTc7ICAg' +
'IAogICAgICAgIH0KCiAgICAgICAgOzsgbCA9PiAxCiAgICAgICAgaWYgKGNoYXIgPT0gMTA4KSB7CiAgICAgICAgICAgIGNoYXIgPSA0OTsKICAgICAgICB9CgogICAg' +
'ICAgIDs7IG8gPT4gMAogICAgICAgIGlmIChjaGFyID09IDExMSkgewogICAgICAgICAgICBjaGFyID0gNDg7CiAgICAgICAgfQoKICAgICAgICA7OyBzID0+IDUKICAg' +
'ICAgICBpZiAoY2hhciA9PSAxMTUpIHsKICAgICAgICAgICAgY2hhciA9IDUzOwogICAgICAgIH0KCiAgICAgICAgOzsgdSA9PiB2CiAgICAgICAgaWYgKGNoYXIgPT0g' +
'MTE3KSB7CiAgICAgICAgICAgIGNoYXIgPSAxMTg7CiAgICAgICAgfQoKICAgICAgICA7OyB6ID0+IDIKICAgICAgICBpZiAoY2hhciA9PSAxMjIpIHsKICAgICAgICAg' +
'ICAgY2hhciA9IDUwOwogICAgICAgIH0KCiAgICAgICAgdGFyZ2V0ID0gdGFyZ2V0LnN0b3JlX3VpbnQoY2hhciwgOCk7CiAgICB9CiAgICByZXR1cm4gdGFyZ2V0LmVu' +
'ZF9jZWxsKCkuYmVnaW5fcGFyc2UoKTsKfQ==';
'X25vcm1hbGl6ZShzbGljZSBzcmMpIGltcHVyZSBpbmxpbmVfcmVmIHsKICAgIHRocm93X3VubGVzcygxMzQsIHNyYy5zbGljZV9yZWZzKCkgPT0gMCk7IDs7IEludmFs' +
'aWQgYXJndW1lbnQgZXJyb3IKICAgIGJ1aWxkZXIgdGFyZ2V0ID0gYmVnaW5fY2VsbCgpOwogICAgcmVwZWF0KHNyYy5zbGljZV9iaXRzKCkgLyA4KSB7CiAgICAgICAg' +
'aW50IGNoYXIgPSBzcmN+bG9hZF91aW50KDgpOwoKICAgICAgICA7OyBiID0+IDYKICAgICAgICBpZiAoY2hhciA9PSA5OCkgewogICAgICAgICAgICBjaGFyID0gNTQ7' +
'CiAgICAgICAgfQoKICAgICAgICA7OyBnLCBxID0+IDkKICAgICAgICBpZiAoKGNoYXIgPT0gMTAzKSB8IChjaGFyID09IDExMykpIHsKICAgICAgICAgICAgY2hhciA9' +
'IDU3OyAgICAKICAgICAgICB9CgogICAgICAgIDs7IGwgPT4gMQogICAgICAgIGlmIChjaGFyID09IDEwOCkgewogICAgICAgICAgICBjaGFyID0gNDk7CiAgICAgICAg' +
'fQoKICAgICAgICA7OyBvID0+IDAKICAgICAgICBpZiAoY2hhciA9PSAxMTEpIHsKICAgICAgICAgICAgY2hhciA9IDQ4OwogICAgICAgIH0KCiAgICAgICAgOzsgcyA9' +
'PiA1CiAgICAgICAgaWYgKGNoYXIgPT0gMTE1KSB7CiAgICAgICAgICAgIGNoYXIgPSA1MzsKICAgICAgICB9CgogICAgICAgIDs7IHUgPT4gdgogICAgICAgIGlmIChj' +
'aGFyID09IDExNykgewogICAgICAgICAgICBjaGFyID0gMTE4OwogICAgICAgIH0KCiAgICAgICAgOzsgeiA9PiAyCiAgICAgICAgaWYgKGNoYXIgPT0gMTIyKSB7CiAg' +
'ICAgICAgICAgIGNoYXIgPSA1MDsKICAgICAgICB9CgogICAgICAgIHRhcmdldCA9IHRhcmdldC5zdG9yZV91aW50KGNoYXIsIDgpOwogICAgfQogICAgcmV0dXJuIHRh' +
'cmdldC5lbmRfY2VsbCgpLmJlZ2luX3BhcnNlKCk7Cn0K';
files['libs/dns.tact'] =
'aW1wb3J0ICIuL2Rucy5mYyI7CgpzdHJ1Y3QgRE5TUmVzb2x2ZVJlc3VsdCB7CiAgICBwcmVmaXg6IEludDsgCiAgICByZWNvcmQ6IENlbGw/OyAKfQoKQG5hbWUoZG5z' +
'X3N0cmluZ190b19pbnRlcm5hbCkKbmF0aXZlIGRuc1N0cmluZ1RvSW50ZXJuYWwoc3RyOiBTdHJpbmcpOiBTbGljZT87CgpAbmFtZShkbnNfaW50ZXJuYWxfbm9ybWFs' +
Expand Down
15 changes: 11 additions & 4 deletions src/test/e2e-emulated/contracts/dns.tact
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import "@stdlib/dns";
import "@stdlib/deploy";

contract DNSTester with Deployable, DNSResolver {
/// Used for storing temporary values
tmpC: Cell = cell("te6cckEBAQEAAgAAAEysuc0="); // empty cell

init() {

}
init() {}

get fun stringToInternal(src: String): Slice? {
return dnsStringToInternal(src);
Expand All @@ -30,4 +30,11 @@ contract DNSTester with Deployable, DNSResolver {
override fun doResolveDNS(subdomain: Slice, category: Int): DNSResolveResult {
return DNSResolveResult{ prefix: subdomain.bits(), record: beginCell().storeSlice(subdomain).endCell() };
}
}

receive("test dnsInternalNormalize throws") {
let sliceWithRef = beginCell().storeRef(emptyCell()).asSlice();

// Refs in the passed Slice are prohibited
self.tmpC = dnsInternalNormalize(sliceWithRef).asCell();
}
}
15 changes: 15 additions & 0 deletions src/test/e2e-emulated/dns.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,19 @@ describe("dns", () => {
}
});
}

it("should test dnsInternalNormalize throws", async () => {
const sendResult = await contract.send(
treasure.getSender(),
{ value: toNano("10") },
"test dnsInternalNormalize throws",
);

expect(sendResult.transactions).toHaveTransaction({
from: treasure.address,
to: contract.address,
success: false,
exitCode: 134, // Invalid argument
});
});
});
11 changes: 11 additions & 0 deletions src/test/exit-codes/contracts/tact-reserved-contract-errors.tact
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ contract ReservedContractErrorsTester with Ownable {
}
require(failed, "Int.toFloatString() didn't error on digits 78");

// Case 5
try {
// Refs in the passed Slice are prohibited
let sliceWithRef = beginCell().storeRef(emptyCell()).asSlice();
dump(dnsInternalNormalize(sliceWithRef));
failed = false;
} catch (exitCode) {
nativeThrowIf(exitCode, exitCode != targetCode);
}
require(failed, "dnsInternalNormalize() didn't error on Slice with refs");

// As we got here, everything above caused the target exit code,
// so let's throw it explicitly now for the tests on Blueprint's side
throw(targetCode);
Expand Down
6 changes: 3 additions & 3 deletions stdlib/libs/dns.fc
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ int dns_internal_verify(slice sc) inline_ref {
return counter == 0 & ~ isHyphen;
}

slice dns_internal_normalize(slice src) inline_ref {
throw_unless(src.slice_refs() == 0, 134); ;; Invalid argument error
slice dns_internal_normalize(slice src) impure inline_ref {
throw_unless(134, src.slice_refs() == 0); ;; Invalid argument error
builder target = begin_cell();
repeat(src.slice_bits() / 8) {
int char = src~load_uint(8);
Expand Down Expand Up @@ -166,4 +166,4 @@ slice dns_internal_normalize(slice src) inline_ref {
target = target.store_uint(char, 8);
}
return target.end_cell().begin_parse();
}
}

0 comments on commit 242fbd5

Please sign in to comment.