getline (C stdio.h) behaves incorrectly on 64 bit hardy/intrepid

Bug #365036 reported by Dan Davison
4
Affects Status Importance Assigned to Milestone
Ubuntu
Invalid
Undecided
Unassigned

Bug Description

I think the C function getline is behaving incorrectly in our 64 bit version of Ubuntu 8.04 and 8.10. The problem involves getline writing to areas of memory that it shouldn't. I agree that on the face of it that sounds unlikely. Here's my test code:

[CODE]
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv) {
    int i=10, maxlinelength=0 ;
    char *line=NULL ;

    maxlinelength = 0 ;
    line = NULL ;

    printf("%d, %d\n", i, maxlinelength) ;
    getline(&line, &maxlinelength, stdin) ;
    printf("%d, %d\n", i, maxlinelength) ;

    return 0 ;
}
[/CODE]

On 32 bit ubuntu 9.04 that prints what you'd expect:

[CODE]
10, 0

10, 120
[/CODE]

(the blank line comes from me hitting return when it tries to read from stdin; you can redirect a file into it if you want)

However on 64 bit ubuntu 8.04 and 8.10 that prints out

[CODE]
10, 0

0, 120
[/CODE]

I.e. it seems to me that getline has overwritten the memory location of the variable i.

The two machines that are producing incorrect results are

1)
[CODE]ash:~> lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 8.10
Release: 8.10
Codename: intrepid
ash:~> uname -a
Linux ash 2.6.27-11-server #1 SMP Thu Jan 29 20:13:12 UTC 2009 x86_64 GNU/Linux

[/CODE]
gcc 4.3.2

and

2)
[CODE]oak:~> uname -a
Linux oak 2.6.24-19-server #1 SMP Wed Aug 20 18:43:06 UTC 2008 x86_64 GNU/Linux
oak:~> lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 8.04.1
Release: 8.04
Codename: hardy
[/CODE]
gcc 4.2.4

Dan

Revision history for this message
Lars Ljung (larslj) wrote :

Thank you for your bug report, but the bug is actually in your code ;) On a 64-bit machine an int is still 32-bit but size_t is 64-bit, and that's the cause of your problem. Try using "size_t maxlinelength;" instead.

If you follow the man page for getline() and define _GNU_SOURCE you will get a warning about incompatible pointer type.

Changed in ubuntu:
status: New → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.