public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Bugs in dg/struct profilr test - but what was intended?
@ 2008-06-10 23:17 Andy H
  0 siblings, 0 replies; only message in thread
From: Andy H @ 2008-06-10 23:17 UTC (permalink / raw)
  To: GCC Development

Hi,

In the process of fixing tests for AVR and other small targets I have 
come across issues with profile
tests in gcc/dg/struct that affect all targets and I would like reviewed 
so I can raise patches to rectify them correctly.

Both involve random creating structures then setting and checking value.

The first is a BUG in wo_prof_malloc_size_var.c

A random number (<32768) of structures are "mallocated", but then code 
always sets and checks 1000 of them.
Which will be undefined (aka crash)  if the random amount is less than 1000.
The obvious fix is to create random number  <1000 and check only those 
created. (but see next test first
before saying ok)


#include <stdlib.h>
typedef struct
{
  int a;
  float b;
}str_t;

#define N 1000

int
main ()
{
  int i, num;

  num = rand();
  str_t * p = malloc (num * sizeof (str_t));

  if (p == 0)
    return 0;

  for (i = 0; i < N; i++)
    p[i].b = i;

  for (i = 0; i < N; i++)
    p[i].a = p[i].b + 1;

  for (i = 0; i < N; i++)
    if (p[i].a != p[i].b + 1)
      abort ();
 
  return 0;
}


The other test that has  similar behavior is w_prof_two_strs.c

This does check  the random amount before malloc but oddly defaults to 
zero if the number exceeds 1000.  
This will work, but it seem likely the intent was to limit the random 
amount to 1000. Otherwise
32 times out of 33 it will not create structures.


#define N 1000

str_t1 *p1;
str_t2 *p2;
int num;

void
foo (void)
{
  int i;

  for (i=0; i < num; i++)
    p2[i].c = 2;
}

int
main ()
{
  int i, r;

  r = rand ();
  num = r > N ? N : num;
  p1 = malloc (num * sizeof (str_t1));
  p2 = malloc (num * sizeof (str_t2));

  if (p1 == NULL || p2 == NULL)
    return 0;

  for (i = 0; i < num; i++)
    p1[i].a = 1;

  foo ();

  for (i = 0; i < num; i++)
    if (p1[i].a != 1 || p2[i].c != 2)
      abort ();

  return 0;
}



So what should these two test do, with out messing up the test purposes?
I also note, some of the struct tests don't check the returned pointer 
from malloc - but some do.


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2008-06-10 23:17 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-10 23:17 Bugs in dg/struct profilr test - but what was intended? Andy H

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).