0

So here's my problem (my questions are at the end),

I'm trying to initialize a list of "Personne" from a file. Here's the struct of "Personne" and the code i'm using :

struct :

struct s_personne {
    char name[STRING];
    unsigned char password[SHA256_DIGEST_LENGTH];
    char *salt;
    char email[LSTRING];
};

code :

int parseMyJsonPersonne(Plist l){
    FILE* fichier = NULL;
    char buffer[1024];
    char temp[502];

    struct json_object *parsed_json;
    struct json_object *name;
    struct json_object *password;
    struct json_object *salt;
    struct json_object *email;

    Personne p = (Personne)malloc(sizeof(Personne));

    fichier = fopen("../../data/Personne.json", "r");
    if (fichier != NULL) {
        while (fgets(temp, 502, fichier)!=NULL) {
            //printf("temp : %s", temp);
            strncat(buffer, temp, strlen(temp));
            if (strcmp(temp, "}\n")==0) {

                //printf("Object find\n");
                //printf("buffer : \n%s\n\n", buffer);

                parsed_json = json_tokener_parse(buffer);

                json_object_object_get_ex(parsed_json, "name", &name);
                json_object_object_get_ex(parsed_json, "password", &password);
                json_object_object_get_ex(parsed_json, "salt", &salt);
                json_object_object_get_ex(parsed_json, "email", &email);

                if (setPAll(p, (char *)json_object_get_string(name), (char *)json_object_get_string(password), (char *)json_object_get_string(salt), (char *)json_object_get_string(email))!=0) {
                    puts("Une ERREUR est survenue dans l'assemblage d'un objet (type=Personne)\n");
                    return 1;
                }
                if (addPersonne(p, l)!=0){
                    puts("Une ERREUR est survenue dans l'assemblage d'un objet (type=PList)\n");
                    return 1;
                }
                memset(buffer, '\0', 1024);
            }
        }
    }
    else {
        puts("Une ERREUR est survenue lors du chargement des différents comptes\n\n");
        return 1;
    }

    puts("Checkpoint 1 !!!");
    fclose(fichier);
    puts("Checkpoint 2 !!!");

    free(p);
    free(name);
    free(password);
    free(salt);
    free(email);

    return 0;
}

The error occurs when I try to make the fclose(fichier); however I already know that the cause of the problem is when calling the setPAll function. This one calls the setPPassword funtion which has the following code:

int setPPassword(Personne p, char * password) {
    char stock[2];
    long val;
    printf("length of password (=64 ?) : %ld\n", strlen(password));
    printf("lenght p->password (=32 ?) : %d\n\n", SHA256_DIGEST_LENGTH);
    if (strlen(password) != 64) {
        puts("ERROR in setPPassword");
        return 1;
    }
    for(size_t i=0; i<SHA256_DIGEST_LENGTH; i++) {
        stock[0]=password[2*i];
        stock[1]=password[2*i+1];
        printf("stock : %s\n", stock);
        val = strtol(stock, NULL, 16);
        p->password[i]=(unsigned char)val;
        printf("value in p->password at %ld : %02x\n",i, p->password[i]);
    }
    return 0;
}

the problem is due to the p->password[i]=(unsigned char)val;

The problem here is that I really need my p->password to contain hex values. So is there a way to fix this error while initializing my p->password or is there a way to work around it by changing my setPPassword function?

magjico
  • 3
  • 4
  • 2
    You have plenty of code that doesn't really make any sense. Like for example how you use `stock` as a null-terminated string when it's not really a null-terminated string (misses the actual null-terminator). – Some programmer dude Apr 10 '20 at 10:02
  • 2
    Also, what does the `addPersonne` do? Does it add the ***pointer*** to a container of some sort? Then `free(p)` is probably the wrong thing to do. – Some programmer dude Apr 10 '20 at 10:02
  • For the addPersonne funtion it uses realloc to add a person to the list. – magjico Apr 10 '20 at 10:28
  • the Personne p = malloc(sizeof(*p)); returns an error. – magjico Apr 10 '20 at 10:30
  • 2
    Please create a [mcve], and [edit] your question to show it. Also please take some to read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Apr 10 '20 at 10:37
  • the error from the malloc is : error: dereferencing pointer to incomplete type ‘struct s_personne’ And here's the code addPersonne : int addPersonne(Personne r,Plist l){ if ((l->size)==0) { l->List = (Personne *)malloc(1*(sizeof(Personne))); }else{ l->List = realloc(l->List,(l->size+1)*(sizeof(Personne))); }
    (l->List)[l->size] = r; l->size++; return 0; }
    – magjico Apr 10 '20 at 11:14
  • 2
    Have you included the header file where you define `struct s_personne`? Also on a related subject, in C you [should not cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Apr 10 '20 at 11:29
  • I solved my problem, thank you very much @Some programmer dude it was indeed due to my addPersonne function and free(p) I should have been more careful. Sorry. – magjico Apr 10 '20 at 12:15

0 Answers0