Skip to content

Commit

Permalink
Lab free: Move answer to JavaScript
Browse files Browse the repository at this point in the history
Signed-off-by: David A. Wheeler <[email protected]>
  • Loading branch information
david-a-wheeler committed Jan 29, 2025
1 parent c2466e4 commit f74f21c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 53 deletions.
54 changes: 1 addition & 53 deletions docs/labs/free.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,11 @@
<link rel="stylesheet" href="checker.css">
<script src="js-yaml.min.js"></script>
<script src="checker.js"></script>
<script src="free.js"></script>
<link rel="license" href="https://creativecommons.org/licenses/by/4.0/">

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

<!-- Sample expected answer -->
<script id="expected0" type="plain/text">
asprintf(&result, "pre_%s_post", s);
free(s);
return result;
</script>

<!-- Full pattern of correct answer -->
<script id="correct0" type="plain/text">
\s*
asprintf \( & result , "pre_%s_post" , s \) ;
free \( s \) ;
return result ;
\s*
</script>

<script id="info" type="application/yaml">
---
hints:
- present: |-
\s*free[^;]*; asprintf
text: Do not free the input first, you need to use it.
examples:
- - |-
free(s);
asprintf(&result, "pre_%s_post", s);
- present: |-
\s* asprintf \(
absent: free
text: This fails to free the memory, likely leading to a missing release.
examples:
- - |-
asprintf(&result, ""pre_%s_post"", s);
- absent: return
text: This fails to return the result.
- absent: |-
\s* [^;]+;[^;]+;[^;]+; \s*
text: There should be 3 statements, each terminated with a semicolon.
examples:
- - |-
asprintf(&result, "pre_%s_post", s);
free(s);
return result
- present: |-
\s* return result ; free \s*
text: Do not do anything after the return, it will not execute.
examples:
- - |-
asprintf(&result, "pre_%s_post", s);
return result;
free(s);
# debug: true
</script>
</head>
<body>
<!-- For GitHub Pages formatting: -->
Expand Down
57 changes: 57 additions & 0 deletions docs/labs/free.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
info =
{
hints: [
{
present: String.raw`\s*free[^;]*; asprintf`,
text: "Do not free the input first, you need to use it.",
examples: [
[
"free(s);\nasprintf(&result, \"pre_%s_post\", s);"
]
]
},
{
present: String.raw`\s* asprintf \(`,
absent: "free",
text: "This fails to free the memory, likely leading to a missing release.",
examples: [
[
"asprintf(&result, \"\"pre_%s_post\"\", s);"
]
]
},
{
absent: "return",
text: "This fails to return the result."
},
{
absent: String.raw`\s* [^;]+;[^;]+;[^;]+; \s*`,
text: "There should be 3 statements, each terminated with a semicolon.",
examples: [
[
"asprintf(&result, \"pre_%s_post\", s);\nfree(s);\nreturn result"
]
]
},
{
present: String.raw`\s* return result ; free \s*`,
text: "Do not do anything after the return, it will not execute.",
examples: [
[
"asprintf(&result, \"pre_%s_post\", s);\nreturn result;\nfree(s);"
]
]
}
],
expected: [
` asprintf(&result, "pre_%s_post", s);
free(s);
return result;`
],
correct: [
String.raw`\s*
asprintf \( & result , "pre_%s_post" , s \) ;
free \( s \) ;
return result ; \s*`
],
}

0 comments on commit f74f21c

Please sign in to comment.