[PATCH] Validate that the languages array matches the language enum

Simon Marchi simon.marchi@ericsson.com
Thu Oct 26 17:31:00 GMT 2017


There is the requirement for the languages array elements to be in the
same order as the language enum.  This patch adds a check so that GDB
will abort if it is not the case.

The added gdb_assert runs before the ui and/or readline are properly set
up.  This means that if the assert fails, GDB will segfault instead of
presenting the usual message:

  /home/emaisin/src/binutils-gdb/gdb/language.c:1147: internal-error: void _initialize_language(): Assertion `languages[i]->la_language == i' failed.
  A problem internal to GDB has been detected,
  further debugging may prove unreliable.
  Quit this debugging session? (y or n) [1]    4553 segmentation fault  ./gdb

I don't think this is a big problem, as it should never trigger
normally.  If it does, it means the patch you are working on is breaking
that assumption, and the segfault will give a big clue about it.

gdb/ChangeLog:

	* language.c (_initialize_language): Check that the elements of
	the languages array match the language enum.
---
 gdb/ChangeLog  | 5 +++++
 gdb/language.c | 6 ++++++
 2 files changed, 11 insertions(+)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index f180b1d..9d7f935 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2017-10-26  Simon Marchi  <simon.marchi@ericsson.com>
+
+	* language.c (_initialize_language): Check that the elements of
+	the languages array match the language enum.
+
 2017-10-26  Yao Qi  <yao.qi@linaro.org>
 
 	* breakpoint.h (regular_breakpoint_inserted_here_p): Remove.
diff --git a/gdb/language.c b/gdb/language.c
index ecdf431..3a3f287 100644
--- a/gdb/language.c
+++ b/gdb/language.c
@@ -1140,4 +1140,10 @@ For Fortran the default is off; for other languages the default is on."),
 
   /* Have the above take effect.  */
   set_language (language_auto);
+
+  /* Verify the assumption that the order of the languages array matches the
+     language enum.  */
+  for (int i = 0; i < ARRAY_SIZE (languages); i++)
+    gdb_assert (languages[i]->la_language == i);
+
 }
-- 
2.7.4



More information about the Gdb-patches mailing list