6

以下简单的程序段错误对我来说:

#include <string.h>
int main()
{
    void* voidp = NULL;
    char zeroes[sizeof(void*)];
    memset(zeroes, 0, sizeof(void*));
    int res = memcmp(&voidp, zeroes, sizeof(void*));
    return 0;
}

调试器显示段错误在 memcmp 中。

我的机器运行的是 64 位 Ubuntu 8.04,gcc 版本是 4.2.4。

它在我尝试过的其他机器(32 位或 64 位)上运行良好。

关于发生了什么的任何想法?

更新:这是汇编代码:

    .file   "failed.c"
    .text
.globl main
    .type   main, @function
main:
.LFB2:
    pushq   %rbp
.LCFI0:
    movq    %rsp, %rbp
.LCFI1:
    subq    $32, %rsp
.LCFI2:
    movq    %fs:40, %rax
    movq    %rax, -8(%rbp)
    xorl    %eax, %eax
    movq    $0, -32(%rbp)
    leaq    -16(%rbp), %rdi
    movl    $8, %edx
    movl    $0, %esi
    call    memset
    leaq    -16(%rbp), %rsi
    leaq    -32(%rbp), %rdi
    movl    $8, %edx
    call    memcmp
    movl    %eax, -20(%rbp)
    movl    $0, %eax
    movq    -8(%rbp), %rdx
    xorq    %fs:40, %rdx
    je  .L3
    call    __stack_chk_fail
.L3:
    leave
    ret
.LFE2:
    .size   main, .-main
    .section    .eh_frame,"a",@progbits
.Lframe1:
    .long   .LECIE1-.LSCIE1
.LSCIE1:
    .long   0x0
    .byte   0x1
    .string "zR"
    .uleb128 0x1
    .sleb128 -8
    .byte   0x10
    .uleb128 0x1
    .byte   0x3
    .byte   0xc
    .uleb128 0x7
    .uleb128 0x8
    .byte   0x90
    .uleb128 0x1
    .align 8
.LECIE1:
.LSFDE1:
    .long   .LEFDE1-.LASFDE1
.LASFDE1:
    .long   .LASFDE1-.Lframe1
    .long   .LFB2
    .long   .LFE2-.LFB2
    .uleb128 0x0
    .byte   0x4
    .long   .LCFI0-.LFB2
    .byte   0xe
    .uleb128 0x10
    .byte   0x86
    .uleb128 0x2
    .byte   0x4
    .long   .LCFI1-.LCFI0
    .byte   0xd
    .uleb128 0x6
    .align 8
.LEFDE1:
    .ident  "GCC: (GNU) 4.2.4 (Ubuntu 4.2.4-1ubuntu4)"
    .section    .note.GNU-stack,"",@progbits

