Article View: es.comp.os.linux.programacion
Article #661Re: Problemas con punteros
From: Discreto
Date: Sun, 04 Feb 2007 06:10
Date: Sun, 04 Feb 2007 06:10
107 lines
4029 bytes
4029 bytes
A strtok no le puedes enviar como cadena principal un NULL, tiene que
ser una cadena valida.
arrase escribió:
> No logro que este codigo adjunto funcione, si lo compilais tal cual
> parece que todo va bien pero en realidad hay un overflow, en concreto la
> primera vez que se ejecuta la linea *aux=strtok(NULL," "); se esta
> escribiendo fuera de la memoria reservada, la manera mas facil de verlo
> si teneis electric fence [1] instalado es compilarlo con:
>
> gcc aux.c -lefence -ggdb -o aux
>
> y correrlo en gdb, podreis ver esta salida:
>
> Program received signal SIGSEGV, Segmentation fault.
> 0x0804864d in main () at aux.c:32
> 32 *aux=strtok(NULL," ");
>
> El problema es que no se como reservar correctamente la memoria para que
> sea un puntero que apunta a un array de tipo char, que basicamente es lo
> que getopt necesita.
>
> ¿Alguien me puede echar una mano?
>
> Saludos.
>
> [1] http://perens.com/FreeSoftware/
>
> ================= AUX.C =================
>
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <getopt.h>
>
> struct module{
> char **args;
> };
>
> struct node{
> struct module *mod;
> };
>
> void print_r(struct module *mod);
> void parse(int argc, char **argv);
>
> int main(){
> char str[]="-a aaaa -b bbbb";
> char **aux;
> struct node *nod;
>
> nod=(struct node *)malloc(sizeof(struct node));
> nod->mod=(struct module *)malloc(sizeof(struct module));
> nod->mod->args=(char**)malloc(sizeof(char)*6);
> aux=nod->mod->args;
> *aux="asd";
> aux++;
> *aux=strtok(str," ");
> while(*aux!=NULL){
> aux++;
> *aux=strtok(NULL," ");
> }
> print_r(nod->mod);
> parse(5,nod->mod->args);
> }
>
> void print_r(struct module *mod){
> char **aux;
> aux=mod->args;
> while(*aux!=NULL){
> printf("%s\n",*aux);
> aux++;
> }
> }
> void parse(int argc, char **argv){
> /* Store current commad line argument */
> int next_opt=0;
> /* Short options */
> const char* const short_opt = "a:b:";
> /* Long options */
> const struct option long_opt[] =
> {
> { "--A", 1, NULL, 'a' },
> { "--B", 1, NULL, 'b' },
> /* End of long options */
> { NULL, 0, NULL, 0 }
> };
> /* Start parsing command line */
> while(next_opt!=-1){
> next_opt=getopt_long(argc, argv, short_opt, long_opt, NULL);
> switch(next_opt){
> case 'a':
> puts(optarg);
> break;
> case 'b':
> puts(optarg);
> break;
> };
> }
> }
Technical Details
Message-ID:
<CNexh.10415$vZ.5907@fe167.usenetserver.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!uns-out.usenetserver.com!news.usenetserver.com!pc02.usenetserver.com!fe167.usenetserver.com.POSTED!232df425!not-for-mailReferences:
<eq39a9$g7h$1@hercules.cohp1>Show Full Headers
Complete Article Headers
X-Google-Thread: f86c8,97b70f2db46c0da6 X-Google-Attributes: gidf86c8,public X-Google-Language: SPANISH,ASCII Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!uns-out.usenetserver.com!news.usenetserver.com!pc02.usenetserver.com!fe167.usenetserver.com.POSTED!232df425!not-for-mail From: Discreto <ftp777@terra.es> User-Agent: Thunderbird 1.5.0.9 (Windows/20061207) MIME-Version: 1.0 Newsgroups: es.comp.os.linux.programacion Subject: Re: Problemas con punteros References: <eq39a9$g7h$1@hercules.cohp1> In-Reply-To: <eq39a9$g7h$1@hercules.cohp1> Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: quoted-printable Lines: 108 Message-ID: <CNexh.10415$vZ.5907@fe167.usenetserver.com> X-Complaints-To: abuse@usenetserver.com X-Abuse-Info: Please be sure to forward a copy of ALL headers X-Abuse-Info: Otherwise we will be unable to process your complaint properly. NNTP-Posting-Date: Sun, 04 Feb 2007 01:10:42 EST Date: Sun, 04 Feb 2007 07:10:38 +0100