:: Re: [DNG] netman-gtk3: invalid poin…
Página superior
Eliminar este mensaje
Responder a este mensaje
Autor: aitor_czr
Fecha:  
A: Irrwahn, Edward Bartolo, dng
Asunto: Re: [DNG] netman-gtk3: invalid pointer error
Hi Irrwahn,

On 01/06/2016 11:24 PM, dng-request@??? wrote:
> Note: I didn't try to undertand what the code is intended
> to do, just commenting on some obvious flaws.
>
>> >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));
> Better C style would've been:
>
>      cad1=malloc(1024);

>
> Casting malloc()'s return value is nonsense and potentially dangerous,
> sizeof(char) is always guaranteed to equal 1.
>
> [...]
>
>> >          cad2 = strstr((char *) cad1, name);
>> >          res  = strstr((char *) cad2, str1);
> Now you assigned to cad2 and res and lost your only reference
> to your meticulously allocated buffers! (More bogus casts, BTW.)

>
> [...]
>
> And then you tried to free something that was not a pointer value
> returned by any of the *alloc() functions:
>
>> > // free(cad2);
>> > // free(res);
> [...]
>
> As I said, I didn't try to guess the intention - if you want to
> work in-place an the string or if you're actually looking for strcpy().
>
> HTH, regards
> Irrwahn


And thanks for your replay :)

I think that the origin of the issue is in the 'strstr' function. Have a
look at the 'scan_dim' method. This method runs 'backend 8' and edits
the textual output in a file named 'output'. It also returns an integer
'cont' (the number of available active wifis).

At the end of this method I can free *cad1; I also can free *cad2...;
*But i can't free both pointers*. Maybe the have common memory
addresses? I think so.

Here is the new 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:",
"\"", "\n");
scan_buffer (_quality, "/usr/lib/netman/bin/backend 8", "Quality=",
"=" , "Signal level");
scan_buffer (_address, "/usr/lib/netman/bin/backend 8", "Address:", "
" , "\n");

   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]);
   }


char **num, **denom;

   num = (char**)malloc(N*sizeof(char*));
   if(!num) {printf("Fallo de asignación de memoria\n"); exit(-1);}
   for(i=0; i<N; i++)
   {
     num[i] = (char*)malloc(3*sizeof(char));
     if(!num[i]) {printf("Fallo de asignación de memoria\n");
                  exit(-1);
                 }
    }


   denom = (char**)malloc(N*sizeof(char*));
   if(!denom) {printf("Fallo de asignación de memoria\n"); exit(-1);}
   for(i=0; i<N; i++)
   {
     denom[i] = (char*)malloc(3*sizeof(char));
     if(!denom[i]) {printf("Fallo de asignación de memoria\n");
                  exit(-1);
                 }
    }


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=malloc(1024);
       if(!cad1) {printf("Fallo de asignación de memoria\n"); exit(-1);}


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


   res=malloc(1024);
       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[strlen(res)]='\0';



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


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


          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=malloc(1024);
       if(!cad1) {printf("Fallo de asignación de memoria\n"); exit(-1);}


   cad2=malloc(1024);
       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);
             }
   }


// free(cad1);
free(cad2);
pclose(fp);
fclose(fp1);
return cont;
}