更新:(这应该是评论,但不适合 600 个字符)。当我尝试sudo apt-get install libc6时,我得到:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
libc6 is already the newest version.
You might want to run `apt-get -f install' to correct these:
The following packages have unmet dependencies:
  libc6: Depends: libc-bin (= 2.11.1-0ubuntu7.5) but it is not installable
         Depends: findutils (>= 4.4.0-2ubuntu2) but 4.2.32-1ubuntu2 is to be installed
  libc6-dev: Depends: libc6 (= 2.7-10ubuntu5) but 2.11.1-0ubuntu7.5 is to be installed
  libc6-i386: Depends: libc6 (= 2.7-10ubuntu5) but 2.11.1-0ubuntu7.5 is to be installed
  locales: Depends: belocs-locales-bin (>= 2.4-2.2ubuntu2) but it is not going to be installed
  locales-all: Depends: glibc-2.11-1 but it is not installable
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).

最后,如果我尝试这样做sudo apt-get -f install,我会得到:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
libc6 is already the newest version.
You might want to run `apt-get -f install' to correct these:
The following packages have unmet dependencies:
  libc6: Depends: libc-bin (= 2.11.1-0ubuntu7.5) but it is not installable
         Depends: findutils (>= 4.4.0-2ubuntu2) but 4.2.32-1ubuntu2 is to be installed
  libc6-dev: Depends: libc6 (= 2.7-10ubuntu5) but 2.11.1-0ubuntu7.5 is to be installed
  libc6-i386: Depends: libc6 (= 2.7-10ubuntu5) but 2.11.1-0ubuntu7.5 is to be installed
  locales: Depends: belocs-locales-bin (>= 2.4-2.2ubuntu2) but it is not going to be installed
  locales-all: Depends: glibc-2.11-1 but it is not installable
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
botond@adaptive1:~$ sudo apt-get -f install
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Correcting dependencies... Done
The following packages were automatically installed and are no longer required:
  vim-runtime linux-libc-dev mysql-common openssh-blacklist
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  libdns35
The following packages will be REMOVED:
  adduser apparmor apparmor-utils apt apt-file apt-utils aptitude at atop base-files base-passwd bash bash-completion bind9-host binutils bsdmainutils bsdutils bzip2 command-not-found
  console-setup console-terminus console-tools coreutils cpio cpp cpp-4.2 cron curl dash debconf debconf-i18n debianutils dhcp3-client dhcp3-common diff dmidecode dmsetup dnsutils dosfstools dpkg
  e2fslibs e2fsprogs ed eject ethtool fdutils file findutils friendly-recovery ftp fuse-utils g++ g++-4.2 g++-4.2-multilib gcc gcc-4.2 gcc-4.2-multilib gdb-minimal gettext-base gnupg gpgv grep
  groff-base grub gzip hdparm hostname ifupdown info initramfs-tools initscripts inputattach iproute iptables iputils-arping iputils-ping iputils-tracepath klogd laptop-detect less lib32gcc1
  lib32gomp1 lib32stdc++6 libacl1 libapr1 libaprutil1 libapt-pkg-perl libatm1 libattr1 libbind9-30 libblkid1 libboost-dev libbz2-1.0 libc6 libc6-dev libc6-dev-i386 libc6-i386 libcap1
  libck-connector0 libcomerr2 libconfig-file-perl libconsole libcurl3 libcurl3-gnutls libcwidget3 libdb-dev libdb4.6 libdb4.6++ libdb4.6++-dev libdb4.6-dev libdbd-mysql-perl libdbi-perl
  libdbus-1-3 libdevmapper1.02.1 libedit2 libelfg0 libexpat1 libfribidi0 libfuse2 libgc1c2 libgcc1 libgcrypt11 libgdbm3 libglib2.0-0 libgnutls13 libgomp1 libgpg-error0 libgpmg1
  libhtml-parser-perl libhtml-tagset-perl libhtml-tree-perl libidn11 libisc35 libisccc30 libisccfg30 libiw29 libkeyutils1 libkrb53 libldap-2.4-2 liblist-moreutils-perl liblocale-gettext-perl
  liblwres30 liblzo2-2 libmagic1 libmysqlclient15off libncurses5 libncursesw5 libneon27 libnet-daemon-perl libnewt0.52 libntfs-3g23 libopencdk10 libpam-modules libpam0g libparted1.7-1 libpcap0.8
  libpcre3 libplrpc-perl libpopt0 libpq5 libreadline5 libreadline6 librpc-xml-perl libsasl2-2 libsasl2-modules libselinux1 libsepol1 libsigc++-2.0-0c2a libslang2 libsqlite3-0 libss2 libssl0.9.8
  libstdc++6 libstdc++6-4.2-dev libsvn1 libsysfs2 libtasn1-3 libterm-readkey-perl libtext-charwidth-perl libtext-iconv-perl libtext-wrapi18n-perl liburi-perl libusb-0.1-4 libuuid1 libvolume-id0
  libwrap0 libwww-perl libx11-6 libx11-data libxau6 libxcb-xlib0 libxcb1 libxdmcp6 libxext6 libxml-parser-perl libxml2 libxmuu1 linux-image-2.6.24-23-server linux-image-server linux-server
  linux-ubuntu-modules-2.6.24-23-server locales locales-all login logrotate lsb-base lsb-release lshw lsof ltrace lvm2 lzma make makedev man-db mawk menu mii-diag mktemp mlocate module-init-tools
  mount mtr-tiny mysql-client-5.0 mysql-server mysql-server-5.0 nano ncurses-base ncurses-bin net-tools netbase netcat netcat-traditional ntfs-3g ntp ntpdate openssh-client openssh-server parted
  passwd pciutils pcmciautils perl perl-base perl-modules popularity-contest ppp pppconfig pppoeconf procps psmisc python python-apt python-central python-cxx python-cxx-dev python-dev
  python-gdbm python-gnupginterface python-minimal python-mysqldb python-pkg-resources python-pycurl python-setuptools python-simplejson python-support python2.5 python2.5-dev python2.5-minimal
  reiserfsprogs rsync screen sed sshfs startup-tasks strace subversion sudo sysklogd system-services sysvutils tar tasksel tasksel-data tcpd tcpdump telnet time tzdata ubuntu-keyring
  ubuntu-minimal ubuntu-standard ucf udev ufw update-inetd update-manager-core upstart upstart-compat-sysv upstart-logd usbutils util-linux util-linux-locales uuid-runtime valgrind vim vim-common
  vim-tiny w3m wget whiptail wireless-tools wpasupplicant x11-common xauth zlib1g
The following packages will be upgraded:
  libdns35
WARNING: The following essential packages will be removed.
This should NOT be done unless you know exactly what you are doing!
  apt libc6 (due to apt) libgcc1 (due to apt) libstdc++6 (due to apt) base-files base-passwd (due to base-files) libpam-modules (due to base-files) bash debianutils (due to bash) libncurses5 (due
  to bash) bsdutils coreutils libacl1 (due to coreutils) libselinux1 (due to coreutils) dash mktemp (due to debianutils) diff dpkg lzma (due to dpkg) e2fsprogs e2fslibs (due to e2fsprogs)
  libblkid1 (due to e2fsprogs) libcomerr2 (due to e2fsprogs) libss2 (due to e2fsprogs) libuuid1 (due to e2fsprogs) findutils grep gzip hostname login libpam0g (due to login) mount ncurses-base
  ncurses-bin perl-base python-minimal python2.5-minimal (due to python-minimal) sed sysvutils libsepol1 (due to sysvutils) tar util-linux lsb-base (due to util-linux) tzdata (due to util-linux)
  libslang2 (due to util-linux) zlib1g (due to util-linux)
1 upgraded, 0 newly installed, 316 to remove and 4 not upgraded.
4 not fully installed or removed.
Need to get 11.3kB of archives.
After this operation, 712MB disk space will be freed.
You are about to do something potentially harmful.
To continue type in the phrase 'Yes, do as I say!'
 ?] 

显然,我没有输入“是的,照我说的做!”。似乎有些事情真的搞砸了......知道如何解决它吗?

4

1 回答 1

1

我可以肯定,在带有 GCC 4.1.2 的 RHEL 5 上,代码工作正常,无论是 32 位还是 64 位(AMD x86/64 架构)。

您(可能)发现了一个错误。

也许您应该升级到 GCC 4.5.2(在过去 24 小时左右发布 - 截至 2010 年 12 月 19 日)。或者您可能需要升级 C 库,但这更有可能是编译器问题而不是库问题。

于 2010-12-20T00:03:37.030 回答