├── .cs50.yml
├── .gitignore
├── LICENSE
├── README.md
├── helpers
├── __init__.py
├── _common.py
├── bash.py
├── clang.py
├── cp.py
├── ls.py
├── make.py
├── mv.py
├── rm.py
├── runtime.py
└── valgrind.py
├── run_tests.py
└── test_files
├── a
├── floating_point_exception.c
└── segmentation_fault.c
├── clang
├── array_bounds.c
├── array_subscript.c
├── assignment_as_condition.c
├── bad_define.c
├── bad_include.c
├── catch_all_expected_expression.c
├── catch_all_expected_identifier.c
├── comp_str_literal_unspecified.c
├── conflicting_types.c
├── continue_not_in_loop.c
├── control_reaches_non_void.c
├── declaration_shadows_local_var0.c
├── declaration_shadows_local_var1.c
├── div_by_zero.c
├── expected_closing_brace.c
├── expected_closing_parens.c
├── expected_for_semi_colon.c
├── expected_identifier_or_parens.c
├── expected_if_closing_parens.c
├── expected_param_declarator.c
├── expected_semi_colon.c
├── expected_while_in_do_while.c
├── expression_not_int_const.c
├── expression_result_unused.c
├── extra_tokens_at_end_of_include.c
├── extraneous_closing_brace.c
├── extraneous_closing_parens.c
├── file_not_found_include.c
├── fmt_specifies_type.c
├── fmt_string_not_string_literal.c
├── has_empty_body.c
├── ignoring_return_value.c
├── implicit_declaration_of_fun.c
├── implicitly_declaring_lib_fun.c
├── incompatible_conversion.c
├── index_out_of_bounds.c
├── invalid_append_string.c
├── invalid_equals.c
├── invalid_preprocessing_directive.c
├── main_must_return_int.c
├── missing_parens.c
├── more_conversions_than_data_args.c
├── multiple_unsequenced_modifications.c
├── one_param_on_main_dec.c
├── relational_comp_res_unused.c
├── second_param_main_char.c
├── self_initialization.c
├── subscripted_val_not_array.c
├── too_many_args_to_fun_call.c
├── type_specifier_missing.c
├── undefined_reference0.c
├── undefined_reference1.c
├── unknown_escape_sequence.c
├── unknown_type_bool.c
├── unknown_type_size_t.c
├── unknown_type_string.c
├── unknown_type_unknown.c
├── unused_arg_in_fmt_string.c
├── unused_var.c
├── use_of_undeclared_identifier.c
├── variable_uninitialized.c
└── void_return.c
├── runtime
├── floating_point_exception
├── floating_point_exception.c
├── segmentation_fault
└── segmentation_fault.c
└── valgrind
├── all_heap_blocks_freed
├── all_heap_blocks_freed.c
├── bytes_definitely_lost
├── bytes_definitely_lost.c
├── conditional_jump_depends
├── conditional_jump_depends.c
├── definitely_lost
├── definitely_lost.c
├── invalid_read
├── invalid_read.c
├── invalid_write
├── invalid_write.c
├── use_of_uninitialised_val
└── use_of_uninitialised_val.c
/.cs50.yml:
--------------------------------------------------------------------------------
1 | help50: true
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | *.egg-info/
24 | .installed.cfg
25 | *.egg
26 | MANIFEST
27 |
28 | # PyInstaller
29 | # Usually these files are written by a python script from a template
30 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
31 | *.manifest
32 | *.spec
33 |
34 | # Installer logs
35 | pip-log.txt
36 | pip-delete-this-directory.txt
37 |
38 | # Unit test / coverage reports
39 | htmlcov/
40 | .tox/
41 | .coverage
42 | .coverage.*
43 | .cache
44 | nosetests.xml
45 | coverage.xml
46 | *.cover
47 | .hypothesis/
48 | .pytest_cache/
49 |
50 | # Translations
51 | *.mo
52 | *.pot
53 |
54 | # Django stuff:
55 | *.log
56 | local_settings.py
57 | db.sqlite3
58 |
59 | # Flask stuff:
60 | instance/
61 | .webassets-cache
62 |
63 | # Scrapy stuff:
64 | .scrapy
65 |
66 | # Sphinx documentation
67 | docs/_build/
68 |
69 | # PyBuilder
70 | target/
71 |
72 | # Jupyter Notebook
73 | .ipynb_checkpoints
74 |
75 | # pyenv
76 | .python-version
77 |
78 | # celery beat schedule file
79 | celerybeat-schedule
80 |
81 | # SageMath parsed files
82 | *.sage.py
83 |
84 | # Environments
85 | .env
86 | .venv
87 | env/
88 | venv/
89 | ENV/
90 | env.bak/
91 | venv.bak/
92 |
93 | # Spyder project settings
94 | .spyderproject
95 | .spyproject
96 |
97 | # Rope project settings
98 | .ropeproject
99 |
100 | # mkdocs documentation
101 | /site
102 |
103 | # mypy
104 | .mypy_cache/
105 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # helpers
2 |
3 | Helpers used by help50 v3+
4 |
5 |
6 | ## Contributing
7 |
8 | TODO
9 |
10 | ## Tests
11 | Tests for each helpers are implemented using `doctest`. To run all tests, run `./run_tests.py`.
12 |
--------------------------------------------------------------------------------
/helpers/__init__.py:
--------------------------------------------------------------------------------
1 | from . import bash, clang, cp, ls, make, mv, rm, runtime, valgrind
2 |
--------------------------------------------------------------------------------
/helpers/_common.py:
--------------------------------------------------------------------------------
1 | FILE_PATH = "(?:(?:.*)\/(?:[^/]+)\/)?"
2 | BASH_PATH = "{}(?:ba)?sh".format(FILE_PATH)
3 |
--------------------------------------------------------------------------------
/helpers/bash.py:
--------------------------------------------------------------------------------
1 | import re
2 |
3 | from . import _common
4 |
5 | from help50 import helper
6 |
7 |
8 | @helper("bash")
9 | def cd_no_such_file_or_directory(lines):
10 | """
11 | >>> "Are you sure" in cd_no_such_file_or_directory([ \
12 | "bash: cd: baz: No such file or directory" \
13 | ])[1][0]
14 | True
15 | """
16 | matches = re.search(r"^{}: (?:line \d+: )?cd: (.+): No such file or directory".format(_common.BASH_PATH), lines[0])
17 | if not matches:
18 | return
19 |
20 | response = [
21 | "Are you sure `{}` exists?".format(matches.group(1)),
22 | "Did you misspell `{}`?".format(matches.group(1))
23 | ]
24 |
25 | return lines[0:1], response
26 |
27 |
28 | @helper("bash")
29 | def cd_not_a_directory(lines):
30 | """
31 | >>> "trying to change" in cd_not_a_directory([ \
32 | "bash: cd: foo: Not a directory" \
33 | ])[1][0]
34 | True
35 | """
36 | matches = re.search(r"^{}: (?:line \d+: )?cd: (.+): Not a directory".format(_common.BASH_PATH), lines[0])
37 | if not matches:
38 | return
39 |
40 | response = [
41 | "Looks like you're trying to change directories, but `{}` isn't a directory.".format(matches.group(1)),
42 | "Did you mean to create the directory `{}` first?".format(matches.group(1))
43 | ]
44 |
45 | return lines[0:1], response
46 |
47 |
48 | @helper("bash")
49 | def command_not_found(lines):
50 | """
51 | >>> "Are you sure" in command_not_found([ \
52 | "bash: foo: command not found" \
53 | ])[1][0]
54 | True
55 | """
56 | matches = re.search(r"^{}: (.+): command not found".format(_common.BASH_PATH), lines[0])
57 | if not matches:
58 | return
59 |
60 | if (matches.group(1) == "check"):
61 | response = ["Did you mean to execute `check50`?"]
62 | elif (matches.group(1) == "style"):
63 | response = ["Did you mean to execute `style50`?"]
64 | elif (matches.group(1) == "submit"):
65 | response = ["Did you mean to execute `submit50`?"]
66 | else:
67 | response = [
68 | "Are you sure `{}` exists?".format(matches.group(1)),
69 | "Did you misspell `{}`?".format(matches.group(1)),
70 | "Did you mean to execute `./{}`?".format(matches.group(1))
71 | ]
72 |
73 | return lines[0:1], response
74 |
75 |
76 | @helper("bash")
77 | def dot_slash_no_such_file_or_directory(lines):
78 | """
79 | >>> "Are you sure" in dot_slash_no_such_file_or_directory([ \
80 | "bash: ./foo: No such file or directory" \
81 | ])[1][0]
82 | True
83 | """
84 | matches = re.search(r"^{}: ./(.+): No such file or directory".format(_common.BASH_PATH), lines[0])
85 | if not matches:
86 | return
87 |
88 | response = [
89 | "Are you sure `{}` exists?".format(matches.group(1)),
90 | "Did you misspell `{}`?".format(matches.group(1)),
91 | "Did you mean to execute `{}` instead of `./{}`?".format(matches.group(1), matches.group(1))
92 | ]
93 |
94 | return lines[0:1], response
95 |
96 |
97 | @helper("bash")
98 | def permission_denied(lines):
99 | """
100 | >>> "couldn't be executed" in permission_denied([ \
101 | "bash: ./foo: Permission denied" \
102 | ])[1][0]
103 | True
104 | """
105 | matches = re.search(r"^{}: .*?(([^/]+?)\.?([^/.]*)): Permission denied".format(_common.BASH_PATH), lines[0])
106 | if not matches:
107 | return
108 |
109 | response = [
110 | "`{}` couldn't be executed.".format(matches.group(1))
111 | ]
112 |
113 | if (matches.group(3).lower() == "c"):
114 | response.append("Did you mean to execute `./{}`?".format(matches.group(2)))
115 | elif (matches.group(3).lower() == "pl"):
116 | response.append("Did you mean to execute `perl {}`?".format(matches.group(1)))
117 | elif (matches.group(3).lower() == "php"):
118 | response.append("Did you mean to execute `php {}`?".format(matches.group(1)))
119 | elif (matches.group(3).lower() == "rb"):
120 | response.append("Did you mean to execute `ruby {}`?".format(matches.group(1)))
121 | else:
122 | response.append("Does `{}` definitely exist?".format(matches.group(1)))
123 | response.append("Do you need to make `{}` executable with " \
124 | "`chmod +x {}`?".format(matches.group(1), matches.group(1)))
125 |
126 | return lines[0:1], response
127 |
--------------------------------------------------------------------------------
/helpers/clang.py:
--------------------------------------------------------------------------------
1 | import collections
2 | import re
3 |
4 | from help50 import helper
5 |
6 |
7 | @helper("clang")
8 | def array_bounds(lines):
9 | """
10 | >>> "location 1 of `a`" in array_bounds([ \
11 | "test.c:5:20: error: array index 1 is past the end of the array (which contains 1 element) " \
12 | "[-Werror,-Warray-bounds]", \
13 | " printf(\\"%d\\\\n\\", a[1]);", \
14 | " ^ ~" \
15 | ])[1][0]
16 | True
17 | """
18 | matches = _match(r"array index (\d+) is past the end of the array", lines[0])
19 | if not matches:
20 | return
21 |
22 | array = _caret_extract(lines[1:3])
23 |
24 | if array:
25 | response = [
26 | "Careful, on line {} of `{}`, it looks like you're trying to access location {} of `{}`, which doesn't" \
27 | " exist; `{}` isn't that long.".format(matches.line, matches.file, matches.group[0], array, array)
28 | ]
29 | else:
30 | response = [
31 | "Careful, on line {} of `{}`, it looks like you're trying to access location {} of an array," \
32 | " which doesn't exist; the array isn't that long.".format(matches.line, matches.file, matches.group[0])
33 | ]
34 | response.append("Keep in mind that arrays are 0-indexed.")
35 |
36 | if array:
37 | return lines[0:3], response
38 |
39 | return lines[0:1], response
40 |
41 |
42 | @helper("clang")
43 | def array_subscript(lines):
44 | """
45 | >>> all(s in array_subscript([ \
46 | "foo.c:6:21: error: array subscript is not an integer", \
47 | " printf(\\"%i\\\\n\\", x[\\"28\\"]);", \
48 | " ^~~~~" \
49 | ])[1][0] for s in ["array `x`", "index (`\\"28\\"`)"])
50 | True
51 | """
52 | matches = _match(r"array subscript is not an integer", lines[0])
53 | if not matches:
54 | return
55 |
56 | array = _caret_extract(lines[1:3], left_aligned=False)
57 | index = _tilde_extract(lines[1:3])
58 |
59 | if array and index:
60 | response = [
61 | "Looks like you're trying to access an element of the array `{}` on line {} of `{}`, but your index (`{}`)" \
62 | " is not of type `int`.".format(array, matches.line, matches.file, index)
63 | ]
64 | if index[0] == index[-1] == '"':
65 | response.append("Right now, your index is of type `string` instead.")
66 | else:
67 | response = [
68 | "Looks like you're trying to access an element of an array on line {} of `{}`, but your index" \
69 | " is not of type `int`.".format(matches.line, matches.file)
70 | ]
71 |
72 | response.append("Make sure your index (the value between square brackets) is an `int`.")
73 |
74 | if len(lines) >= 2 and re.search(r"[.*]", lines[1]):
75 | return lines[0:2], response
76 |
77 | return lines[0:1], response
78 |
79 |
80 | @helper("clang")
81 | def assignment_as_condition(lines):
82 | """
83 | >>> "try using a double equals" in assignment_as_condition([ \
84 | "foo.c:6:10: error: using the result of an assignment as a condition without parentheses " \
85 | "[-Werror,-Wparentheses]", \
86 | " if (x = 28)", \
87 | " ~~^~~~" \
88 | ])[1][0]
89 | True
90 | """
91 | matches = _match(r"using the result of an assignment as a condition without parentheses", lines[0])
92 | if not matches:
93 | return
94 |
95 | response = [
96 | "When checking for equality in the condition on line {} of `{}`, try using a double equals sign (`==`) instead " \
97 | "of a single equals sign (`=`).".format(matches.line, matches.file)
98 | ]
99 |
100 | if len(lines) >= 2 and re.search(r"if\s*\(", lines[1]):
101 | return lines[0:2], response
102 |
103 | return lines[0:1], response
104 |
105 |
106 | @helper("clang")
107 | def bad_define(lines):
108 | """
109 | >>> "trying to define a constant" in bad_define([ \
110 | "foo.c:18:1: error: unknown type name 'define'", \
111 | "define _XOPEN_SOURCE 500", \
112 | "^" \
113 | ])[1][0]
114 | True
115 | """
116 | matches = _match(r"unknown type name 'define'", lines[0])
117 | if not matches:
118 | return
119 |
120 | response = [
121 | "If trying to define a constant on line {} of `{}`, be sure to use `#define` rather than" \
122 | " just `define`.".format(matches.line, matches.file)
123 | ]
124 |
125 | return lines[0:3 if len(lines) >= 3 else 1], response
126 |
127 |
128 | @helper("clang")
129 | def bad_include(lines):
130 | """
131 | >>> "trying to include a header" in bad_include([ \
132 | "foo.c:18:1: error: unknown type name 'include'", \
133 | "include ", \
134 | "^" \
135 | ])[1][0]
136 | True
137 | """
138 | matches = _match("unknown type name 'include'", lines[0])
139 | if not matches:
140 | return
141 |
142 | response = [
143 | "If trying to include a header file on line {} of `{}`, be sure to use `#include` rather than" \
144 | " just `include`.".format(matches.line, matches.file)
145 | ]
146 |
147 | return lines[0:3 if len(lines) >= 3 else 1], response
148 |
149 |
150 | @helper("clang")
151 | def comp_str_literal_unspecified(lines):
152 | """
153 | >>> "compare two strings" in comp_str_literal_unspecified([ \
154 | "foo.c:6:14: error: result of comparison against a string literal is unspecified (use strncmp instead) " \
155 | "[-Werror,-Wstring-compare]", \
156 | " if (word < \\"twenty-eight\\")", \
157 | " ^ ~~~~~~~~~~~~~~" \
158 | ])[1][0]
159 | True
160 | """
161 | matches = _match(r"result of comparison against a string literal is unspecified", lines[0])
162 | if not matches:
163 | return
164 |
165 | response = [
166 | "You seem to be trying to compare two strings on line {} of `{}`".format(matches.line, matches.file),
167 | "You can't compare two strings the same way you would compare two numbers (with `<`, `>`, etc.).",
168 | "Did you mean to compare two characters instead? If so, try using single quotation marks around characters " \
169 | "instead of double quotation marks.",
170 | "If you need to compare two strings, try using the `strcmp` function declared in `string.h`."
171 | ]
172 |
173 | if len(lines) >= 3 and _has_caret(lines[2]):
174 | return lines[0:3], response
175 |
176 | return lines[0:1], response
177 |
178 |
179 | @helper("clang")
180 | def conflicting_types(lines):
181 | """
182 | >>> "with a different return type" in conflicting_types([ \
183 | "foo.c:3:12: error: conflicting types for 'round'", \
184 | "int round(int n);", \
185 | " ^" \
186 | ])[1][0]
187 | True
188 | """
189 | matches = _match(r"conflicting types for '(.*)'", lines[0])
190 | if not matches:
191 | return
192 |
193 | response = [
194 | "Looks like you're redeclaring the function `{}`, but with a different return type on" \
195 | " line {} of `{}`.".format(matches.group[0], matches.line, matches.file)
196 | ]
197 |
198 | if len(lines) >= 4:
199 | prev_declaration = re.search(r"^([^:]+):(\d+):\d+: note: previous declaration is here", lines[3])
200 | if prev_declaration:
201 | if matches.file == prev_declaration.group(1):
202 | response.append("You had already declared this function on line {}.".format(prev_declaration.group(2)))
203 | else:
204 | response.append("The function `{}` is already declared in the library {}. Try renaming your" \
205 | " function.".format(matches.group[0], prev_declaration.group(1).split('/')[-1]))
206 |
207 | return lines[0:4], response
208 |
209 | return lines[0:1], response
210 |
211 |
212 | @helper("clang")
213 | def continue_not_in_loop(lines):
214 | """
215 | >>> "trying to use `continue`" in continue_not_in_loop([ \
216 | "test.c:51:17: error: 'continue' statement not in loop statement", \
217 | " continue;", \
218 | " ^" \
219 | ])[1][0]
220 | True
221 | """
222 | matches = _match(r"'continue' statement not in loop statement", lines[0])
223 | if not matches:
224 | return
225 |
226 | response = [
227 | "Looks like you're trying to use `continue` on line {} of `{}`, which isn't inside of a loop, but that keyword" \
228 | " can only be used inside of a loop.".format(matches.line, matches.file)
229 | ]
230 |
231 | return lines[0:1], response
232 |
233 |
234 | @helper("clang")
235 | def control_reaches_non_void(lines):
236 | """
237 | >>> "always return a value" in control_reaches_non_void([ \
238 | "foo.c:3:16: warning: control reaches end of non-void function [-Wreturn-type]", \
239 | "int foo(void) {}", \
240 | " ^" \
241 | ])[1][0]
242 | True
243 | """
244 | matches = _match(r"control (may )?reach(es)? end of non-void function", lines[0])
245 | if not matches:
246 | return
247 |
248 | response = [
249 | "Ensure that your function will always return a value. If your function is not meant to return a value," \
250 | " try changing its return type to `void`."
251 | ]
252 |
253 | return lines[0:1], response
254 |
255 |
256 | @helper("clang")
257 | def declaration_shadows_local_var(lines):
258 | """
259 | >>> "already been declared" in declaration_shadows_local_var([ \
260 | "foo.c:6:8: error: declaration shadows a local variable [-Werror,-Wshadow]", \
261 | " int x = 28;", \
262 | " ^", \
263 | "foo.c:5:13: note: previous declaration is here", \
264 | " int x = 2;", \
265 | " ^" \
266 | ])[1][0]
267 | True
268 |
269 | >>> "separated with a semicolon" in declaration_shadows_local_var([ \
270 | "bar.c:5:20: error: declaration shadows a local variable [-Werror,-Wshadow]", \
271 | " for (int i = 0, i < 28, i++)", \
272 | " ^" \
273 | ])[1][1]
274 | True
275 | """
276 | matches = _match(r"declaration shadows a local variable", lines[0])
277 | if not matches:
278 | return
279 |
280 | response = [
281 | "On line {} of `{}`, it looks like you're trying to declare a variable that's already been declared " \
282 | "elsewhere.".format(matches.line, matches.file)
283 | ]
284 |
285 | # check to see if declaration shadowing is due to for loop with commas instead of semicolons
286 | if len(lines) >= 2:
287 | for_loop = re.search(r"^\s*for\s*\(", lines[1])
288 | if for_loop:
289 | response.append("If you meant to create a `for` loop, be sure that each part of the `for` loop is separated " \
290 | "with a semicolon rather than a comma.")
291 |
292 | if (len(lines) >= 3 and re.search(r"^\s*\^$", lines[2])):
293 | return lines[0:3], response
294 |
295 | return lines[0:2], response
296 |
297 | # see if we can get the line number of the previous declaration of the variable
298 | prev_declaration_file = None
299 | prev_declaration_line = None
300 | if len(lines) >= 4:
301 | prev = re.search(r"^([^:]+):(\d+):\d+: note: previous declaration is here", lines[3])
302 | if prev:
303 | prev_declaration_line = prev.group(2)
304 | prev_declaration_file = prev.group(1)
305 |
306 | omit_suggestion = "If you meant to use the variable you've already declared previously"
307 | if prev_declaration_line and prev_declaration_file:
308 | omit_suggestion += " (on line {} of `{}`)".format(prev_declaration_line, prev_declaration_file)
309 |
310 | omit_suggestion += ", try getting rid of the data type of the variable on line {} of `{}`. You only need " \
311 | "to include the data type when you first declare a variable.".format(matches.line, matches.file)
312 |
313 | response.append(omit_suggestion)
314 | response.append("Otherwise, if you did mean to declare a new variable, try changing its name to a name that " \
315 | "hasn't been used yet.")
316 |
317 | if len(lines) >= 4 and prev_declaration_line != None:
318 | return (lines[0:7], response) if len(lines) >= 6 and re.search(r"^\s*\^$", lines[5]) else (lines[0:4], response)
319 |
320 | if len(lines) >= 2:
321 | return lines[0:2], response
322 |
323 | return lines[0:1], response
324 |
325 |
326 | @helper("clang")
327 | def div_by_zero(lines):
328 | """
329 | >>> "trying to divide by `0`" in div_by_zero([ \
330 | "foo.c:5:16: error: division by zero is undefined [-Werror,-Wdivision-by-zero]", \
331 | "int x = 28 / 0;", \
332 | " ^ ~" \
333 | ])[1][0]
334 | True
335 | """
336 | matches = _match(r"division by zero is undefined", lines[0])
337 | if not matches:
338 | return
339 |
340 | response = [
341 | "Looks like you're trying to divide by `0` (which isn't defined mathematically) on line {} of" \
342 | " `{}`.".format(matches.line, matches.file)
343 | ]
344 |
345 | if len(lines) >= 2:
346 | return lines[0:2], response
347 |
348 | return lines[0:1], response
349 |
350 |
351 | @helper("clang")
352 | def expected_closing_brace(lines):
353 | """
354 | >>> "matched with a closing brace" in expected_closing_brace([ \
355 | "foo.c:9:2: error: expected '}'", \
356 | "}", \
357 | " ^" \
358 | ])[1][0]
359 | True
360 | """
361 | matches = _match(r"expected '}'", lines[0])
362 | if not matches:
363 | return
364 |
365 | response = [
366 | "Make sure that all opening brace symbols `{` are matched with a closing brace `}`."
367 | ]
368 |
369 | return lines[0:1], response
370 |
371 |
372 | @helper("clang")
373 | def expected_closing_parens(lines):
374 | """
375 | >>> "matched with a closing parenthesis" in expected_closing_parens([ \
376 | "foo.c:6:1: error: expected ')'", \
377 | "}", \
378 | "^" \
379 | ])[1][0]
380 | True
381 | """
382 | matches = _match(r"expected '\)'", lines[0])
383 | if not matches:
384 | return
385 |
386 | # assume that the line number for the matching ')' is the line that generated the error
387 | match_line = matches.line
388 | n = 1
389 |
390 | # if there's a note on which '(' to match, use that line number instead
391 | if len(lines) >= 4:
392 | parens_match = re.search(r"^([^:]+):(\d+):\d+: note: to match this '\('", lines[3])
393 | if parens_match:
394 | match_line = parens_match.group(2)
395 | n = 4
396 |
397 | response = [
398 | "Make sure that all opening parentheses `(` are matched with a closing parenthesis" \
399 | " `)` in `{}`.".format(matches.file),
400 | "In particular, check to see if you are missing a closing parenthesis" \
401 | " on line {} of `{}`.".format(match_line, matches.file)
402 | ]
403 |
404 | return lines[0:n], response
405 |
406 | @helper("clang")
407 | def expected_for_semi_colon(lines):
408 | """
409 | >>> "separate the three components" in expected_for_semi_colon([ \
410 | "foo.c:5:22: error: expected ';' in 'for' statement specifier", \
411 | " for (int i = 0, i < 28, i++)", \
412 | " ^" \
413 | ])[1][0]
414 | True
415 | """
416 | matches = _match(r"expected ';' in 'for' statement specifier", lines[0])
417 | if not matches:
418 | return
419 |
420 | response = [
421 | "Be sure to separate the three components of the 'for' loop on line {} with " \
422 | "semicolons.".format(matches.file)
423 | ]
424 |
425 | if len(lines) >= 2 and re.search(r"for\s*\(", lines[1]):
426 | return lines[0:2], response
427 |
428 | return lines[0:1], response
429 |
430 |
431 | @helper("clang")
432 | def expected_identifier_or_parens(lines):
433 | """
434 | >>> "functions start and end" in expected_identifier_or_parens([ \
435 | "foo.c:1:17: error: expected identifier or '('", \
436 | "int main(void); {", \
437 | " ^", \
438 | "1 error generated." \
439 | ])[1][0]
440 | True
441 | """
442 | matches = _match(r"expected identifier or '\('", lines[0])
443 | if not matches:
444 | return
445 |
446 | response = [
447 | "Looks like `clang` is having some trouble understanding where your functions start and end in your code.",
448 | "Are you defining a function (like `main` or some other function) somewhere just before " \
449 | "line {} of `{}`?".format(matches.line, matches.file),
450 | "If so, make sure the function's first line doesn't end with a semicolon.",
451 | "Also, make sure that all of the code for your function is inside of curly braces."
452 | ]
453 |
454 | return lines[0:1], response
455 |
456 |
457 | @helper("clang")
458 | def expected_if_open_parens(lines):
459 | """
460 | >>> "enclosing the condition" in expected_if_open_parens([ \
461 | "foo.c:6:8: error: expected '(' after 'if'", \
462 | " if x == 28", \
463 | " ^" \
464 | ])[1][0]
465 | True
466 | """
467 | matches = _match(r"expected '\(' after 'if'", lines[0])
468 | if not matches:
469 | return
470 |
471 | response = [
472 | "In your `if` statement on line {} of `{}`, be sure that you're enclosing the condition you're testing within" \
473 | " parentheses.".format(matches.line, matches.file)
474 | ]
475 |
476 | if len(lines) >= 2 and re.search(r"if\s*\(", lines[1]):
477 | return lines[0:2], response
478 |
479 | return lines[0:1], response
480 |
481 |
482 | @helper("clang")
483 | def expected_param_declarator(lines):
484 | """
485 | >>> "calling it inside of curly" in expected_param_declarator([ \
486 | "foo.c:3:12: error: expected parameter declarator", \
487 | "int square(28);", \
488 | " ^", \
489 | ])[1][0]
490 | True
491 | """
492 | matches = _match(r"expected parameter declarator", lines[0])
493 | if not matches:
494 | return
495 |
496 | response = [
497 | "If you're trying to call a function on line {} of `{}`, be sure that you're calling it inside of curly braces " \
498 | "within a function. Also check that the function's header (the line introducing the function's name) doesn't " \
499 | "end in a semicolon.".format(matches.line, matches.file),
500 | "Alternatively, if you're trying to declare a function or prototype on line {} of `{}`, be sure each argument " \
501 | "to the function is formatted as a data type followed by a variable name.".format(matches.line, matches.file)
502 | ]
503 |
504 | if len(lines) >= 3 and re.search(r"^\s*\^$", lines[2]):
505 | return lines[0:3], response
506 |
507 | return lines[0:1], response
508 |
509 |
510 | @helper("clang")
511 | def expected_semi_colon(lines):
512 | """
513 | >>> "missing a semicolon" in expected_semi_colon([ \
514 | "foo.c:5:27: error: expected ';' after expression", \
515 | " printf(\\"hello, world!\\")", \
516 | " ^", \
517 | " ;" \
518 | ])[1][0]
519 | True
520 | """
521 | matches = _match(r"expected ';' (?:after expression|at end of declaration|after do\/while statement)", lines[0])
522 | if not matches:
523 | return
524 |
525 | response = [
526 | "Are you missing a semicolon at the end of line {} of `{}`?".format(matches.line, matches.file)
527 | ]
528 |
529 | if len(lines) >= 3 and re.search(r"^\s*\^$", lines[2]):
530 | if len(lines) >= 4 and re.search(r"^\s*;$", lines[3]):
531 | return lines[0:4], response
532 | return lines[0:3], response
533 |
534 | return lines[0:1], response
535 |
536 |
537 | @helper("clang")
538 | def expected_while_in_do_while(lines):
539 | """
540 | >>> "trying to create a `do/while`" in expected_while_in_do_while([ \
541 | "foo.c:9:1: error: expected 'while' in do/while loop", \
542 | "}", \
543 | "^" \
544 | ])[1][0]
545 | True
546 | """
547 | matches = _match(r"expected 'while' in do/while loop", lines[0])
548 | if not matches:
549 | return
550 |
551 | response = [
552 | "Looks like you're trying to create a `do/while` loop, but you've left off the `while` statement.",
553 | "Try adding `while` followed by a condition just before line {} of `{}`.".format(matches.line, matches.file)
554 | ]
555 | return lines[0:1], response
556 |
557 |
558 | @helper("clang")
559 | def expression_not_int_const(lines):
560 | """
561 | >>> "each `case` in a `switch`" in expression_not_int_const([ \
562 | "foo.c:7:15: error: expression is not an integer constant expression", \
563 | " case (x > 28):", \
564 | " ~^~~~~~~" \
565 | ])[1][0]
566 | True
567 | """
568 | matches = _match(r"expression is not an integer constant expression", lines[0])
569 | if not matches:
570 | return
571 |
572 | response = [
573 | "Remember that each `case` in a `switch` statement needs to be an integer (or a `char`, which is really just " \
574 | "an integer), not a Boolean expression or other type."
575 | ]
576 |
577 | return (lines[0:2], response) if len(lines) >= 2 else (lines[0:1], response)
578 |
579 |
580 | @helper("clang")
581 | def expression_result_unused(lines):
582 | """
583 | >>> "operation, but not saving" in expression_result_unused([ \
584 | "foo.c:6:16: error: expression result unused [-Werror,-Wunused-value]", \
585 | "n*12;", \
586 | " ^ 1 error generated." \
587 | ])[1][0]
588 | True
589 | """
590 | matches = _match(r"expression result unused", lines[0])
591 | if not matches:
592 | return
593 |
594 | response = [
595 | "On line {} of `{}` you are performing an operation, but not saving the result.".format(matches.line, matches.file),
596 | "Did you mean to print or store the result in a variable?"
597 | ]
598 |
599 | return lines[0:1], response
600 |
601 |
602 | @helper("clang")
603 | def extra_tokens_at_end_of_include(lines):
604 | """
605 | >>> "removing the `c`" in extra_tokens_at_end_of_include([ \
606 | "foo.c:1:19: error: extra tokens at end of #include directive [-Werror,-Wextra-tokens]", \
607 | "#include c", \
608 | " ^" \
609 | ])[1][2]
610 | True
611 | """
612 | matches = _match(r"extra tokens at end of #include directive", lines[0])
613 | if not matches:
614 | return
615 |
616 | response = [
617 | "You seem to have an error in `{}` on line {}.".format(matches.file, matches.line),
618 | "By \"extra tokens\", `clang` means that you have one or more extra characters on that line that you shouldn't."
619 | ]
620 |
621 | if len(lines) >= 3 and re.search(r"^\s*\^", lines[2]):
622 | token = lines[1][lines[2].index("^")]
623 | if token == ";":
624 | response.append("Try removing the semicolon at the end of that line.")
625 | else:
626 | response.append("Try removing the `{}` at the end of that line.".format(token))
627 |
628 | return lines[0:2], response
629 |
630 | return lines[0:1], response
631 |
632 |
633 | @helper("clang")
634 | def extraneous_closing_brace(lines):
635 | """
636 | >>> "have an unnecessary" in extraneous_closing_brace([ \
637 | "foo.c:21:1: error: extraneous closing brace ('}')", \
638 | "}", \
639 | "^" \
640 | ])[1][0]
641 | True
642 | """
643 | matches = _match(r"extraneous closing brace \('}'\)", lines[0])
644 | if not matches:
645 | return
646 |
647 | response = [
648 | "You seem to have an unnecessary `}}` on line {} of `{}`.".format(matches.line, matches.file)
649 | ]
650 |
651 | if len(lines) >= 3 and re.search(r"^\s*\^\s*$", lines[2]):
652 | return lines[0:3], response
653 |
654 | return lines[0:1], response
655 |
656 |
657 | @helper("clang")
658 | def extraneous_closing_parens(lines):
659 | """
660 | >>> "have an extra parenthesis" in extraneous_closing_parens([ \
661 | "foo.c:1:19: error: extraneous ')' before ';' [-Werror]", \
662 | "digit = (number % (tracker)) / (tracker/10));", \
663 | " ^" \
664 | ])[1][0]
665 | True
666 | """
667 | matches = _match(r"extraneous '\)' before ';'", lines[0])
668 | if not matches:
669 | return
670 |
671 | response = [
672 | "You seem to have an extra parenthesis on line {} of `{}`, just before " \
673 | "the semicolon.".format(matches.line, matches.file)
674 | ]
675 |
676 | if len(lines) >= 3 and _has_caret(lines[2]):
677 | return lines[0:3], response
678 |
679 | return lines[0:1], response
680 |
681 |
682 | @helper("clang")
683 | def file_not_found_include(lines):
684 | """
685 | >>> "trying to `#include`" in file_not_found_include([ \
686 | "foo.c:1:10: fatal error: 'studio.h' file not found", \
687 | "#include ", \
688 | " ^" \
689 | ])[1][0]
690 | True
691 | """
692 | matches = _match(r"'(.*)' file not found", lines[0])
693 | if not matches:
694 | return
695 |
696 | response = [
697 | "Looks like you're trying to `#include` a file (`{}`) on line {} of `{}` which " \
698 | "does not exist.".format(matches.group[0], matches.line, matches.file)
699 | ]
700 |
701 | if matches.group[0] in ["studio.h"]:
702 | response.append("Did you mean to `#include ` (without the `u`)?")
703 | else:
704 | response.append("Check to make sure you spelled the filename correctly.")
705 |
706 | if len(lines) >= 2 and re.search(r"#include", lines[1]):
707 | return lines[0:2], response
708 |
709 | return lines[0:1], response
710 |
711 |
712 | # TODO: pattern match on argument's type
713 | @helper("clang")
714 | def fmt_specifies_type(lines):
715 | """
716 | >>> "correct format code" in fmt_specifies_type([ \
717 | "foo.c:5:19: error: format specifies type 'int' but the argument has type 'char *' [-Werror,-Wformat]", \
718 | " printf(\\"%d\\n\\", \\"hello!\\");", \
719 | " ~~ ^~~~~~~~", \
720 | " %s" \
721 | ])[1][0]
722 | True
723 | """
724 | matches = _match(r"format specifies type '[^:]+' but the argument has type '[^:]+'", lines[0])
725 | if not matches:
726 | return
727 |
728 | response = [
729 | "Be sure to use the correct format code (e.g., `%i` for integers, `%f` for floating-point values, `%s` for " \
730 | "strings, etc.) in your format string on line {} of `{}`.".format(matches.line, matches.file)
731 | ]
732 |
733 | if len(lines) >= 3 and re.search(r"\^", lines[2]):
734 | if len(lines) >= 4 and re.search(r"%", lines[3]):
735 | return lines[0:4], response
736 |
737 | return lines[0:3], response
738 |
739 | return lines[0:1], response
740 |
741 |
742 | @helper("clang")
743 | def fmt_string_not_string_literal(lines):
744 | """
745 | >>> "double-quoted string" in fmt_string_not_string_literal([ \
746 | "foo.c:6:16: error: format string is not a string literal (potentially insecure) " \
747 | "[-Werror,-Wformat-security]", \
748 | "printf(c);", \
749 | " ^" \
750 | ])[1][0]
751 | True
752 | """
753 | matches = _match(r"format string is not a string literal", lines[0])
754 | if matches and len(lines) >= 3 and re.search(r"^\s*\^", lines[2]):
755 | line = matches.line
756 | file = matches.file
757 |
758 | backtrack = len("vsnprintf(") # the longest possible variant of printf
759 | start = max(lines[2].index("^") - backtrack, 0)
760 | matches = re.search(r"(\w*printf|\w*scanf)\s*\(", lines[1][start:])
761 | if matches:
762 | response = [
763 | "The first argument to `{}` on line {} of `{}` should be a double-quoted " \
764 | "string.".format(matches.group(1), line, file)
765 |
766 | ]
767 | return lines[0:2], response
768 |
769 |
770 | @helper("clang")
771 | def has_empty_body(lines):
772 | """
773 | >>> "removing the semicolon directly" in has_empty_body([ \
774 | "foo.c:12:15: error: if statement has empty body [-Werror,-Wempty-body]", \
775 | " if (n > 0);", \
776 | " ^" \
777 | ])[1][0]
778 | True
779 | """
780 | matches = _match(r"(if statement|while loop|for loop) has empty body", lines[0])
781 | if not matches:
782 | return
783 |
784 | response = [
785 | "Try removing the semicolon directly after the closing parentheses of the `{}` on line {} of " \
786 | "`{}`.".format(matches.group[0],matches.line, matches.file)
787 | ]
788 |
789 | if len(lines) >= 2 and re.search(r"if\s*\(", lines[1]):
790 | return lines[0:2], response
791 |
792 | return lines[0:1], response
793 |
794 |
795 | @helper("clang")
796 | def ignoring_return_value(lines):
797 | """
798 | >>> "calling `round`" in ignoring_return_value([ \
799 | "round.c:17:5: error: ignoring return value of function declared with const attribute " \
800 | "[-Werror,-Wunused-value]", \
801 | "round(cents);", \
802 | "^~~~~ ~~~~~" \
803 | ])[1][0]
804 | True
805 | """
806 | matches = _match(r"ignoring return value of function declared with (.+) attribute", lines[0])
807 | if not matches:
808 | return
809 |
810 | function = _caret_extract(lines[1:3])
811 | if function:
812 | response = [
813 | "You seem to be calling `{}` on line {} of `{}` but aren't using its " \
814 | "return value.".format(function, matches.line, matches.file),
815 | "Did you mean to assign it to a variable?"
816 | ]
817 |
818 | return lines[0:3], response
819 |
820 | response = [
821 | "You seem to be calling a function on line {} of `{}` but aren't using its " \
822 | "return value.".format(matches.line, matches.file),
823 | "Did you mean to assign it to a variable?"
824 | ]
825 |
826 | return lines[0:1], response
827 |
828 |
829 | @helper("clang")
830 | def implicit_declaration_of_fun(lines):
831 | """
832 | >>> "implicit declaration of function" in implicit_declaration_of_fun([ \
833 | "foo.c:5:12: error: implicit declaration of function 'get_int' is invalid in C99 " \
834 | "[-Werror,-Wimplicit-function-declaration]", \
835 | " int x = get_int();", \
836 | " ^" \
837 | ])[1][1]
838 | True
839 | """
840 | matches = _match(r"implicit declaration of function '([^']+)' is invalid", lines[0])
841 | if not matches:
842 | return
843 |
844 | response = [
845 | "You seem to have an error in `{}` on line {}.".format(matches.file, matches.line),
846 | "By \"implicit declaration of function '{}'\", `clang` means that it doesn't recognize " \
847 | "`{}`.".format(matches.group[0], matches.group[0])
848 | ]
849 |
850 | if matches.group[0] in ["get_char", "get_double", "get_float", "get_int", "get_long", "get_long_long",
851 | "get_string", "GetChar", "GetDouble", "GetFloat", "GetInt", "GetLong", "GetLongLong",
852 | "GetString"]:
853 | response.append("Did you forget to `#include ` (in which `{}` is declared) atop " \
854 | "your file?".format(matches.group[0]))
855 | elif matches.group[0] in ["crypt"]:
856 | response.append("Did you forget to `#include ` (in which `{}` is declared) atop " \
857 | "your file?".format(matches.group[0]))
858 | response.append("Do you have `#define _XOPEN_SOURCE` above, not below, `#include `?")
859 | elif matches.group[0] in ["eprintf"]:
860 | response.append("The function `eprintf` has been deprecated and is thus no longer part of the CS50 library.")
861 | else:
862 | response.append("Did you forget to `#include` the header file in which `{}` is declared atop " \
863 | "your file?".format(matches.group[0]))
864 | response.append("Did you forget to declare a prototype for `{}` atop `{}`?".format(matches.group[0], matches.file))
865 |
866 | if len(lines) >= 2 and matches.group[0] in lines[1]:
867 | return lines[0:2], response
868 |
869 | return lines[0:1], response
870 |
871 |
872 | @helper("clang")
873 | def implicitly_declaring_lib_fun(lines):
874 | """
875 | >>> "Did you forget to" in implicitly_declaring_lib_fun([ \
876 | "foo.c:3:4: error: implicitly declaring library function 'printf' with type 'int (const char *, ...)' " \
877 | "[-Werror]", \
878 | " printf(\\"hello, world!\\");", \
879 | " ^" \
880 | ])[1][0]
881 | True
882 | """
883 | matches = _match(r"implicitly declaring library function '([^']+)'", lines[0])
884 | if not matches:
885 | return
886 |
887 | if matches.group[0] in ["printf"]:
888 | response = ["Did you forget to `#include ` (in which `printf` is declared) atop your file?"]
889 | elif matches.group[0] in ["malloc"]:
890 | response = ["Did you forget to `#include ` (in which `malloc` is declared) atop your file?"]
891 | else:
892 | response = ["Did you forget to `#include` the header file in which `{}` is declared atop " \
893 | "your file?".format(matches.group[0])]
894 |
895 | if len(lines) >= 2 and re.search(r"printf\s*\(", lines[1]):
896 | return lines[0:2], response
897 |
898 | return lines[0:1], response
899 |
900 |
901 | @helper("clang")
902 | def incompatible_conversion(lines):
903 | """
904 | >>> "By \\"incompatible conversion\\"," in incompatible_conversion([ \
905 | "foo.c:3:8: error: incompatible pointer to integer conversion initializing 'int' with an expression of " \
906 | "type 'char [3]'", \
907 | " [-Werror,-Wint-conversion]", \
908 | " int x = \\"28\\";", \
909 | " ^ ~~~~" \
910 | ])[1][0]
911 | True
912 | """
913 | matches = _match(r"incompatible (.+) to (.+) conversion", lines[0])
914 | if not matches:
915 | return
916 |
917 | response = [
918 | "By \"incompatible conversion\", `clang` means that you are assigning a value to a variable of a different type " \
919 | "on line {} of `{}`. Try ensuring that your value is of " \
920 | "type `{}`.".format(matches.line, matches.file, matches.group[1])
921 | ]
922 |
923 | if len(lines) >= 2 and re.search(r"=", lines[1]):
924 | return lines[0:2], response
925 |
926 | return lines[0:1], response
927 |
928 |
929 | @helper("clang")
930 | def index_out_of_bounds(lines):
931 | """
932 | >>> "but that location is" in index_out_of_bounds([ \
933 | "foo.c:7:5: runtime error: index 2 out of bounds for type 'int [2]'", \
934 | ])[1][0]
935 | True
936 | """
937 | matches = _match(r"index (-?\d+) out of bounds for type '.+'", lines[0])
938 | if not matches:
939 | return
940 |
941 | response = []
942 | if int(matches.group[0]) < 0:
943 | response.append("Looks like you're to access location {} of an array on line {} of `{}`, but that location is " \
944 | "before the start of the array.".format(matches.group[0], matches.line, matches.file))
945 | else:
946 | response.append("Looks like you're to access location {} of an array on line {} of `{}`, but that location is " \
947 | "past the end of the array.".format(matches.group[0], matches.line, matches.file, matches.group[0]))
948 |
949 | return lines[0:1], response
950 |
951 |
952 | @helper("clang")
953 | def invalid_append_string(lines):
954 | """
955 | >>> "concatenate values and strings in C" in invalid_append_string([ \
956 | "test.c:6:15: error: adding 'char' to a string does not append to the string [-Werror, -Wstring-plus-int]", \
957 | " printf(\"\" + c);" \
958 | ])[1][0]
959 | True
960 | """
961 | matches = _match(r"adding '(.+)' to a string does not append to the string", lines[0])
962 | if not matches:
963 | return
964 |
965 | response = [
966 | "Careful, you can't concatenate values and strings in C using the `+` operator, " \
967 | "as you seem to be trying to do on line {} of `{}`.".format(matches.line, matches.file)
968 | ]
969 |
970 | if len(lines) >= 2 and re.search(r"printf\s*\(", lines[1]):
971 | response.append("Odds are you want to provide `printf` with a format code for that value and pass that value to" \
972 | " `printf` as an argument.")
973 |
974 | return lines, response
975 |
976 | return lines[0:1], response
977 |
978 |
979 | @helper("clang")
980 | def invalid_equals(lines):
981 | """
982 | >>> "comparing two values for equality" in invalid_equals([ \
983 | "foo.c:8:19: error: invalid '==' at end of declaration; did you mean '='?", \
984 | " for(int i == 0; i < height; i++)", \
985 | " ^~", \
986 | " =" \
987 | ])[1][0]
988 | True
989 | """
990 | matches = _match(r"invalid '==' at end of declaration; did you mean '='?", lines[0])
991 | if not matches:
992 | return
993 |
994 | response = [
995 | "Looks like you may have used '==' (which is used for comparing two values for equality) instead of '=' (which " \
996 | "is used to assign a value to a variable) on line {} of `{}`?".format(matches.line, matches.file)
997 | ]
998 |
999 | return lines[0:1], response
1000 |
1001 |
1002 | @helper("clang")
1003 | def invalid_preprocessing_directive(lines):
1004 | """
1005 | >>> "you've used a preprocessor" in invalid_preprocessing_directive([ \
1006 | "foo.c:1:2: error: invalid preprocessing directive", \
1007 | "#incalude ", \
1008 | " ^" \
1009 | ])[1][0]
1010 | True
1011 | """
1012 | matches = _match(r"invalid preprocessing directive", lines[0])
1013 | if not matches:
1014 | return
1015 |
1016 | response = [
1017 | "By \"invalid preprocesing directive\", `clang` means that you've used a preprocessor command on line {} " \
1018 | "(a command beginning with #) that is not recognized.".format(matches.file)
1019 | ]
1020 |
1021 | if len(lines) >= 2:
1022 | directive = re.search(r"^([^' ]+)", lines[1])
1023 | if directive:
1024 | response.append("Check to make sure that `{}` is a valid directive (like `#include`) and is spelled " \
1025 | "correctly.".format(directive.group(1)))
1026 |
1027 | return lines[0:2], response
1028 |
1029 | return lines[0:1], response
1030 |
1031 |
1032 | @helper("clang")
1033 | def main_must_return_int(lines):
1034 | """
1035 | >>> "type of `void`" in main_must_return_int([ \
1036 | "foo.c:3:1: error: 'main' must return 'int'", \
1037 | "void main(void)", \
1038 | "^~~~", \
1039 | "int" \
1040 | ])[1][1]
1041 | True
1042 | """
1043 | matches = _match(r"'main' must return 'int'", lines[0])
1044 | if not matches:
1045 | return
1046 |
1047 | response = [
1048 | "Your `main` function (declared on line {} of `{}`) must have a return " \
1049 | "type `int`.".format(matches.line, matches.file)
1050 | ]
1051 |
1052 | cur_type = _caret_extract(lines[1:3])
1053 | if len(lines) >= 3 and cur_type:
1054 | response.append("Right now, it has a return type of `{}`.".format(cur_type))
1055 |
1056 | return lines[0:3], response
1057 |
1058 | return lines[0:1], response
1059 |
1060 |
1061 | @helper("clang")
1062 | def missing_parens(lines):
1063 | """
1064 | >>> "call `get_float`" in missing_parens([ \
1065 | "foo.c:1:3: error: assigning to 'float' from incompatible type 'float (void)'", \
1066 | " f = get_float", \
1067 | " ^ ~~~~~~~~~" \
1068 | ])[1][0]
1069 | True
1070 | """
1071 | matches = _match(r"assigning to '(.+)' from incompatible type '.+ \(.+\)'", lines[0])
1072 | if not matches:
1073 | return
1074 |
1075 | function = _tilde_extract(lines[1:3])
1076 | if function:
1077 | response = [
1078 | "Looks like you're trying to call `{}` on line {} of `{}`, but did you forget parentheses after the" \
1079 | " function's name?".format(function, matches.line, matches.file)
1080 | ]
1081 |
1082 | return lines[0:3], response;
1083 |
1084 | response = [
1085 | "Looks like you're trying to call a function on line {} of `{}`, but did you forget parentheses after the" \
1086 | " function's name?".format(matches.line, matches.file)
1087 | ]
1088 |
1089 | return lines[0:1], response;
1090 |
1091 |
1092 | @helper("clang")
1093 | def more_conversions_than_data_args(lines):
1094 | """
1095 | >>> "too many format codes" in more_conversions_than_data_args([ \
1096 | "foo.c:5:16: error: more '%' conversions than data arguments [-Werror,-Wformat]" \
1097 | " printf(\\"%d %d\\n\\", 28);", \
1098 | " ~^" \
1099 | ])[1][0]
1100 | True
1101 | """
1102 | matches = _match(r"more '%' conversions than data arguments", lines[0])
1103 | if not matches:
1104 | return
1105 |
1106 | response = [
1107 | "You have too many format codes in your format string on line {} of `{}`.".format(matches.line, matches.file),
1108 | "Be sure that the number of format codes equals the number of additional arguments."
1109 | ]
1110 |
1111 | if len(lines) >= 2 and re.search(r"%", lines[1]):
1112 | return lines[0:2], response
1113 |
1114 | return lines[0:1], response
1115 |
1116 |
1117 | @helper("clang")
1118 | def multiple_unsequenced_modifications(lines):
1119 | """
1120 | >>> "the variable `space`" in multiple_unsequenced_modifications([ \
1121 | "foo.c:1:10: error: multiple unsequenced modifications to 'space' [-Werror,-Wunsequenced]", \
1122 | " space = space--;", \
1123 | " ~ ^" \
1124 | ])[1][0]
1125 | True
1126 | """
1127 | matches = _match(r"multiple unsequenced modifications to '(.*)'", lines[0])
1128 | if not matches:
1129 | return
1130 |
1131 | variable = matches.group[0]
1132 | response = [
1133 | "Looks like you're changing the variable `{}` multiple times in a row on line {} of " \
1134 | "`{}`.".format(variable, matches.line, matches.file)
1135 | ]
1136 |
1137 | if len(lines) >= 2:
1138 | file = matches.file
1139 | line = matches.line
1140 | matches = re.search(r"(--|\+\+)", lines[1])
1141 | if matches:
1142 | response.append("When using the `{}` operator, there is no need to assign the result to the variable. Try " \
1143 | "using just `{}{}` instead".format(matches.group(1), variable, matches.group(1)))
1144 |
1145 | return lines[0:2], response
1146 |
1147 | return lines[0:1], response
1148 |
1149 |
1150 | @helper("clang")
1151 | def one_param_on_main_dec(lines):
1152 | """
1153 | >>> "be `int main(void)` or" in one_param_on_main_dec([ \
1154 | "foo.c:6:5: error: only one parameter on 'main' declaration [-Werror,-Wmain]", \
1155 | "int main(int x)", \
1156 | " ^" \
1157 | ])[1][0]
1158 | True
1159 | """
1160 | matches = _match(r"only one parameter on 'main' declaration", lines[0])
1161 | if not matches:
1162 | return
1163 |
1164 | response = [
1165 | "Looks like your declaration of `main` on line {} of `{}` isn't quite right. The declaration of `main` should " \
1166 | "be `int main(void)` or `int main(int argc, string argv[])` or some " \
1167 | "equivalent.".format(matches.line, matches.file)
1168 | ]
1169 |
1170 | return lines[0:1], response
1171 |
1172 |
1173 | @helper("clang")
1174 | def relational_comp_res_unused(lines):
1175 | """
1176 | >>> "comparing two values" in relational_comp_res_unused([ \
1177 | "mario.c:12:19: error: relational comparison result unused [-Werror,-Wunused-comparison]", \
1178 | " while (height < 0, height < 23);", \
1179 | " ~~~~~~~^~~", \
1180 | "1 error generated.", \
1181 | "make: *** [mario] Error 1" \
1182 | ])[1][0]
1183 | True
1184 | """
1185 | matches = _match(r"relational comparison result unused", lines[0])
1186 | if not matches:
1187 | return
1188 |
1189 | response = [
1190 | "Looks like you're comparing two values on line {} of `{}` but not using the " \
1191 | "result?".format(matches.line, matches.file)
1192 | ]
1193 |
1194 | if len(lines) >= 3 and _has_caret(lines[2]):
1195 | return lines[0:3], response
1196 |
1197 | return lines[0:1], response
1198 |
1199 |
1200 | @helper("clang")
1201 | def second_param_main_char(lines):
1202 | """
1203 | >>> "declaration of `main`" in second_param_main_char([ \
1204 | "caesar.c:5:5: error: second parameter of 'main' (argument array) must be of type 'char **'", \
1205 | "int main(int argc, int argv[])", \
1206 | " ^" \
1207 | ])[1][0]
1208 | True
1209 | """
1210 | matches = _match(r"second parameter of 'main' \(argument array\) must be of type 'char \*\*'", lines[0])
1211 | if not matches:
1212 | return
1213 |
1214 | response = [
1215 | "Looks like your declaration of `main` isn't quite right.",
1216 | "Be sure its second parameter is `string argv[]` or some equivalent!"
1217 | ]
1218 |
1219 | return lines[0:1], response
1220 |
1221 |
1222 | @helper("clang")
1223 | def self_initialization(lines):
1224 | """
1225 | >>> "both the left- and right-hand" in self_initialization([ \
1226 | "water.c:8:15: error: variable 'x' is uninitialized when used within its own initialization" \
1227 | " [-Werror,-Wuninitialized]", \
1228 | "int x= 12*x;", \
1229 | " ~ ^" \
1230 | ])[1][0]
1231 | True
1232 | """
1233 | matches = _match(r"variable '(.+)' is uninitialized when used within its own initialization", lines[0])
1234 | if not matches:
1235 | return
1236 |
1237 | response = [
1238 | "Looks like you have `{}` on both the left- and right-hand side of the `=` on line {} of `{}`, but `{}` doesn't" \
1239 | " yet have a value.".format(matches.group[0], matches.line, matches.file, matches.group[0]),
1240 | "Be sure not to initialize `{}` with itself.".format(matches.group[0])
1241 | ]
1242 |
1243 | if len(lines) >= 3 and re.search(r"^\s*~\s*\^\s*$", lines[2]):
1244 | return lines[0:3], response
1245 |
1246 | return lines[0:1], response
1247 |
1248 |
1249 | # TODO: extract symbol
1250 | @helper("clang")
1251 | def subscripted_val_not_array(lines):
1252 | """
1253 | >>> "index into a variable" in subscripted_val_not_array([ \
1254 | "fifteen.c:179:21: error: subscripted value is not an array, pointer, or vector", \
1255 | "temp = board[d - 1][d - 2];", \
1256 | " ~~~~~^~~~~~" \
1257 | ])[1][0]
1258 | True
1259 | """
1260 | matches = _match(r"subscripted value is not an array, pointer, or vector", lines[0])
1261 | if not matches:
1262 | return
1263 |
1264 | response = [
1265 | "Looks like you're trying to index into a variable as though it's an array, even though it isn't, on line " \
1266 | "{} of `{}`?".format(matches.line, matches.file)
1267 | ]
1268 |
1269 | return lines[0:1], response
1270 |
1271 |
1272 | @helper("clang")
1273 | def too_many_args_to_fun_call(lines):
1274 | """
1275 | >>> "The function `hashtag`" in too_many_args_to_fun_call([ \
1276 | "mario.c:18:17: error: too many arguments to function call, expected 0, have 1", \
1277 | " hashtag(x);", \
1278 | " ~~~~~~~ ^" \
1279 | ])[1][1]
1280 | True
1281 | """
1282 | matches = _match(r"too many arguments to function call, expected (\d+), have (\d+)", lines[0])
1283 | if not matches:
1284 | return
1285 |
1286 | function = _tilde_extract(lines[1:3]) if len(lines) >= 3 else None
1287 |
1288 | response = [
1289 | "You seem to be passing in too many arguments to a function on line {} of `{}`.".format(matches.line, matches.file)
1290 | ]
1291 |
1292 | if function:
1293 | response.append("The function `{}`".format(function))
1294 | else:
1295 | response.append("The function")
1296 |
1297 | response[1] += " is supposed to take {} argument(s), but you're passing it " \
1298 | "{}.".format(matches.group[0], matches.group[1])
1299 | response.append("Try providing {} fewer argument(s) to the " \
1300 | "function.".format(str(int(matches.group[1]) - int(matches.group[0]))))
1301 |
1302 | if function:
1303 | return lines[0:3], response
1304 |
1305 | return lines[0:1], response
1306 |
1307 |
1308 | @helper("clang")
1309 | def type_specifier_missing(lines):
1310 | """
1311 | >>> "specify its return type" in type_specifier_missing([ \
1312 | "foo.c:3:1: error: type specifier missing, defaults to 'int' [-Werror,-Wimplicit-int]", \
1313 | "square (int x) {", \
1314 | "^" \
1315 | ])[1][1]
1316 | True
1317 | """
1318 | matches = _match(r"type specifier missing, defaults to 'int'", lines[0])
1319 | if not matches:
1320 | return
1321 |
1322 | response = [
1323 | "Looks like you're trying to declare a function on line {} of `{}`.".format(matches.line, matches.file),
1324 | "Be sure, when declaring a function, to specify its return type just before its name."
1325 | ]
1326 |
1327 | if len(lines) >= 3 and re.search(r"^\s*\^$", lines[2]):
1328 | return lines[0:3], response
1329 |
1330 | return lines[0:1], response
1331 |
1332 |
1333 | @helper("clang")
1334 | def undefined_reference(lines):
1335 | """
1336 | >>> "Did you try to compile" in undefined_reference([ \
1337 | "foo.c:(.text+0x20): undefined reference to `main'", \
1338 | "clang: error: linker command failed with exit code 1 (use -v to see invocation)" \
1339 | ])[1][0]
1340 | True
1341 |
1342 | >>> "seem to be implemented" in undefined_reference([ \
1343 | "foo.c:(.text+0x9): undefined reference to `get_int'" \
1344 | ])[1][0]
1345 | True
1346 | """
1347 | matches = _match(r"undefined reference to `([^']+)'", lines[0], raw=True)
1348 | if not matches:
1349 | return
1350 |
1351 | if matches.group[0] == "main":
1352 | response = [
1353 | "Did you try to compile a file that doesn't contain a `main` function?"
1354 | ]
1355 |
1356 | if len(lines) > 3 and "make: *** [helpers] Error" in lines[2]:
1357 | response.append("Are you compiling a `helpers.c` file instead of the file containing the program itself?")
1358 | else:
1359 | response = [
1360 | "By \"undefined reference,\" `clang` means that you've called a function, `{}`, that doesn't seem to be " \
1361 | "implemented.".format(matches.group[0]),
1362 | "If that function has, in fact, been implemented, odds are you've forgotten to tell `clang` to \"link\" " \
1363 | "against the file that implements `{}`.".format(matches.group[0])
1364 | ]
1365 |
1366 | if matches.group[0] in ["get_char", "get_double", "get_float", "get_int", "get_long", "get_long_long",
1367 | "get_string"]:
1368 | response.append("Did you forget to compile with `-lcs50` in order to link against against the CS50 Library, " \
1369 | "which implements `{}`?".format(matches.group[0]))
1370 | elif matches.group[0] in ["GetChar", "GetDouble", "GetFloat", "GetInt", "GetLong", "GetLongLong", "GetString"]:
1371 | response.append("Did you forget to compile with `-lcs50` in order to link against against the CS50 Library, " \
1372 | "which implements `{}`?".format(matches.group[0]))
1373 | elif matches.group[0] == "crypt":
1374 | response.append("Did you forget to compile with -lcrypt in order to link against the crypto library, " \
1375 | "which implemens `crypt`?")
1376 | else:
1377 | response.append("Did you forget to compile with `-lfoo`, where `foo` is the library that defines " \
1378 | "`{}`?".format(matches.group[0]))
1379 |
1380 | return lines[0:1], response
1381 |
1382 |
1383 | @helper("clang")
1384 | def unknown_escape_sequence(lines):
1385 | """
1386 | >>> "space immediately after" in unknown_escape_sequence([ \
1387 | "water.c:9:21: error: unknown escape sequence '\\ ' [-Werror,-Wunknown-escape-sequence]", \
1388 | "printf(\\"bottles: %i \\ n\\", shower);", \
1389 | " ^~" \
1390 | ])[1][0]
1391 | True
1392 | """
1393 | matches = _match(r"unknown escape sequence '\\ '", lines[0])
1394 | if not matches:
1395 | return
1396 |
1397 | response = [
1398 | "Looks like you have a space immediately after a backslash on line {} of `{}` but " \
1399 | "shouldn't.".format(matches.line, matches.file)
1400 | ]
1401 |
1402 | if len(lines) >= 3 and _has_caret(lines[2]):
1403 | response.append("Did you mean to escape some character?")
1404 | return lines[0:3], response
1405 |
1406 | response.append("Did you mean to escape some character?")
1407 | return lines[0:1], response
1408 |
1409 |
1410 | @helper("clang")
1411 | def unknown_type(lines):
1412 | """
1413 | >>> "type, even though" in unknown_type([ \
1414 | "foo.c:1:1: error: unknown type name 'bar'", \
1415 | "bar baz", \
1416 | "^" \
1417 | ])[1][0]
1418 | True
1419 | """
1420 | matches = _match(r"unknown type name '(.+)'", lines[0])
1421 | if not matches:
1422 | return
1423 |
1424 | if matches.group[0] == "define":
1425 | return bad_define(lines)
1426 | if matches.group[0] == "include":
1427 | return bad_include(lines)
1428 |
1429 | response = [
1430 | "You seem to be using `{}` on line {} of `{}` as though it's a type, even though it's not been defined as" \
1431 | " one.".format(matches.group[0], matches.line, matches.file)
1432 | ]
1433 |
1434 | if matches.group[0] == "bool":
1435 | response.append("Did you forget `#include ` or `#include ` atop `{}`?".format(matches.file))
1436 | elif matches.group[0] == "size_t":
1437 | response.append("Did you forget `#include ` atop `{}`?".format(matches.file))
1438 | elif matches.group[0] == "string":
1439 | response.append("Did you forget `#include ` atop `{}`?".format(matches.file))
1440 | else:
1441 | response.append("Did you perhaps misspell `{}` or forget to `typedef` it?".format(matches.group[0]))
1442 |
1443 | return lines[0:3 if len(lines) >= 3 else 1], response
1444 |
1445 |
1446 | @helper("clang")
1447 | def unused_arg_in_fmt_string(lines):
1448 | """
1449 | >>> "more arguments" in unused_arg_in_fmt_string([ \
1450 | "foo.c:5:29: error: data argument not used by format string [-Werror,-Wformat-extra-args]", \
1451 | " printf(\\"%d %d\\", 27, 28, 29);", \
1452 | " ~~~~~~~ ^" \
1453 | ])[1][0]
1454 | True
1455 | """
1456 | matches = _match(r"data argument not used by format string", lines[0])
1457 | if not matches:
1458 | return
1459 |
1460 | response = [
1461 | "You have more arguments in your formatted string on line {} of `{}` than you have" \
1462 | " format codes.".format(matches.line, matches.file),
1463 | "Make sure that the number of format codes equals the number of additional arguments.",
1464 | "Try either adding format code(s) or removing argument(s)."
1465 | ]
1466 |
1467 | if len(lines) >= 2 and re.search(r"%", lines[1]):
1468 | return lines[0:2], response
1469 |
1470 | return lines[0:1], response
1471 |
1472 |
1473 | @helper("clang")
1474 | def unused_var(lines):
1475 | """
1476 | >>> "never used in your program" in unused_var([ \
1477 | "foo.c:6:9: error: unused variable 'x' [-Werror,-Wunused-variable]", \
1478 | " int x = 28;", \
1479 | " ^" \
1480 | ])[1][0]
1481 | True
1482 | """
1483 | matches = _match(r"unused variable '([^']+)'", lines[0])
1484 | if not matches:
1485 | return
1486 |
1487 | response = [
1488 | "It seems that the variable `{}` (declared on line {} of `{}`) is never used in your program. Try either removing" \
1489 | " it altogether or using it.".format(matches.group[0], matches.line, matches.file)
1490 | ]
1491 |
1492 | return lines[0:1], response
1493 |
1494 |
1495 | @helper("clang")
1496 | def use_of_undeclared_indentifier(lines):
1497 | """
1498 | >>> "hasn't been defined" in use_of_undeclared_indentifier([ \
1499 | "foo.c:5:4: error: use of undeclared identifier 'x'", \
1500 | " x = 28;", \
1501 | " ^" \
1502 | ])[1][0]
1503 | True
1504 | """
1505 | matches = _match(r"use of undeclared identifier '([^']+)'", lines[0])
1506 | if not matches:
1507 | return
1508 |
1509 | response = [
1510 | "By \"undeclared identifier,\" `clang` means you've used a name `{}` on line {} of `{}` which hasn't been " \
1511 | "defined.".format(matches.group[0], matches.line, matches.file)
1512 | ]
1513 |
1514 | if matches.group[0] in ["true", "false", "bool", "string"]:
1515 | response.append("Did you forget to `#include ` (in which `{}` is defined) atop your " \
1516 | "file?".format(matches.group[0]))
1517 | else:
1518 | response.append("If you mean to use `{}` as a variable, make sure to declare it by specifying its type, and " \
1519 | "check that the variable name is spelled correctly.".format(matches.group[0]))
1520 |
1521 | if len(lines) >= 2 and matches.file in lines[1]:
1522 | return lines[0:2], response
1523 |
1524 | return lines[0:1], response
1525 |
1526 |
1527 | @helper("clang")
1528 | def variable_uninitialized(lines):
1529 | """
1530 | >>> "trying to use the variable" in variable_uninitialized([ \
1531 | "foo.c:6:20: error: variable 'x' is uninitialized when used here [-Werror,-Wuninitialized]", \
1532 | " printf(\\"%d\\n\\", x);", \
1533 | " ^" \
1534 | ])[1][0]
1535 | True
1536 | """
1537 | matches = _match(r"variable '(.*)' is uninitialized when used here", lines[0])
1538 | if not matches:
1539 | return
1540 |
1541 | response = [
1542 | "It looks like you're trying to use the variable `{}` on line {} of " \
1543 | "`{}`.".format(matches.group[0], matches.line, matches.file),
1544 | "However, on that line, the variable `{}` doesn't have a value yet.".format(matches.group[0]),
1545 | "Be sure to assign a value to `{}` before trying to access its value.".format(matches.group[0])
1546 | ]
1547 |
1548 | if len(lines) >= 2 and matches.group[0] in lines[1]:
1549 | return lines[0:2], response
1550 |
1551 | return lines[0:1], response
1552 |
1553 |
1554 | @helper("clang")
1555 | def void_return(lines):
1556 | """
1557 | >>> "returning `0`" in void_return([ \
1558 | "foo.c:6:10: error: void function 'f' should not return a value [-Wreturn-type]", \
1559 | " return 0;", \
1560 | " ^ ~" \
1561 | ])[1][0]
1562 | True
1563 | """
1564 | matches = _match(r"void function '(.+)' should not return a value", lines[0])
1565 | if not matches:
1566 | return
1567 |
1568 | value = _tilde_extract(lines[1:3])
1569 |
1570 | if len(lines) >= 3 and value:
1571 | response = [
1572 | "It looks like your function, `{}`, is returning `{}` on line {} of `{}`, but its return type is" \
1573 | " `void`.".format(matches.group[0], value, matches.line, matches.file),
1574 | "Are you sure you want to return a value?"
1575 | ]
1576 |
1577 | return lines[0:3], response
1578 |
1579 | response = [
1580 | "It looks like your function, `{}`, is returning a value on line {} of `{}`, but its return type is" \
1581 | " `void`.".format(matches.group[0], matches.line, matches.file),
1582 | "Are you sure you want to return a value?"
1583 | ]
1584 |
1585 | return lines[0:1], response
1586 |
1587 |
1588 | @helper("clang")
1589 | def catch_all(lines):
1590 | """
1591 | This helper should **ALWAYS** be the last helper -- it is a catch-all, worst case scenario.
1592 |
1593 | >>> bool(catch_all([ \
1594 | "foo.c:28:28: error: expected expression" \
1595 | ]))
1596 | True
1597 |
1598 | >>> bool(catch_all([ \
1599 | "bar.c:8:16: error: expected identifier", \
1600 | " if (i < 23) && (i > 0) {", \
1601 | " ^", \
1602 | "1 error generated.", \
1603 | ]))
1604 | True
1605 | """
1606 | matches = _match(r".*", lines[0])
1607 | if not matches:
1608 | return
1609 |
1610 | response = [
1611 | "Not quite sure how to help, but focus your attention on line {} of `{}`!".format(matches.line, matches.file)
1612 | ]
1613 |
1614 | return lines[0:1], response
1615 |
1616 |
1617 | # HELPERS FOR THE HELPERS
1618 |
1619 | def _caret_extract(lines, left_aligned=True):
1620 | """
1621 | Extract the name of a variable above the ^ by default, assumes that ^ is
1622 | under the first character of the variable.
1623 | If left_aligned is set to False, ^ is under the next character after the variable
1624 | """
1625 | if len(lines) < 2 or not _has_caret(lines[1]):
1626 | return
1627 |
1628 | index = lines[1].index("^")
1629 |
1630 | if left_aligned:
1631 | matches = re.match(r"^([A-Za-z0-9_]+)", lines[0][index:])
1632 | else:
1633 | matches = re.match(r"^.*?([A-Za-z0-9_]+)$", lines[0][:index])
1634 |
1635 | return matches.group(1) if matches else None
1636 |
1637 | def _has_caret(line):
1638 | """Returns true if line contains a caret diagnostic."""
1639 | return bool(re.search(r"^[ ~]*\^[ ~]*$", line))
1640 |
1641 |
1642 | _ClangMatch = collections.namedtuple("_ClangMatch", ["file", "line", "group"])
1643 |
1644 |
1645 | def _match(expression, line, raw=False):
1646 | """
1647 | Performs a regular-expression match on a particular clang error or warning message.
1648 | The first capture group is the filename associated with the message.
1649 | The second capture group is the line number associated with the message.
1650 | set raw=True to search for a message that doesn't follow clang's typical error output format.
1651 | """
1652 | query = expression if raw else fr"^([^:\s]+):(\d+):\d+: (?:warning|(?:fatal |runtime )?error): {expression}"
1653 | matches = re.search(query, line)
1654 |
1655 | if not matches:
1656 | return None
1657 |
1658 | if raw:
1659 | return _ClangMatch(file=None, line=None, group=matches.groups())
1660 |
1661 | return _ClangMatch(file=matches.group(1),
1662 | line=matches.group(2),
1663 | group=matches.groups()[2:])
1664 |
1665 |
1666 | def _tilde_extract(lines):
1667 | """Extracts all characters above the first sequence of ~."""
1668 | if len(lines) < 2 or not re.search("~", lines[1]):
1669 | return
1670 |
1671 | start = lines[1].index("~")
1672 | length = 1
1673 |
1674 | while len(lines[1]) > start + length and lines[1][start + length] == "~":
1675 | length += 1
1676 |
1677 | if len(lines[0]) >= start + length:
1678 | return lines[0][start:start+length]
1679 |
--------------------------------------------------------------------------------
/helpers/cp.py:
--------------------------------------------------------------------------------
1 | import re
2 |
3 | from . import _common
4 |
5 | from help50 import helper
6 |
7 |
8 | @helper("cp")
9 | def overwrite(lines):
10 | """
11 | >>> "You are copying" in overwrite([ \
12 | "cp: overwrite ‘bar’?" \
13 | ])[1][0]
14 | True
15 |
16 | >>> "You are copying" in overwrite([ \
17 | "cp: overwrite ‘baz/bar’?" \
18 | ])[1][0]
19 | True
20 | """
21 | matches = re.search(r"^{}cp: overwrite ‘(.+)’\?".format(_common.FILE_PATH), lines[0])
22 | if not matches:
23 | return
24 |
25 | # if "/" is present in destination path, then assume copying to a new directory
26 | new_dir = "/" in matches.group(1)
27 | interpretation = "You are copying a file to `{}`, but there is already a file ".format(matches.group(1))
28 | interpretation += "there " if new_dir else "in the current directory "
29 | interpretation += "with the same name."
30 |
31 | response = [interpretation]
32 | response.append("To copy the file, replacing the old version, type `y` and press return.")
33 | response.append("Typing `n` and pressing return will cancel copying.")
34 |
35 | return lines[0:1], response
36 |
--------------------------------------------------------------------------------
/helpers/ls.py:
--------------------------------------------------------------------------------
1 | import re
2 |
3 | from . import _common
4 |
5 | from help50 import helper
6 |
7 |
8 | @helper("ls")
9 | def cannot_access(lines):
10 | """
11 | >>> "Are you sure" in cannot_access([ \
12 | "ls: cannot access asdf: No such file or directory" \
13 | ])[1][0]
14 | True
15 | """
16 | matches = re.search(r"^{}ls: cannot access (.+): No such file or directory".format(_common.FILE_PATH), lines[0])
17 | if not matches:
18 | return
19 |
20 | response = [
21 | "Are you sure `{}` exists?".format(matches.group(1)),
22 | "Did you misspell `{}`?".format(matches.group(1))
23 | ]
24 |
25 | return lines[0:1], response
26 |
--------------------------------------------------------------------------------
/helpers/make.py:
--------------------------------------------------------------------------------
1 | import re
2 |
3 | from help50 import helper
4 |
5 |
6 | @helper("make")
7 | def no_rule_to_make(lines):
8 | """
9 | >>> "actually have" in no_rule_to_make([ \
10 | "make: *** No rule to make target 'foo'. Stop." \
11 | ])[1][0]
12 | True
13 | """
14 | matches = re.search(r"^make: \*\*\* No rule to make target '(.+)'. Stop.", lines[0])
15 | if not matches:
16 | return
17 |
18 | if matches.group(1) in ["ceasar", "ceaser", "cesar", "caesaer"]:
19 | response = [
20 | "Did you mean to type `make caesar`?"
21 | ]
22 |
23 | return lines[0:1], response
24 |
25 | elif matches.group(1) in ["caesar"]:
26 | response = [
27 | "Is your C file correctly spelled as `caesar.c`?"
28 | ]
29 |
30 | return lines[0:1], response
31 |
32 | response = [
33 | "Do you actually have a file called `{}.c` in the current directory?".format(matches.group(1)),
34 | "If using a Makefile, are you sure you have a target called `{}`?".format(matches.group(1))
35 | ]
36 |
37 | return lines[0:1], response
38 |
39 |
40 | @helper("make")
41 | def no_target_specified(lines):
42 | """
43 | >>> "You don't seem to have" in no_target_specified([ \
44 | "make: *** No targets specified and no makefile found. Stop." \
45 | ])[1][0]
46 | True
47 | """
48 | matches = re.search(r"^make: \*\*\* No targets specified and no makefile found. Stop", lines[0])
49 | if not matches:
50 | return
51 |
52 | response = [
53 | "You don't seem to have a `Makefile`?",
54 | "Or did you mean to execute, say, `make foo` instead of just `make`, whereby `foo.c` contains a program " \
55 | "you'd like to compile?"
56 | ]
57 |
58 | return lines[0:1], response
59 |
60 |
61 | @helper("make")
62 | def nothing_to_be_done(lines):
63 | """
64 | >>> "Try compiling" in nothing_to_be_done([ \
65 | "make: Nothing to be done for `foo.c'." \
66 | ])[1][0]
67 | True
68 | """
69 | matches = re.search(r"^make: Nothing to be done for [`']([^']+).c'.", lines[0])
70 | if not matches:
71 | return
72 |
73 | response = [
74 | "Try compiling your program with `make {}` rather than `make {}.c`.".format(matches.group(1), matches.group(1))
75 | ]
76 |
77 | return lines[0:1], response
78 |
79 |
80 | @helper("make")
81 | def success(lines):
82 | """
83 | >>> "compiles successfully!" in success([ \
84 | "clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wshadow foo.c -lcs50 -lm -o foo" \
85 | ])[1][0]
86 | True
87 | """
88 | matches = re.search(r"^clang", lines[0])
89 | if matches and len(lines) == 1 and "error:" not in lines[0]:
90 | response = [
91 | "Looks like your program compiles successfully!"
92 | ]
93 |
94 | return lines[0:1], response
95 |
96 |
97 | @helper("make")
98 | def up_to_date(lines):
99 | """
100 | >>> "you already compiled" in up_to_date([ \
101 | "make: 'foo' is up to date." \
102 | ])[1][0]
103 | True
104 | """
105 | matches = re.search(r"^make: '(.+)' is up to date.", lines[0])
106 | if not matches:
107 | return
108 |
109 | response = [
110 | "Looks like you already compiled `{}` and haven't made any changes to `{}.c` " \
111 | "since.".format(matches.group(1), matches.group(1)),
112 | "Or did you forget to save `{}.c` before running `make`?".format(matches.group(1))
113 | ]
114 |
115 | return lines[0:1], response
116 |
--------------------------------------------------------------------------------
/helpers/mv.py:
--------------------------------------------------------------------------------
1 | import re
2 |
3 | from . import _common
4 |
5 | from help50 import helper
6 |
7 |
8 | @helper("mv")
9 | def overwrite(lines):
10 | """
11 | >>> "You are" in overwrite([ \
12 | "mv: overwrite ‘bar’?" \
13 | ])[1][0]
14 | True
15 |
16 | >>> "You are" in overwrite([ \
17 | "mv: overwrite ‘baz/foo’?" \
18 | ])[1][0]
19 | True
20 | """
21 | matches = re.search(r"^{}mv: overwrite ‘(.+)’\?".format(_common.FILE_PATH), lines[0])
22 | if not matches:
23 | return
24 |
25 | # if a "/" is present in the destination path, assume moving a file; otherwise, renaming a file
26 | moving = "/" in matches.group(1)
27 |
28 | interpretation = "You are "
29 | interpretation += "moving " if moving else "renaming "
30 | interpretation += "a file to `{}`, but there is already a file with the same name ".format(matches.group(1))
31 | interpretation += "there." if moving else "in the current directory."
32 |
33 | response = [interpretation]
34 | response.append("To replace the old file, type `y` and press return.")
35 | response.append("Typing `n` and pressing return will cancel the operation.")
36 |
37 | return lines[0:1], response
38 |
--------------------------------------------------------------------------------
/helpers/rm.py:
--------------------------------------------------------------------------------
1 | import re
2 |
3 | from . import _common
4 |
5 | from help50 import helper
6 |
7 |
8 | @helper("rm")
9 | def remove_regular_file(lines):
10 | """
11 | >>> "will delete the" in remove_regular_file([ \
12 | "rm: remove regular file ‘foo’?" \
13 | ])[1][0]
14 | True
15 |
16 | >>> "empty anyway" in remove_regular_file([ \
17 | "rm: remove regular empty file ‘foo’?" \
18 | ])[1][0]
19 | True
20 | """
21 | matches = re.search(r"^{}rm: remove regular (?:empty )?file ‘(.+)’\?".format(_common.FILE_PATH), lines[0])
22 | if not matches:
23 | return
24 |
25 | empty = re.search(r"^.*rm: remove regular empty file", lines[0])
26 |
27 | response = [
28 | "The command you typed will delete the file `{}`".format(matches.group(1))
29 | ]
30 |
31 | response[0] += " (which is empty anyway)." if empty else "."
32 | response.append("If you wish to delete `{}`, type `y` and press return.".format(matches.group(1)))
33 | response.append("Typing `n` and pressing return will cancel the operation.")
34 |
35 | return lines[0:1], response
36 |
--------------------------------------------------------------------------------
/helpers/runtime.py:
--------------------------------------------------------------------------------
1 | import re
2 |
3 | from help50 import helper
4 |
5 |
6 | @helper("runtime")
7 | def floating_point_exception(lines):
8 | """
9 | >>> "trying to divide a number by 0" in floating_point_exception([ \
10 | "Floating point exception (core dumped)" \
11 | ])[1][0]
12 | True
13 | """
14 | matches = "Floating point exception" in lines[0]
15 | if not matches:
16 | return
17 |
18 | response = [
19 | "Looks like somewhere in your program, you're trying to divide a number by 0.",
20 | "Check to see where in your program you're using the `/` or `%` operators, and be sure you never divide by 0 or " \
21 | "calculate a number modulo 0.",
22 | "If still unsure of where the problem is, stepping through your code with `debug50` may be helpful!"
23 | ]
24 |
25 | return lines[0:1], response
26 |
27 |
28 | @helper("runtime")
29 | def segmentation_fault(lines):
30 | """
31 | >>> "isn't supposed to access" in segmentation_fault([ \
32 | "Segmentation fault (core dumped)" \
33 | ])[1][0]
34 | True
35 | """
36 | matches = "Segmentation fault" in lines[0]
37 | if not matches:
38 | return
39 |
40 | response = [
41 | "Looks like your program is trying to access areas of memory that it isn't supposed to access.",
42 | "Did you try to change a character in a hardcoded string?",
43 | "Are you accessing an element of an array beyond the size of the array?",
44 | "Are you dereferencing a pointer that you haven't initialized?",
45 | "Are you dereferencing a pointer whose value is `NULL`?",
46 | "Are you dereferencing a pointer after you've freed it?"
47 | ]
48 |
49 | return lines[0:1], response
50 |
51 |
52 | # TODO fix doctest
53 | # @helper("runtime")
54 | # def segmentation_fault_bool(lines):
55 | # """
56 | # >>> "store a value other than" in segmentation_fault_bool([ \
57 | # "Segmentation fault (core dumped)" \
58 | # ])[1][0]
59 | # True
60 | # """
61 | # matches = re.match("^(.+):(\d+):\d+: runtime error: load of value \d+, which is not a valid value for " \
62 | # "type '_Bool'$", lines[0])
63 | # if not matches:
64 | # return
65 | #
66 | # response = [
67 | # "Looks like, on line {} of `{}`, you're trying to store a value other than `true` or `false` in " \
68 | # "a `bool`?".format(matches.group(2), matches.group(1))
69 | # ]
70 | #
71 | # return lines[0:1], response
72 |
--------------------------------------------------------------------------------
/helpers/valgrind.py:
--------------------------------------------------------------------------------
1 | from collections import namedtuple
2 | import locale
3 | import re
4 |
5 | from help50 import helper
6 |
7 | locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
8 |
9 |
10 | @helper("valgrind")
11 | def all_heap_blocks_freed(lines):
12 | """
13 | >>> "memory-related errors!" in all_heap_blocks_freed([ \
14 | "==4412== Memcheck, a memory error detector", \
15 | "==4412== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.", \
16 | "==4412== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info", \
17 | "==4412== Command: ./a.out", \
18 | "==4412==", \
19 | "", \
20 | " ptr = [Linux]", \
21 | "==4412== All heap blocks were freed -- no leaks are possible", \
22 | "==4412== ERROR SUMMARY: 0 errors from 0 contexts" \
23 | ])[1][0]
24 | True
25 | """
26 | # check for recognized output
27 | if not re.search(r"^==\d+== Memcheck, a memory error detector$", lines[0]):
28 | return
29 |
30 | # iterate over lines ourselves
31 | for i, line in enumerate(lines):
32 |
33 | # All heap blocks were freed -- no leaks are possible
34 | # ERROR SUMMARY: 0 errors from 0 contexts
35 | matches = re.search(r"^==\d+== All heap blocks were freed -- no leaks are possible$", line)
36 | if not matches:
37 | continue
38 |
39 | for j in range(i+1, len(lines)):
40 | matches = re.search(r"^==\d+== ERROR SUMMARY: 0 errors from 0 contexts", lines[j])
41 | if not matches:
42 | continue
43 |
44 | response = [
45 | "Looks like your program doesn't have any memory-related errors!",
46 | "Be sure, though, to test it with other inputs!"
47 | ]
48 |
49 | return [line, "...", lines[j]], response
50 |
51 |
52 | @helper("valgrind")
53 | def bytes_definitely_lost(lines):
54 | """
55 | >>> "your program leaked" in bytes_definitely_lost([ \
56 | "==4412== Memcheck, a memory error detector", \
57 | "==4412== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.", \
58 | "==4412== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info", \
59 | "==4412== Command: ./a.out", \
60 | "==4412==", \
61 | "", \
62 | " ptr = [Linux]", \
63 | "==4412== 40 bytes in 1 blocks are definitely lost in loss record 1 of 1", \
64 | "", \
65 | "==4412== 8,013,096 (1,456 direct, 8,011,640 indirect) bytes in 26 blocks are definitely lost in loss " \
66 | "record 2 of 2" \
67 | ])[1][0]
68 | True
69 | """
70 | # check for recognized output
71 | if not re.search(r"^==\d+== Memcheck, a memory error detector$", lines[0]):
72 | return
73 |
74 | # iterate over lines ourselves
75 | for i, line in enumerate(lines):
76 |
77 | # 40 bytes in 1 blocks are definitely lost in loss record 1 of 1
78 | #
79 | # 8,013,096 (1,456 direct, 8,011,640 indirect) bytes in 26 blocks are definitely lost in loss record 2 of 2
80 | matches = re.search(r"^==\d+== ([\d,]+)(?: \(([\d,]+) direct, ([\d,]+) indirect\))? bytes in ([\d,]+) blocks " \
81 | "are definitely lost in loss record [\d,]+ of [\d,]+$", line)
82 | if not matches:
83 | continue
84 |
85 | bytes = "bytes" if locale.atoi(matches.group(1)) > 1 else "byte"
86 |
87 | response = [
88 | "Looks like your program leaked {} {} of memory.".format(matches.group(1), bytes),
89 | "Did you forget to `free` memory that you allocated via `malloc`?"
90 | ]
91 |
92 | frames, frame = _frame_extract(lines[i+1:])
93 | if frame:
94 | if frame.line:
95 | response.append("Take a closer look at line {} of `{}`.".format(frame.line, frame.file))
96 | else:
97 | response.append("Take a closer look at `{}`.".format(frame.function))
98 | response.append("And be sure to compile your program with `-ggdb3` to see line numbers " \
99 | "in `valgrind`'s output.")
100 |
101 | return lines[i:i+1+frames], response
102 |
103 |
104 | @helper("valgrind")
105 | def conditional_jump_depends(lines):
106 | """
107 | >>> "trying to use" in conditional_jump_depends([ \
108 | "==4412== Memcheck, a memory error detector", \
109 | "==4412== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.", \
110 | "==4412== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info", \
111 | "==4412== Command: ./a.out", \
112 | "==4412==", \
113 | "", \
114 | " ptr = [Linux]", \
115 | "==4412== Conditional jump or move depends on uninitialised value(s)" \
116 | ])[1][0]
117 | True
118 | """
119 | # check for recognized output
120 | if not re.search(r"^==\d+== Memcheck, a memory error detector$", lines[0]):
121 | return
122 |
123 | # iterate over lines ourselves
124 | for i, line in enumerate(lines):
125 |
126 | # Conditional jump or move depends on uninitialised value(s)
127 | matches = re.search(r"^==\d+== Conditional jump or move depends on uninitialised value\(s\)$", line)
128 | if not matches:
129 | continue
130 |
131 | response = [
132 | "Looks like you're trying to use a variable that might not have a value?",
133 | ]
134 |
135 | frames, frame = _frame_extract(lines[i+1:])
136 | if frame:
137 | if frame.line:
138 | response.append("Take a closer look at line {} of `{}`.".format(frame.line, frame.file))
139 | else:
140 | response.append("Take a closer look at `{}`.".format(frame.function))
141 | response.append("And be sure to compile your program with `-ggdb3` to see line numbers " \
142 | "in `valgrind`'s output.")
143 |
144 | return lines[i:i+1], response
145 |
146 |
147 | @helper("valgrind")
148 | def definitely_lost(lines):
149 | """
150 | >>> "your program leaked" in definitely_lost([ \
151 | "==4412== Memcheck, a memory error detector", \
152 | "==4412== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.", \
153 | "==4412== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info", \
154 | "==4412== Command: ./a.out", \
155 | "==4412==", \
156 | "", \
157 | " ptr = [Linux]", \
158 | "==4412== definitely lost: 4 bytes in 1 blocks" \
159 | ])[1][0]
160 | True
161 | """
162 | # check for recognized output
163 | if not re.search(r"^==\d+== Memcheck, a memory error detector$", lines[0]):
164 | return
165 |
166 | # iterate over lines ourselves
167 | for i, line in enumerate(lines):
168 |
169 | # definitely lost: 4 bytes in 1 blocks
170 | matches = re.search(r"^==\d+== definitely lost: ([\d,]+) bytes in ([\d,]+) blocks$", line)
171 | if matches and locale.atoi(matches.group(1)) != 0:
172 | bytes = "bytes" if locale.atoi(matches.group(1)) > 1 else "byte"
173 |
174 | response = [
175 | "Looks like your program leaked {} {} of memory.".format(matches.group(1), bytes),
176 | "Did you forget to `free` memory that you allocated via `malloc`?"
177 | ]
178 |
179 | matches = re.search(r"^==\d+== Command: ([^\n]+)$.+^==\d+== Rerun with --leak-check=full to see details " \
180 | "of leaked memory$", "\n".join(lines), re.DOTALL | re.MULTILINE)
181 | if matches:
182 | response.append("Run `valgrind --leak-check=full {}` for more details.".format(matches.group(1)))
183 |
184 | return lines[i:i+1], response
185 |
186 |
187 | @helper("valgrind")
188 | def invalid_read(lines):
189 | """
190 | >>> "trying to access" in invalid_read([ \
191 | "==4412== Memcheck, a memory error detector", \
192 | "==4412== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.", \
193 | "==4412== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info", \
194 | "==4412== Command: ./a.out", \
195 | "==4412==", \
196 | "", \
197 | " ptr = [Linux]", \
198 | "==4412== Invalid read of size 8" \
199 | ])[1][0]
200 | True
201 | """
202 | # check for recognized output
203 | if not re.search(r"^==\d+== Memcheck, a memory error detector$", lines[0]):
204 | return
205 |
206 | # iterate over lines ourselves
207 | for i, line in enumerate(lines):
208 |
209 | # Invalid read of size 8
210 | matches = re.search(r"^==\d+== Invalid read of size ([\d,]+)$", line)
211 | if not matches:
212 | continue
213 |
214 | bytes = "bytes" if locale.atoi(matches.group(1)) > 1 else "byte"
215 |
216 | response = [
217 | "Looks like you're trying to access {} {} of memory that isn't yours?".format(matches.group(1), bytes),
218 | "Did you try to index into an array beyond its bounds?"
219 | ]
220 |
221 | frames, frame = _frame_extract(lines[i+1:])
222 | if frame:
223 | if frame.line:
224 | response.append("Take a closer look at line {} of `{}`.".format(frame.line, frame.file))
225 | else:
226 | response.append("Take a closer look at `{}`.".format(frame.function))
227 | response.append("And be sure to compile your program with `-ggdb3` to see line numbers " \
228 | "in `valgrind`'s output.")
229 |
230 | return lines[i:i+1+frames], response
231 |
232 |
233 | @helper("valgrind")
234 | def invalid_write(lines):
235 | """
236 | >>> "trying to modify" in invalid_write([ \
237 | "==4412== Memcheck, a memory error detector", \
238 | "==4412== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.", \
239 | "==4412== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info", \
240 | "==4412== Command: ./a.out", \
241 | "==4412==", \
242 | "", \
243 | " ptr = [Linux]", \
244 | "==4412== Invalid write of size 4" \
245 | ])[1][0]
246 | True
247 | """
248 | # check for recognized output
249 | if not re.search(r"^==\d+== Memcheck, a memory error detector$", lines[0]):
250 | return
251 |
252 | # iterate over lines ourselves
253 | for i, line in enumerate(lines):
254 |
255 | # Invalid write of size 4
256 | matches = re.search(r"^==\d+== Invalid write of size ([\d,]+)$", line)
257 | if not matches:
258 | continue
259 |
260 | bytes = "bytes" if locale.atoi(matches.group(1)) > 1 else "byte"
261 |
262 | response = [
263 | "Looks like you're trying to modify {} {} of memory that isn't yours?".format(matches.group(1), bytes),
264 | "Did you try to store something beyond the bounds of an array?"
265 | ]
266 |
267 | frames, frame = _frame_extract(lines[i+1:])
268 | if frame:
269 | if frame.line:
270 | response.append("Take a closer look at line {} of `{}`.".format(frame.line, frame.file))
271 | else:
272 | response.append("Take a closer look at `{}`.".format(frame.function))
273 | response.append("And be sure to compile your program with `-ggdb3` to see line numbers " \
274 | "in `valgrind`'s output.")
275 |
276 | return lines[i:i+1+frames], response
277 |
278 |
279 | @helper("valgrind")
280 | def use_of_uninitialized_val(lines):
281 | """
282 | >>> "might not have a" in use_of_uninitialized_val([ \
283 | "==4412== Memcheck, a memory error detector", \
284 | "==4412== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.", \
285 | "==4412== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info", \
286 | "==4412== Command: ./a.out", \
287 | "==4412==", \
288 | "", \
289 | " ptr = [Linux]", \
290 | "==4412== Use of uninitialised value of size 8" \
291 | ])[1][0]
292 | True
293 | """
294 | # check for recognized output
295 | if not re.search(r"^==\d+== Memcheck, a memory error detector$", lines[0]):
296 | return
297 |
298 | # iterate over lines ourselves
299 | for i, line in enumerate(lines):
300 |
301 | # Use of uninitialized value of size 8
302 | matches = re.search(r"^==\d+== Use of uninitialised value of size ([\d,]+)$", line)
303 | if not matches:
304 | continue
305 |
306 | response = [
307 | "Looks like you're trying to use a {}-byte variable that might not have a value?".format(matches.group(1))
308 | ]
309 |
310 | frames, frame = _frame_extract(lines[i+1:])
311 | if frame:
312 | if frame.line:
313 | response.append("Take a closer look at line {} of `{}`.".format(frame.line, frame.file))
314 | else:
315 | response.append("Take a closer look at `{}`.".format(frame.function))
316 | response.append("And be sure to compile your program with `-ggdb3` to see line numbers " \
317 | "in `valgrind`'s output.")
318 |
319 | return lines[i:i+1+frames], response
320 |
321 |
322 | # HELPER FOR THE HELPERS
323 |
324 | # Parses lines for stack frames, returning (frames, frame), where frames is the number of frames parsed,
325 | # and frame is a tuple with address, function, file, and line fields representing the likely source of an error.
326 | def _frame_extract(lines):
327 |
328 | # identify possible frames
329 | frames = []
330 | Frame = namedtuple("Frame", ["address", "function", "file", "line"])
331 | for line in lines:
332 | matches = re.search(r"^==\d+== (?:at|by) (0x[0-9A-Fa-f]+): (.+) \((.+?)(?::(\d+))?\)", line)
333 | if matches:
334 | frames.append(Frame(address=matches.group(1), function=matches.group(2), file=matches.group(3),
335 | line=matches.group(4)))
336 | else:
337 | break
338 |
339 | # infer actual frame
340 | frames.reverse()
341 | for i in range(len(frames)-1):
342 |
343 | # at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
344 | # by 0x400546: foo (foo.c:6)
345 | # by 0x400568: main (foo.c:12)
346 | if (frames[i].line and not frames[i+1].line):
347 | return len(frames), frames[i]
348 |
349 | # at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
350 | # by 0x400546: foo (in /srv/www/foo)
351 | # by 0x400568: main (in /srv/www/foo)
352 | if (not frames[i].line and frames[i].file != frames[i+1].file):
353 | return len(frames), frames[i]
354 |
355 | # at 0x508299B: _itoa_word (_itoa.c:179)
356 | # by 0x5086636: vfprintf (vfprintf.c:1660)
357 | # by 0x5087E70: buffered_vfprintf (vfprintf.c:2356)
358 | # by 0x5082DFD: vfprintf (vfprintf.c:1313)
359 | # by 0x508D3D8: printf (printf.c:33)
360 | # by 0x40054C: main (foo.c:6)
361 | if (frames[i].line and frames[i+1].line and len(frames[i].address) < len(frames[i+1].address)):
362 | return len(frames), frames[i]
363 |
364 | # at 0x40054F: foo (foo.c:7)
365 | # by 0x400568: main (foo.c:12)
366 | return (len(frames), frames[len(frames)-1]) if frames else (0, None)
367 |
--------------------------------------------------------------------------------
/run_tests.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python3
2 | import doctest
3 | import inspect
4 | import unittest
5 |
6 | import helpers
7 |
8 | # helpers use doctest for their tests, but we convert doctest to unittest since unittest has nicer output
9 | testSuite = unittest.TestSuite()
10 |
11 | for member in dir(helpers):
12 | mod = getattr(helpers, member)
13 | if inspect.ismodule(mod):
14 | testSuite.addTests(doctest.DocTestSuite(mod))
15 |
16 |
17 | if __name__ == "__main__":
18 | runner = unittest.TextTestRunner()
19 | runner.run(testSuite)
20 |
--------------------------------------------------------------------------------
/test_files/a/floating_point_exception.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int x = 1 % 0;
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/a/segmentation_fault.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int temp[1];
3 | temp[10000]++;
4 | }
5 |
--------------------------------------------------------------------------------
/test_files/clang/array_bounds.c:
--------------------------------------------------------------------------------
1 | #include
2 | int main(void) {
3 | int a[1];
4 | printf("%d\n", a[1]);
5 | }
6 |
--------------------------------------------------------------------------------
/test_files/clang/array_subscript.c:
--------------------------------------------------------------------------------
1 | #include
2 | int main(void) {
3 | int x[30];
4 | printf("%i\n", x["28"]);
5 | }
6 |
--------------------------------------------------------------------------------
/test_files/clang/assignment_as_condition.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int x = 1;
3 | if (x = 3) {
4 | x++;
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/test_files/clang/bad_define.c:
--------------------------------------------------------------------------------
1 | define _XOPEN_SOURCE 500
2 | int main(void) {
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/clang/bad_include.c:
--------------------------------------------------------------------------------
1 | include
2 | int main(void) {
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/clang/catch_all_expected_expression.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | if (2 == 2)
3 | int x = 1;
4 | }
5 |
--------------------------------------------------------------------------------
/test_files/clang/catch_all_expected_identifier.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int i = 1;
3 | if (i < 23) && (i > 0) {
4 |
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/test_files/clang/comp_str_literal_unspecified.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int x = 0;
3 | if ("general" < "kenobi") {
4 | x++;
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/test_files/clang/conflicting_types.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int round(int n);
4 |
5 | int main(void) {
6 | }
7 |
--------------------------------------------------------------------------------
/test_files/clang/continue_not_in_loop.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | continue;
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/clang/control_reaches_non_void.c:
--------------------------------------------------------------------------------
1 | int foo();
2 |
3 | int main(void) {
4 | foo();
5 | }
6 |
7 | int foo() {}
8 |
--------------------------------------------------------------------------------
/test_files/clang/declaration_shadows_local_var0.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(void) {
4 | int x = 2;
5 | do {
6 | int x = 28;
7 | x++;
8 | } while (x < 30);
9 | printf("%d\n", x);
10 | }
11 |
--------------------------------------------------------------------------------
/test_files/clang/declaration_shadows_local_var1.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int i = 1;
3 | for(int i = 0, i < 17, i++) {
4 |
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/test_files/clang/div_by_zero.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int x;
3 | x = 28 / 0;
4 | }
5 |
--------------------------------------------------------------------------------
/test_files/clang/expected_closing_brace.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 |
--------------------------------------------------------------------------------
/test_files/clang/expected_closing_parens.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(void) {
4 | if (1 == 1
5 | printf("hi\n");
6 | }
7 |
--------------------------------------------------------------------------------
/test_files/clang/expected_for_semi_colon.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(void) {
4 | for (int i = 0, i < 28, i++) {
5 | printf("%d\n", i);
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/test_files/clang/expected_identifier_or_parens.c:
--------------------------------------------------------------------------------
1 | int main(void); {
2 |
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/clang/expected_if_closing_parens.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int x = 1;
3 | if x == 28
4 | }
5 |
--------------------------------------------------------------------------------
/test_files/clang/expected_param_declarator.c:
--------------------------------------------------------------------------------
1 | int square(28);
2 |
3 | int main(void) {
4 |
5 | }
6 |
--------------------------------------------------------------------------------
/test_files/clang/expected_semi_colon.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(void) {
4 | printf("hello, world!")
5 | }
6 |
--------------------------------------------------------------------------------
/test_files/clang/expected_while_in_do_while.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | do {
3 |
4 | }
5 | }
6 |
--------------------------------------------------------------------------------
/test_files/clang/expression_not_int_const.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int x = 1;
3 | switch (x) {
4 | case ("hello"): break;
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/test_files/clang/expression_result_unused.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int n = 1;
3 | n*12;
4 | }
5 |
--------------------------------------------------------------------------------
/test_files/clang/extra_tokens_at_end_of_include.c:
--------------------------------------------------------------------------------
1 | #include c
2 |
3 | int main(void) {
4 |
5 | }
6 |
--------------------------------------------------------------------------------
/test_files/clang/extraneous_closing_brace.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | }
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/clang/extraneous_closing_parens.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | int main(void) {
5 | int x = round(2.4));
6 | printf("%d\n", x);
7 | }
8 |
--------------------------------------------------------------------------------
/test_files/clang/file_not_found_include.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(void) {
4 |
5 | }
6 |
--------------------------------------------------------------------------------
/test_files/clang/fmt_specifies_type.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(void) {
4 | printf("%d\n", "hello!");
5 | }
6 |
--------------------------------------------------------------------------------
/test_files/clang/fmt_string_not_string_literal.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(void) {
4 | char *c = "hi";
5 | int x;
6 | printf(c);
7 | }
8 |
--------------------------------------------------------------------------------
/test_files/clang/has_empty_body.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | if (2 == 2);
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/clang/ignoring_return_value.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(void) {
4 | round(1.7);
5 | }
6 |
--------------------------------------------------------------------------------
/test_files/clang/implicit_declaration_of_fun.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int x = eprintf();
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/clang/implicitly_declaring_lib_fun.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | printf("hello, world!");
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/clang/incompatible_conversion.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int x = "28";
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/clang/index_out_of_bounds.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int n = 2;
3 | int temp[n];
4 | for (int i = 0; i <= 2; i++) {
5 | temp[i] = 1;
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/test_files/clang/invalid_append_string.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(void) {
4 | char c = 'a';
5 | printf("%s" + c, "");
6 | }
7 |
--------------------------------------------------------------------------------
/test_files/clang/invalid_equals.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | for(int i == 0; i < 2; i++);
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/clang/invalid_preprocessing_directive.c:
--------------------------------------------------------------------------------
1 | #incalude
2 |
3 | int main(void);
4 |
--------------------------------------------------------------------------------
/test_files/clang/main_must_return_int.c:
--------------------------------------------------------------------------------
1 | void main(void);
2 |
--------------------------------------------------------------------------------
/test_files/clang/missing_parens.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(void) {
4 | float f;
5 | f = get_float;
6 | }
7 |
--------------------------------------------------------------------------------
/test_files/clang/more_conversions_than_data_args.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(void) {
4 | printf("%d %d\n", 28);
5 | }
6 |
--------------------------------------------------------------------------------
/test_files/clang/multiple_unsequenced_modifications.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int space = 1;
3 | space = space--;
4 | }
5 |
--------------------------------------------------------------------------------
/test_files/clang/one_param_on_main_dec.c:
--------------------------------------------------------------------------------
1 | int main(int x);
2 |
--------------------------------------------------------------------------------
/test_files/clang/relational_comp_res_unused.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | while(2 < 3, 3 < 4);
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/clang/second_param_main_char.c:
--------------------------------------------------------------------------------
1 | int main(int argc, int argv[]);
2 |
--------------------------------------------------------------------------------
/test_files/clang/self_initialization.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int x = 12*x;
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/clang/subscripted_val_not_array.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int x = 0;
3 | x[1]++;
4 | }
5 |
--------------------------------------------------------------------------------
/test_files/clang/too_many_args_to_fun_call.c:
--------------------------------------------------------------------------------
1 | void f(void);
2 |
3 | int main(void) {
4 | f(1);
5 | }
6 |
7 | void f(void) {
8 | int x;
9 | x++;
10 | }
11 |
--------------------------------------------------------------------------------
/test_files/clang/type_specifier_missing.c:
--------------------------------------------------------------------------------
1 | square (int x) {
2 | x++;
3 | }
4 |
5 | int main(void);
6 |
--------------------------------------------------------------------------------
/test_files/clang/undefined_reference0.c:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cs50/helpers/5216e4b8a1dbfc47ee60c230d8da378826aa646f/test_files/clang/undefined_reference0.c
--------------------------------------------------------------------------------
/test_files/clang/undefined_reference1.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int x = foo();
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/clang/unknown_escape_sequence.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(void) {
4 | printf("%s \ n", "hi");
5 | }
6 |
--------------------------------------------------------------------------------
/test_files/clang/unknown_type_bool.c:
--------------------------------------------------------------------------------
1 | bool baz;
2 | int main(void) {
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/clang/unknown_type_size_t.c:
--------------------------------------------------------------------------------
1 | size_t baz;
2 | int main(void) {
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/clang/unknown_type_string.c:
--------------------------------------------------------------------------------
1 | string baz;
2 | int main(void) {
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/clang/unknown_type_unknown.c:
--------------------------------------------------------------------------------
1 | bar baz;
2 | int main(void) {
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/clang/unused_arg_in_fmt_string.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(void) {
4 | printf("%d %d", 27, 28, 29);
5 | }
6 |
--------------------------------------------------------------------------------
/test_files/clang/unused_var.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int x = 28;
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/clang/use_of_undeclared_identifier.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | x++;
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/clang/variable_uninitialized.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(void) {
4 | int x;
5 | printf("%d\n", x);
6 | }
7 |
--------------------------------------------------------------------------------
/test_files/clang/void_return.c:
--------------------------------------------------------------------------------
1 | void f(void);
2 |
3 | int main(void) {
4 | f();
5 | }
6 |
7 | void f(void) {
8 | return 0;
9 | }
10 |
--------------------------------------------------------------------------------
/test_files/runtime/floating_point_exception:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cs50/helpers/5216e4b8a1dbfc47ee60c230d8da378826aa646f/test_files/runtime/floating_point_exception
--------------------------------------------------------------------------------
/test_files/runtime/floating_point_exception.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int x = 1;
3 | int y = 0;
4 | x = x % y;
5 | }
6 |
--------------------------------------------------------------------------------
/test_files/runtime/segmentation_fault:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cs50/helpers/5216e4b8a1dbfc47ee60c230d8da378826aa646f/test_files/runtime/segmentation_fault
--------------------------------------------------------------------------------
/test_files/runtime/segmentation_fault.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int temp[1];
3 | temp[10000]++;
4 | }
5 |
--------------------------------------------------------------------------------
/test_files/valgrind/all_heap_blocks_freed:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cs50/helpers/5216e4b8a1dbfc47ee60c230d8da378826aa646f/test_files/valgrind/all_heap_blocks_freed
--------------------------------------------------------------------------------
/test_files/valgrind/all_heap_blocks_freed.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int x = 17;
3 | }
4 |
--------------------------------------------------------------------------------
/test_files/valgrind/bytes_definitely_lost:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cs50/helpers/5216e4b8a1dbfc47ee60c230d8da378826aa646f/test_files/valgrind/bytes_definitely_lost
--------------------------------------------------------------------------------
/test_files/valgrind/bytes_definitely_lost.c:
--------------------------------------------------------------------------------
1 | // Run valgrind with --leak-check=full
2 |
3 | #include
4 |
5 | int main(void) {
6 | int *x = malloc(sizeof(int));
7 | }
8 |
--------------------------------------------------------------------------------
/test_files/valgrind/conditional_jump_depends:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cs50/helpers/5216e4b8a1dbfc47ee60c230d8da378826aa646f/test_files/valgrind/conditional_jump_depends
--------------------------------------------------------------------------------
/test_files/valgrind/conditional_jump_depends.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int x;
3 | if (x == 17) {
4 | (void) x;
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/test_files/valgrind/definitely_lost:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cs50/helpers/5216e4b8a1dbfc47ee60c230d8da378826aa646f/test_files/valgrind/definitely_lost
--------------------------------------------------------------------------------
/test_files/valgrind/definitely_lost.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(void) {
4 | int *x = malloc(sizeof(int));
5 | }
6 |
--------------------------------------------------------------------------------
/test_files/valgrind/invalid_read:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cs50/helpers/5216e4b8a1dbfc47ee60c230d8da378826aa646f/test_files/valgrind/invalid_read
--------------------------------------------------------------------------------
/test_files/valgrind/invalid_read.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int x[1];
3 | x[1700]++;
4 | }
5 |
--------------------------------------------------------------------------------
/test_files/valgrind/invalid_write:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cs50/helpers/5216e4b8a1dbfc47ee60c230d8da378826aa646f/test_files/valgrind/invalid_write
--------------------------------------------------------------------------------
/test_files/valgrind/invalid_write.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int x[1];
3 | x[1700] = 17;
4 | }
5 |
--------------------------------------------------------------------------------
/test_files/valgrind/use_of_uninitialised_val:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cs50/helpers/5216e4b8a1dbfc47ee60c230d8da378826aa646f/test_files/valgrind/use_of_uninitialised_val
--------------------------------------------------------------------------------
/test_files/valgrind/use_of_uninitialised_val.c:
--------------------------------------------------------------------------------
1 | int main(void) {
2 | int x;
3 | int foo = x + 2;
4 | int y[10];
5 | y[foo] = 17;
6 | }
7 |
--------------------------------------------------------------------------------