Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #157026

Re: Segment fault ../sysdeps/i386/i686/multiarch/strcpy-sse2.S

From "Thomas Schmitt" <scdbackup@gmx.net>
Newsgroups linux.debian.user
Subject Re: Segment fault ../sysdeps/i386/i686/multiarch/strcpy-sse2.S
Date 2015-08-29 09:20 +0200
Message-ID <q2IAG-3ek-17@gated-at.bofh.it> (permalink)
References <q2Ihj-2Cx-1@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


Hi,

Dan Richard wrote
> char *str;   strcpy(str, "hello");

You are writing to a random memory location.
The pointer str is not initialized to point to valid
memory, which you would have to allocate previously.

Try instead

  char str[100];
 
  strcpy(str, "hello");

This is good for strings up to 99 characters plus
trailing 0 as terminator.

If your string length is only predictable at run time
then use dynamic memory allocation

  ...
  #include <stdlib.h>
  ...

  char *str;
  int lstr;

  lstr= strlen(argv[1]);

  /* >>> maybe check for absurd size */

  str= calloc(1, lstr + 1);
  if(str == NULL) {

     /* >>> bail out with message about lack of memory */;

  }
  strcpy(str, argv[1]);

(Warning: Written from wetware memory. Not actually tested.
          Take this only as sketch for the real code.)

Further reading:
  Classic: Kernighan, Ritchie, The C Programming Language
  Online:  google "C language tutorial"
  Recreational: http://crashworks.org/if_programming_languages_were_vehicles/


Have a nice day :)

Thomas

Back to linux.debian.user | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Re: Segment fault ../sysdeps/i386/i686/multiarch/strcpy-sse2.S "Thomas Schmitt" <scdbackup@gmx.net> - 2015-08-29 09:20 +0200
  Re: Segment fault ../sysdeps/i386/i686/multiarch/strcpy-sse2.S Lev Lazinskiy <lev@levlaz.org> - 2015-08-29 09:30 +0200

csiph-web