Article View: es.comp.os.linux.programacion
Article #659Problemas con punteros
From: arrase
Date: Sun, 04 Feb 2007 00:35
Date: Sun, 04 Feb 2007 00:35
105 lines
3913 bytes
3913 bytes
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;
};
}
}
--
<arrase@gulcas.org><http://arrase.no-ip.org><www.gulcas.org>
<Gnupg Keys: http://arrase.no-ip.org/pubkey.txt>
print "".join(map(lambda numChar:filter(lambda x:(x<"0") or \
(x>"9"),numChar),["0"+chr(104)+"0","1"+chr(111)+"1","2"+chr(108)+"2", \
"3"+chr(97)+"3","4"+chr(32)+"4","5"+chr(109)+"5","6"+chr(117)+"6", \
"7"+chr(110)+"7","8"+chr(100)+"8","9"+chr(111)+"9"]))
Technical Details
Message-ID:
<eq39a9$g7h$1@hercules.cohp1>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!homer!news.glorb.com!news-spur1.glorb.com!news.glorb.com!news.antakira.com!guada07.guadawireless.net!neozero.escomposlinux.org!hercules.escomposlinux.org!escomposlinux.org!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: f86c8,97b70f2db46c0da6,start X-Google-Attributes: gidf86c8,public X-Google-Language: FRENCH,ASCII Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!homer!news.glorb.com!news-spur1.glorb.com!news.glorb.com!news.antakira.com!guada07.guadawireless.net!neozero.escomposlinux.org!hercules.escomposlinux.org!escomposlinux.org!not-for-mail From: arrase <arrase@gulcas.org> Newsgroups: es.comp.os.linux.programacion Subject: Problemas con punteros Date: Sun, 04 Feb 2007 01:35:40 +0100 Organization: escomposlinux.org Lines: 105 Message-ID: <eq39a9$g7h$1@hercules.cohp1> Reply-To: arrase@gulcas.org NNTP-Posting-Host: 168.red-80-38-162.staticip.rima-tde.net Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: hercules.cohp1 1170548875 16625 80.38.162.168 (4 Feb 2007 00:27:55 GMT) X-Complaints-To: newsmaster@news-17.escomposlinux.org NNTP-Posting-Date: Sun, 4 Feb 2007 00:27:55 +0000 (UTC) User-Agent: Thunderbird 1.5.0.9 (X11/20061224) X-Enigmail-Version: 0.94.1.1