GCC Newspaper
JUNE 15, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
gcc/ada

The Ada front end guards against empty switches in `Getopt`

The Ada front end's command-line parsing utility `GNAT.Command_Line.Getopt` now safely handles empty switch strings, preventing potential errors.

This commit enhances the robustness of GNAT.Command_Line.Getopt within the libgnat runtime by adding guards against empty switch strings. Previously, checks for the first character of Switches did not always account for an empty input string, potentially leading to errors. The updated Getopt function now explicitly checks if Switches are empty before attempting to access their first element, preventing crashes in scenarios with malformed command-line arguments.

In Details

The gcc/ada subsystem handles the Ada front end, and libgnat is its core runtime library. This change addresses a robustness issue in GNAT.Command_Line.Getopt, a utility for parsing command-line options. Specifically, the code was attempting to access the first character of the Switches parameter to determine its type (e.g., if it started with an asterisk * for special handling) without first verifying that Switches was not an empty string. This could lead to a runtime error if an empty string was passed. The fix adds an explicit check for an empty Switches parameter before dere…

For Context

The Ada front end for the GCC compiler includes a runtime library called libgnat, which provides essential functions for Ada programs. One such function is GNAT.Command_Line.Getopt, which is used to parse command-line arguments when you run an Ada program, similar to how operating systems process arguments for commands like ls or grep. This commit improves the Getopt function's reliability. Previously, if the list of "switches" (like -a or --verbose) provided to Getopt was a completely empty string, the function might try to look at its first character, causing an error. This update adds a check to ensure the switch string isn't empty before trying to examine its contents, preventing crashes and making Ada programs that use Getopt more robust when faced with unusual or malformed command-line inputs.

Filed Under: adaruntimebugfixsafety