After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 313127 - I replaced qsort of stdlib.h with g_qsort_with_data, and gets SegV.
I replaced qsort of stdlib.h with g_qsort_with_data, and gets SegV.
Status: RESOLVED NOTABUG
Product: glib
Classification: Platform
Component: general
2.7.x
Other All
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2005-08-10 15:10 UTC by teruakigemma
Modified: 2005-08-10 16:13 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description teruakigemma 2005-08-10 15:10:12 UTC
Please describe the problem:
To get shuffled natural numbers sequence, I writed a cmp_func which returns [-1,
0,1] randomly and then quick sorted the array of integers. When I used qsort of 
stdlib.h , it just worked fine, but when replace it with g_qsort_with_data of 
glib.h, SOMETIMES causes SegV. 

Steps to reproduce:
#include <glib.h>
#define A 16

gint gint_cmp_shuffle(const gint *a, const gint *b, gpointer user_data)
{
  return g_random_int_range(-1, 2);
}

gint main(gint argc, gchar *argv[])
{ 
  gint i;
  gint table[A];
  for (i = 0; i < A; i++) table[i] = i;
  for (i = 0; i < 128; i++) {
    g_print("%d\n", i);
    g_qsort_with_data(table, A, sizeof(gint), (GCompareDataFunc)
gint_cmp_shuffle, NULL);
  }
  return 0;
}


Actual results:
Outputs of gprint are like "0 1 2 ... 17 Segmentation fault" "0 1 2 ... 6 
Segmentation fault".

Expected results:
"0 1 2 ... 126 127"

Does this happen every time?
Yes.

Other information:
Following codes works fine, it always outputs "0 1 2 ... 126 127" and ends 
normally.

#include <stdlib.h>
#include <glib.h>
#define A 16

int int_cmp_shuffle(const int *a, const int *b)
{
  return g_random_int_range(-1, 2);
}

int main(int argc, char *argv[])
{ 
  int i;
  int table[A];
  for (i = 0; i < A; i++) table[i] = i;
  for (i = 0; i < 128; i++) {
    g_print("%d\n", i);
    qsort(table, A, sizeof(int), (int(*)(const void*, const void*))
int_cmp_shuffle);
  }
  return 0;
}
Comment 1 Matthias Clasen 2005-08-10 16:13:21 UTC
What makes you think that would work ? the compare function passed to qsort has to 
implement a (partial) order relation, ie be at least transitive and antisymmetric