	* gengtype.c (get_bufused): New static inline function.
	(walk_type): Using get_bufused, see if something was written out
	into a generated loop, and add an explicit break statement if
	nothing was written out.

Index: gengtype.c
===================================================================
--- gengtype.c	(revision 189778)
+++ gengtype.c	(working copy)
@@ -1535,6 +1535,14 @@ oprintf (outf_p o, const char *format, ...)
   free (s);
 }
 
+/* Return the current number of space used in file buffer O.
+   This can be used to see if e.g. walk_type wrote something or not.  */
+static inline size_t
+get_bufused (outf_p o)
+{
+  return o->bufused;
+}
+
 /* Open the global header file and the language-specific header files.  */
 
 static void
@@ -2598,6 +2606,7 @@ walk_type (type_p t, struct walk_type_data *d)
 	    const char *oldval = d->val;
 	    const char *oldprevval3 = d->prev_val[3];
 	    char *newval;
+	    size_t curr_bufused;
 
 	    oprintf (d->of, "%*sif (%s != NULL) {\n", d->indent, "", d->val);
 	    d->indent += 2;
@@ -2613,8 +2622,12 @@ walk_type (type_p t, struct walk_type_data *d)
 	    d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
 	    d->used_length = 1;
 	    d->prev_val[3] = oldval;
+	    curr_bufused = get_bufused (d->of);
 	    walk_type (t->u.p, d);
 	    free (newval);
+	    if (curr_bufused == get_bufused (d->of))
+	      /* Nothing was written out.  Avoid empty loops by breaking out.  */
+	      oprintf (d->of, "%*sbreak;\n", d->indent, "");
 	    d->val = oldval;
 	    d->prev_val[3] = oldprevval3;
 	    d->used_length = 0;
@@ -2632,6 +2645,7 @@ walk_type (type_p t, struct walk_type_data *d)
 	int loopcounter;
 	const char *oldval = d->val;
 	char *newval;
+	size_t curr_bufused;
 
 	/* If it's an array of scalars, we optimize by not generating
 	   any code.  */
@@ -2670,8 +2684,12 @@ walk_type (type_p t, struct walk_type_data *d)
 	d->indent += 2;
 	d->val = newval = xasprintf ("%s[i%d]", oldval, loopcounter);
 	d->used_length = 1;
+	curr_bufused = get_bufused (d->of);
 	walk_type (t->u.a.p, d);
 	free (newval);
+	if (curr_bufused == get_bufused (d->of))
+	  /* Nothing was written out.  Avoid empty loops by breaking out.  */
+	  oprintf (d->of, "%*sbreak;\n", d->indent, "");
 	d->used_length = 0;
 	d->val = oldval;
 	d->indent -= 2;
