:: Re: [DNG] netman-gtk3: invalid poin…
Góra strony
Delete this message
Reply to this message
Autor: aitor_czr
Data:  
Dla: Edward Bartolo, rainerweikusat, dng
Temat: Re: [DNG] netman-gtk3: invalid pointer error
On 01/06/2016 07:59 PM, Edward Bartolo <edbarx@???> wrote:
> Hi,
>
> I can't free the memory of some pointers ( *cad2 and *res) in
> netman-gtk3. This is what i get:
>
> ~$ gcc main.c -o main
>
> ~$ ./main
> *** Error in `./main': munmap_chunk(): invalid pointer:
> 0x0000000001bfdab4 ***
> Aborted
>
> See the comented lines 119 and 120 in the attached file.
>
> The textual output of this applications shows the caracteristics of the
> available active wifis.
>
> Any hints?
>
>      Aitor.

>
> Note: you need to install the backend of netman.


This is the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int N;
int scan_dim();
void scan_buffer (char **ptr, char *command, char *name, char *str1,
char *str2);

int main(int argc, char **argv)
{
int i;
char **_essid, **_address, **_quality;

N=scan_dim();

_essid = (char**)malloc(N*sizeof(char*));
if(!_essid) {printf("Fallo de asignación de memoria\n"); exit(-1);}

_address = (char**)malloc(N*sizeof(char*));
if(!_address) {printf("Fallo de asignación de memoria\n"); exit(-1);}

_quality = (char**)malloc(N*sizeof(char*));
if(!_quality) {printf("Fallo de asignación de memoria\n"); exit(-1);}

scan_buffer (_essid, "/usr/lib/netman/bin/backend 8", "ESSID:",
"\"", "\"");
scan_buffer (_quality, "/usr/lib/netman/bin/backend 8", "Quality=",
"=" , "Signal level");
scan_buffer (_address, "/usr/lib/netman/bin/backend 8", "Address:", "
" , "\"");

   for(i=0; i<N; i++)
   {
     printf("%s\n", _essid[i]);
     free(_essid[i]);
   }


   for(i=0; i<N; i++)
   {
     printf("%s\n", _address[i]);
     free(_address[i]);
   }


   for(i=0; i<N; i++)
   {
     printf("%s\n", _quality[i]);
     free(_quality[i]);
   }


free(_essid);
free(_address);
free(_quality);
system("rm -f output");
return 0;
}

void scan_buffer (char **ptr, char *command, char *name, char *str1,
char *str2)
{
FILE *fp;
char *cad1, *cad2, *res;

   cad1=(char*)malloc(1024*sizeof(char));
       if(!cad1) {printf("Fallo de asignación de memoria\n"); exit(-1);}


   cad2=(char*)malloc(1024*sizeof(char));
       if(!cad2) {printf("Fallo de asignación de memoria\n"); exit(-1);}


   res=(char*)malloc(1024*sizeof(char));
       if(!res) {printf("Fallo de asignación de memoria\n"); exit(-1);}


fp = fopen("output", "r");
if(!fp) exit(-1);

   int cont=0;
   while (fgets(cad1, 1024, fp))
   {
     if(strstr((char *) cad1, name))
        {
          cad2 = strstr((char *) cad1, name);
          res  = strstr((char *) cad2, str1);


          if(name!="Address:")
          {
           int bool=0;
           int i=0;
           int n,m;
           while ((bool!=1) && (res[i]!='\0'))
             {
              if(str2[0]==res[i])
                 {
                   m=i; n=0;
                 while ((bool!=1) && (str1[n]==res[m]))
                         {
                           n++; m++;
                           if (n == strlen(res)) bool=1;
                         }
                 }
              i++;
             }


           for (i=(m-n); res[i]!='\0';i++) res[i]='\0';
          }
          else res[18]='\0';


          int j=0;
          while(j<strlen(res)) {res[j]=res[j+1]; j++;}
          res[j]='\0';


          j=1;
          while(res[strlen(res)-j]==' ') {res[strlen(res)-j]='\0'; j++;}


          ptr[cont] = (char*)malloc(strlen(res)*sizeof(char));
          if(!ptr[cont]) {printf("Fallo de asignación de memoria\n"); 
exit(-1);}


          strcpy(ptr[cont], res);
          ptr[cont][strlen(res)]='\0';
          cont++;
        }
   }


free(cad1);
// free(cad2);
// free(res);
fclose(fp);
return;
}

int scan_dim ()
{
FILE *fp, *fp1;
char *cad1, *cad2;

   cad1=(char*)malloc(1024*sizeof(char));
       if(!cad1) {printf("Fallo de asignación de memoria\n"); exit(-1);}


   cad2=(char*)malloc(1024*sizeof(char));
       if(!cad2) {printf("Fallo de asignación de memoria\n"); exit(-1);}


fp = popen("/usr/lib/netman/bin/backend 8", "r");
if(!fp) exit(-1);

fp1 = fopen("output", "w");
if(!fp1) exit(-1);

   int cont=0;
   while (fgets(cad1, 1024, fp))
   {
     if(strstr((char *) cad1, "Address:"))
        {
           cad2 = strstr((char *) cad1, "Address:");
           fprintf(fp1, "%s", cad2);
          cont++;
        }
     else if(strstr((char *) cad1, " "))
             {
               cad2 = strstr((char *) cad1, " ");
               fprintf(fp1, "%s", cad2);
             }
   }


pclose(fp);
fclose(fp1);
return cont;
}