Click here to Skip to main content
12,401,069 members (62,093 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

7.1K views
3 bookmarked
Posted

Issues of 64-bit code in real programs: qsort

, 13 Jan 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
We continue the cycle of posts about 64-bit errors detected in real applications. Time passes, demands for memory being consumed grow more and more, and now the time has come when somebody decides to sort an array consisting of more than 2^31 items. For that purpose this person chooses the function
This is an old version of the currently published technical blog.

We continue the cycle of posts about 64-bit errors detected in real applications. Time passes, demands for memory being consumed grow more and more, and now the time has come when somebody decides to sort an array consisting of more than 2^31 items. For that purpose this person chooses the function qsort implemented in OpenBSD 4.5. The result is a 64-bit error detected. The post “library/6287: 64-bit unsafety of qsort(3)” describes this error and explains the way to fix it.

Let us consider this error in detail. At the moment of writing this post, the last revision of the file “lib/libc/stdlib/qsort.c” included into OpenBSD 4.6. dates back to August, 2005. There, the function qsort employs the auxiliary variables “d” and “r” that have the type int:

void
qsort(void *aa, size_t n, size_t es,
  int (*cmp)(const void *, const void *))
{
  char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
  int d, r, swaptype, swap_cnt;
  char *a = aa;
  . . . .

You may see the full text of the function here: http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdlib/qsort.c?rev=1.10;content-type=text%2Fplain

These variables are used to store different sizes and it leads to errors when processing a large number of items. The correction lies in declaring these variables as size_t:

size_t d, r;

This error is exemplary because it was detected only in 2010! It seems that a large number of errors in 64-bit programs will begin to occur when a standard user computer has more than 16 Gbytes of memory and programs begin to exploit it very actively.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


You may also be interested in...

Comments and Discussions


Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 13 Jan 2010
Article Copyright 2010 by viva64.com
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid