Skip to content

grid.cpp and environment.cpp use std::runtime_error and rand() without including <stdexcept>/<cstdlib> #45

Description

@dmccoystephenson

Two translation units use standard-library facilities whose headers are never included, relying instead on whatever <iostream> / <string> / <vector> happen to pull in transitively on the current toolchain.

  • src/grid.cpp throws std::runtime_error (lines 30 and 116) and calls rand() (line 120). Its only includes are header/grid.h and <iostream> (lines 1 and 3); grid.h adds <iostream>, <vector>, and location.h → entity.h → <string>. <stdexcept> and <cstdlib> are never included on that path.
  • src/environment.cpp throws std::runtime_error (lines 67 and 86) and catches it (line 141). It includes <stdlib.h> (so rand() on line 113 is covered) and <iostream>, but not <stdexcept>.

The build succeeds today because libstdc++'s <iostream> and <string> transitively include <stdexcept>, and <iostream> reaches <cstdlib>. That is an implementation detail, not a guarantee: a different standard library (libc++, MSVC's STL) or a future libstdc++ header cleanup can drop the transitive include, at which point the library stops compiling with 'runtime_error' is not a member of 'std' even though nothing in the project changed.

Suggested fix: add #include <stdexcept> and #include <cstdlib> to src/grid.cpp, and #include <stdexcept> to src/environment.cpp (optionally replacing <stdlib.h> with <cstdlib> there for consistency, though that is cosmetic). No behaviour changes; the acceptance criterion is that make still builds cleanly and bash run_tests.sh still passes.

This issue body was drafted during a Gardener session (https://github.com/Stephenson-Software/gardener).


drafted by Claude on behalf of Daniel Stephenson

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions