Skip to content

Commit f74f21c

Browse files
Lab free: Move answer to JavaScript
Signed-off-by: David A. Wheeler <[email protected]>
1 parent c2466e4 commit f74f21c

File tree

2 files changed

+58
-53
lines changed

2 files changed

+58
-53
lines changed

docs/labs/free.html

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,63 +7,11 @@
77
<link rel="stylesheet" href="checker.css">
88
<script src="js-yaml.min.js"></script>
99
<script src="checker.js"></script>
10+
<script src="free.js"></script>
1011
<link rel="license" href="https://creativecommons.org/licenses/by/4.0/">
1112

1213
<!-- See create_labs.md for how to create your own lab! -->
1314

14-
<!-- Sample expected answer -->
15-
<script id="expected0" type="plain/text">
16-
asprintf(&result, "pre_%s_post", s);
17-
free(s);
18-
return result;
19-
</script>
20-
21-
<!-- Full pattern of correct answer -->
22-
<script id="correct0" type="plain/text">
23-
\s*
24-
asprintf \( & result , "pre_%s_post" , s \) ;
25-
free \( s \) ;
26-
return result ;
27-
\s*
28-
</script>
29-
30-
<script id="info" type="application/yaml">
31-
---
32-
hints:
33-
- present: |-
34-
\s*free[^;]*; asprintf
35-
text: Do not free the input first, you need to use it.
36-
examples:
37-
- - |-
38-
free(s);
39-
asprintf(&result, "pre_%s_post", s);
40-
- present: |-
41-
\s* asprintf \(
42-
absent: free
43-
text: This fails to free the memory, likely leading to a missing release.
44-
examples:
45-
- - |-
46-
asprintf(&result, ""pre_%s_post"", s);
47-
- absent: return
48-
text: This fails to return the result.
49-
- absent: |-
50-
\s* [^;]+;[^;]+;[^;]+; \s*
51-
text: There should be 3 statements, each terminated with a semicolon.
52-
examples:
53-
- - |-
54-
asprintf(&result, "pre_%s_post", s);
55-
free(s);
56-
return result
57-
- present: |-
58-
\s* return result ; free \s*
59-
text: Do not do anything after the return, it will not execute.
60-
examples:
61-
- - |-
62-
asprintf(&result, "pre_%s_post", s);
63-
return result;
64-
free(s);
65-
# debug: true
66-
</script>
6715
</head>
6816
<body>
6917
<!-- For GitHub Pages formatting: -->

docs/labs/free.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
info =
2+
{
3+
hints: [
4+
{
5+
present: String.raw`\s*free[^;]*; asprintf`,
6+
text: "Do not free the input first, you need to use it.",
7+
examples: [
8+
[
9+
"free(s);\nasprintf(&result, \"pre_%s_post\", s);"
10+
]
11+
]
12+
},
13+
{
14+
present: String.raw`\s* asprintf \(`,
15+
absent: "free",
16+
text: "This fails to free the memory, likely leading to a missing release.",
17+
examples: [
18+
[
19+
"asprintf(&result, \"\"pre_%s_post\"\", s);"
20+
]
21+
]
22+
},
23+
{
24+
absent: "return",
25+
text: "This fails to return the result."
26+
},
27+
{
28+
absent: String.raw`\s* [^;]+;[^;]+;[^;]+; \s*`,
29+
text: "There should be 3 statements, each terminated with a semicolon.",
30+
examples: [
31+
[
32+
"asprintf(&result, \"pre_%s_post\", s);\nfree(s);\nreturn result"
33+
]
34+
]
35+
},
36+
{
37+
present: String.raw`\s* return result ; free \s*`,
38+
text: "Do not do anything after the return, it will not execute.",
39+
examples: [
40+
[
41+
"asprintf(&result, \"pre_%s_post\", s);\nreturn result;\nfree(s);"
42+
]
43+
]
44+
}
45+
],
46+
expected: [
47+
` asprintf(&result, "pre_%s_post", s);
48+
free(s);
49+
return result;`
50+
],
51+
correct: [
52+
String.raw`\s*
53+
asprintf \( & result , "pre_%s_post" , s \) ;
54+
free \( s \) ;
55+
return result ; \s*`
56+
],
57+
}

0 commit comments

Comments
 (0)