Skip to content

Commit b3aedd2

Browse files
committed
based on pr #181
1 parent da77a9e commit b3aedd2

9 files changed

Lines changed: 288 additions & 110 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,7 @@ target
9999
*.unparsed
100100
*.gobrafied
101101
*.internal
102+
.devcontainer
103+
.metals
104+
.bazelbsp
105+
.bazel

pkg/addr/fmt.go

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// +gobra
16+
1517
package addr
1618

1719
import (
@@ -24,6 +26,8 @@ import (
2426

2527
// ParseFormattedIA parses an IA that was formatted with the FormatIA function.
2628
// The same options must be provided to successfully parse.
29+
// @ trusted
30+
// @ requires false
2731
func ParseFormattedIA(ia string, opts ...FormatOption) (IA, error) {
2832
parts := strings.Split(ia, "-")
2933
if len(parts) != 2 {
@@ -33,15 +37,17 @@ func ParseFormattedIA(ia string, opts ...FormatOption) (IA, error) {
3337
if err != nil {
3438
return 0, serrors.WrapStr("parsing ISD part", err, "value", ia)
3539
}
36-
as, err := ParseFormattedAS(parts[1], opts...)
40+
as_, err := ParseFormattedAS(parts[1], opts...)
3741
if err != nil {
3842
return 0, serrors.WrapStr("parsing AS part", err, "value", ia)
3943
}
40-
return MustIAFrom(isd, as), nil
44+
return MustIAFrom(isd, as_), nil
4145
}
4246

4347
// ParseFormattedISD parses an ISD number that was formatted with the FormatISD
4448
// function. The same options must be provided to successfully parse.
49+
// @ trusted
50+
// @ requires false
4551
func ParseFormattedISD(isd string, opts ...FormatOption) (ISD, error) {
4652
o := applyFormatOptions(opts)
4753
if o.defaultPrefix {
@@ -56,29 +62,35 @@ func ParseFormattedISD(isd string, opts ...FormatOption) (ISD, error) {
5662

5763
// ParseFormattedAS parses an AS number that was formatted with the FormatAS
5864
// function. The same options must be provided to successfully parse.
59-
func ParseFormattedAS(as string, opts ...FormatOption) (AS, error) {
65+
// @ trusted
66+
// @ requires false
67+
func ParseFormattedAS(as_ string, opts ...FormatOption) (AS, error) {
6068
o := applyFormatOptions(opts)
6169
if o.defaultPrefix {
62-
trimmed := strings.TrimPrefix(as, "AS")
63-
if trimmed == as {
64-
return 0, serrors.New("prefix is missing", "prefix", "AS", "value", as)
70+
trimmed := strings.TrimPrefix(as_, "AS")
71+
if trimmed == as_ {
72+
return 0, serrors.New("prefix is missing", "prefix", "AS", "value", as_)
6573
}
66-
as = trimmed
74+
as_ = trimmed
6775
}
68-
return parseAS(as, o.separator)
76+
return parseAS(as_, o.separator)
6977
}
7078

7179
// FormatIA formats the ISD-AS.
80+
// @ trusted
81+
// @ requires false
7282
func FormatIA(ia IA, opts ...FormatOption) string {
7383
o := applyFormatOptions(opts)
74-
as := fmtAS(ia.AS(), o.separator)
84+
as_ := fmtAS(ia.AS(), o.separator)
7585
if o.defaultPrefix {
76-
return fmt.Sprintf("ISD%d-AS%s", ia.ISD(), as)
86+
return fmt.Sprintf("ISD%d-AS%s", ia.ISD(), as_)
7787
}
78-
return fmt.Sprintf("%d-%s", ia.ISD(), as)
88+
return fmt.Sprintf("%d-%s", ia.ISD(), as_)
7989
}
8090

8191
// FormatISD formats the ISD number.
92+
// @ trusted
93+
// @ requires false
8294
func FormatISD(isd ISD, opts ...FormatOption) string {
8395
o := applyFormatOptions(opts)
8496
if o.defaultPrefix {
@@ -88,24 +100,28 @@ func FormatISD(isd ISD, opts ...FormatOption) string {
88100
}
89101

90102
// FormatAS formats the AS number.
91-
func FormatAS(as AS, opts ...FormatOption) string {
103+
// @ trusted
104+
// @ requires false
105+
func FormatAS(as_ AS, opts ...FormatOption) string {
92106
o := applyFormatOptions(opts)
93-
s := fmtAS(as, o.separator)
107+
s := fmtAS(as_, o.separator)
94108
if o.defaultPrefix {
95109
return "AS" + s
96110
}
97111
return s
98112
}
99113

100-
func fmtAS(as AS, sep string) string {
101-
if !as.inRange() {
102-
return fmt.Sprintf("%d [Illegal AS: larger than %d]", as, MaxAS)
114+
// @ trusted
115+
// @ requires as_.inRange()
116+
func fmtAS(as_ AS, sep string) string {
117+
if !as_.inRange() {
118+
return fmt.Sprintf("%d [Illegal AS: larger than %d]", as_, MaxAS)
103119
}
104-
// Format BGP ASes as decimal
105-
if as <= MaxBGPAS {
106-
return strconv.FormatUint(uint64(as), 10)
120+
// Format BGP ASes as_ decimal
121+
if as_ <= MaxBGPAS {
122+
return strconv.FormatUint(uint64(as_), 10)
107123
}
108-
// Format all other ASes as 'sep'-separated hex.
124+
// Format all other ASes as_ 'sep'-separated hex.
109125
const maxLen = len("ffff:ffff:ffff")
110126
var b strings.Builder
111127
b.Grow(maxLen)
@@ -114,18 +130,20 @@ func fmtAS(as AS, sep string) string {
114130
b.WriteString(sep)
115131
}
116132
shift := uint(asPartBits * (asParts - i - 1))
117-
b.WriteString(strconv.FormatUint(uint64(as>>shift)&asPartMask, asPartBase))
133+
b.WriteString(strconv.FormatUint(uint64(as_>>shift)&asPartMask, asPartBase))
118134
}
119135
return b.String()
120136
}
121137

122-
type FormatOption func(*formatOptions)
138+
type FormatOption = func(*formatOptions)
123139

124140
type formatOptions struct {
125141
defaultPrefix bool
126142
separator string
127143
}
128144

145+
// @ trusted
146+
// @ requires false
129147
func applyFormatOptions(opts []FormatOption) formatOptions {
130148
o := formatOptions{
131149
defaultPrefix: false,
@@ -139,6 +157,8 @@ func applyFormatOptions(opts []FormatOption) formatOptions {
139157

140158
// WithDefaultPrefix enables the default prefix which depends on the type. For
141159
// the AS number, the prefix is 'AS'. For the ISD number, the prefix is 'ISD'.
160+
// @ trusted
161+
// @ requires false
142162
func WithDefaultPrefix() FormatOption {
143163
return func(o *formatOptions) {
144164
o.defaultPrefix = true
@@ -147,13 +167,17 @@ func WithDefaultPrefix() FormatOption {
147167

148168
// WithSeparator sets the separator to use for formatting AS numbers. In case of
149169
// the empty string, the ':' is used.
170+
// @ trusted
171+
// @ requires false
150172
func WithSeparator(separator string) FormatOption {
151173
return func(o *formatOptions) {
152174
o.separator = separator
153175
}
154176
}
155177

156178
// WithFileSeparator returns an option that sets the separator to underscore.
179+
// @ trusted
180+
// @ requires false
157181
func WithFileSeparator() FormatOption {
158182
return WithSeparator("_")
159183
}

pkg/addr/fmt_spec.gobra

Lines changed: 0 additions & 21 deletions
This file was deleted.

pkg/addr/host.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515

1616
// +gobra
1717

18-
// @ initEnsures ErrBadHostAddrType.ErrorMem()
19-
// @ initEnsures ErrMalformedHostAddrType.ErrorMem()
20-
// @ initEnsures ErrUnsupportedSVCAddress.ErrorMem()
2118
package addr
2219

2320
import (

verification/dependencies/net/ip.gobra

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (ip IP) To4(ghost wildcard bool) (res IP) {
8484
}
8585

8686
pure
87-
preserves forall i int :: 0 <= i && i < len(s) ==> acc(&s[i], definitions.ReadL16)
87+
requires forall i int :: 0 <= i && i < len(s) ==> acc(&s[i], definitions.ReadL16)
8888
func isZeros(s []byte) bool
8989

9090
// To16 converts the IP address ip to a 16-byte representation.

verification/dependencies/strconv/atoi.gobra

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,39 @@
99

1010
package strconv
1111

12+
// import "errors" //(joao) stub errors package still not implemented
13+
14+
// ErrRange indicates that a value is out of range for the target type.
15+
// var ErrRange = errors.New("value out of range") // (joao): no support for global vars and for the errors package
16+
17+
// ErrSyntax indicates that a value does not have the right syntax for the target type.
18+
// var ErrSyntax = errors.New("invalid syntax") // (joao): no support for global vars and for the errors package
19+
20+
// A NumError records a failed conversion.
21+
type NumError struct {
22+
Func string // the failing function (ParseBool, ParseInt, ParseUint, ParseFloat, ParseComplex)
23+
Num string // the input
24+
Err error // the reason the conversion failed (e.g. ErrRange, ErrSyntax, etc.)
25+
}
26+
27+
requires p > 0
28+
requires acc(e, p)
29+
ensures acc(e, p)
30+
decreases _
31+
func (e *NumError) Error(ghost p perm) string /*{
32+
return "strconv." + e.Func + ": " + "parsing " + Quote(e.Num) + ": " + e.Err.Error()
33+
}*/
34+
35+
36+
requires acc(e, _)
37+
decreases
38+
pure func (e *NumError) Unwrap() error { return e.Err }
39+
40+
// const intSize = 32 << (^uint(0) >> 63)
41+
42+
// IntSize is the size in bits of an int or uint value.
43+
// const IntSize = intSize
44+
1245
// a to the power of b
1346
ghost
1447
requires exp >= 0
@@ -20,7 +53,7 @@ pure func Exp(base int, exp int) (res int) {
2053
// ParseUint is like ParseInt but for unsigned numbers.
2154
requires base == 0 || (2 <= base && base <= 36)
2255
requires bitSize > 0 && bitSize <= 64
23-
ensures retErr == nil ==> (ret >= 0 && ret < Exp(2, bitSize))
56+
ensures retErr == nil ==> (ret >= 0 && ret < uint64(Exp(2, bitSize)))
2457
ensures retErr != nil ==> retErr.ErrorMem()
2558
decreases _
2659
func ParseUint(s string, base int, bitSize int) (ret uint64, retErr error)

0 commit comments

Comments
 (0)