IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Discussion :

[C] un petit heisenbug avec des printfs


Sujet :

Windows

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juin 2005
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2005
    Messages : 8
    Points : 9
    Points
    9
    Par défaut [C] un petit heisenbug avec des printfs
    Bonjour,

    J'ai à programmer un logiciel qui dump la structure d'un exécutable au format PE. Le problème, c'est que mon programme souffre d'un petit heisenbug. Si je rajoute des printf, des ftell... il se comporte différemment, et la je commence à avoir le cerveau en bouilli à force de tourner en rond. Un petit coup de pouce serait le bien venu

    Code C : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
     
    #include "stdafx.h"
    #include <stdio.h>
    #include <shlwapi.h> // For types
     
     
    typedef struct _PE_FILE_HEADER
    {
    	ULONG					signature;
    	IMAGE_FILE_HEADER		header;
    	IMAGE_OPTIONAL_HEADER	optionalHeader;
    }PE_FILE_HEADER;
     
     
     
    ULONG main(ULONG argc, PCHAR argv[])
    {
    	IMAGE_DOS_HEADER		dosHeader;
    	PE_FILE_HEADER			peHeader;
    	PIMAGE_SECTION_HEADER	pSections;
     
    	FILE* file;
    	errno_t errno;
    	PCHAR szFilename = "toto.exe";
     
    	if(argc == 2)
    	{
    		szFilename = argv[1];
    		printf("Using %s as file\n", szFilename);
    	}
     
    	memset(&dosHeader, 0, sizeof(IMAGE_DOS_HEADER));
    	memset(&peHeader, 0, sizeof(PE_FILE_HEADER));
     
    	if((errno = fopen_s(&file, szFilename, "r")) != 0)
    	{
    		perror("Error while opening file");
    		return -1;
    	}
    	if (fread(&dosHeader, sizeof(IMAGE_DOS_HEADER), 1, file) != 1)
    	{
    		printf("Error while reading IMAGE_DOS_HEADER\n");
    		return -1;
    	}
     
    	if(fseek(file, dosHeader.e_lfanew, 0) != 0)
    	{
    		printf("Error while seeking PE header start\n");
    		return -1;
    	}
     
    	if(fread(&peHeader, sizeof(PE_FILE_HEADER), 1, file) != 1)
    	{
    		printf("Error while reading IMAGE_DOS_HEADER\n");
    		return -1;
    	}
     
    	printf("Number of sections : %d\n", peHeader.header.NumberOfSections);
    	printf("Entry point : %#8lx\n", peHeader.optionalHeader.AddressOfEntryPoint);
    	printf("File alignment: %#8lx\n", peHeader.optionalHeader.FileAlignment);
     
    	if((pSections = (PIMAGE_SECTION_HEADER)calloc(peHeader.header.NumberOfSections, sizeof(IMAGE_SECTION_HEADER))) == NULL)
    	{
    		printf("Error while allocation memory for the sections\n");
    		return -1;
    	}
     
    	printf("%#8lx\n",ftell(file));
    	size_t readSize;
    	if((readSize = fread(pSections, sizeof(IMAGE_SECTION_HEADER), peHeader.header.NumberOfSections, file)) != peHeader.header.NumberOfSections)
    	{
    		printf("Error while reading IMAGE_SECTION_HEADER\n");
    		printf("Only %d sections read\n", readSize);
    		return -1;
    	}
     
     
    	for(int i = 0; i < peHeader.header.NumberOfSections; i++)
    	{
    		printf("Section %s : \n", pSections[i].Name);
     
    		printf("\tCharacteristics : %#8lx\n", pSections[i].Characteristics);
    		printf("\tMisc : %#8lx\n", pSections[i].Misc);
     
    		printf("\tNumber of line numbers : %#x\n", pSections[i].NumberOfLinenumbers);
    		printf("\tPointer to line numbers : %#x\n", pSections[i].PointerToLinenumbers);
     
    		printf("\tNumber of relocations : %#x\n", pSections[i].NumberOfRelocations);
    		printf("\tPointer to relocations : %#8lx\n", pSections[i].PointerToRelocations);
     
    		printf("\tSize of raw data : %#8lx\n", pSections[i].SizeOfRawData);
    		printf("\tPointer to raw data : %#8lx\n", pSections[i].PointerToRawData);
     
    		printf("\tVirtual address : %#8lx\n", pSections[i].VirtualAddress);
     
    		printf("\tEnd of raw data : %#8lx\n", pSections[i].SizeOfRawData + pSections[i].PointerToRawData);
    	}
     
    	return 0;
    }


    Ce code me produit comme résultat :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    C:\>toto.exe
    Number of sections : 4
    Entry point :   0x155f
    File alignment:    0x200
    Error while reading IMAGE_SECTION_HEADER
    Only 3 sections read
    Si j'enleve le commentaire du ftell:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    C:\>toto.exe
    Number of sections : 4
    Entry point :   0x156f
    File alignment:    0x200
       0x1d9
    Section .text :
            Characteristics : 0x60000020
            Misc :    0x9e8
            Number of line numbers : 0
            Pointer to line numbers : 0
            Number of relocations : 0
            Pointer to relocations :        0
            Size of raw data :    0xa00
            Pointer to raw data :    0x400
            Virtual address :   0x1000
            End of raw data :    0xe00
    Section .rdata :
            Characteristics : 0x40000040
            Misc :    0x8fa
            Number of line numbers : 0
            Pointer to line numbers : 0
            Number of relocations : 0
            Pointer to relocations :        0
            Size of raw data :    0xa00
            Pointer to raw data :    0xe00
            Virtual address :   0x2000
            End of raw data :   0x1800
    Section .data :
            Characteristics : 0xc0000040
            Misc :    0x388
            Number of line numbers : 0
            Pointer to line numbers : 0
            Number of relocations : 0
            Pointer to relocations :        0
            Size of raw data :    0x200
            Pointer to raw data :   0x1800
            Virtual address :   0x3000
            End of raw data :   0x1a00
    Section .rsrc :
            Characteristics :        0
            Misc :    0x1ac
            Number of line numbers : 0
            Pointer to line numbers : 0
            Number of relocations : 0
            Pointer to relocations :        0
            Size of raw data :    0x200
            Pointer to raw data :        0
            Virtual address :   0x4000
            End of raw data :    0x200
    Il n'y a pas d'erreur dans mon fread, mais néammoins, il me lit un peu n'importe quoi (la section rsrc se termine en 0x1c00 par exemple).

    Et si je supprime des printf dans ma dernière boucles (j'en laisse que 4 maxi), cela marche correctement (tout comme quand je compile en mode debug). Ca m'a tout l'aire d'etre n beau buffer overflow, mais je le vois pas :s

    Merci de toute aide ou idée apportée

  2. #2
    Futur Membre du Club
    Profil pro
    Inscrit en
    Juin 2005
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2005
    Messages : 8
    Points : 9
    Points
    9
    Par défaut
    Bon bah après avoir réécrit le code from scratch, j'ai trouvé le bug. Je fesais tout simplement un fopen(..., "r") sur un fichier binaire.
    La programmation Linux à la vie dure.. Mais bon c'est quand même pas très explicite comme retour d'erreur

  3. #3
    Expert éminent sénior
    Avatar de Médinoc
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Septembre 2005
    Messages
    27 379
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 40
    Localisation : France

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2005
    Messages : 27 379
    Points : 41 573
    Points
    41 573
    Par défaut
    En fait, c'est une mauvaise pratique dans toute programmation C, puisque c'est le standard qui demande le b, pas Windows...
    Mais sous un système POSIX, le programme "tombe en marche" malgré l'absence de b...

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Petits soucis avec des vecteurs
    Par Superzobi dans le forum SL & STL
    Réponses: 3
    Dernier message: 03/05/2007, 09h04
  2. en fin d'apprentisage avec des petits problemes!
    Par nanon dans le forum Droit du travail
    Réponses: 19
    Dernier message: 04/09/2006, 12h01
  3. Petit problemes de variables avec des Packages
    Par Invité dans le forum Modules
    Réponses: 4
    Dernier message: 02/08/2006, 17h08
  4. un petit code tout simple o_O avec des $POST variable :)
    Par dark_vidor dans le forum Langage
    Réponses: 5
    Dernier message: 08/04/2006, 01h08
  5. petit souci avec des variables avec des fonctions psql
    Par dust62 dans le forum PostgreSQL
    Réponses: 4
    Dernier message: 02/04/2005, 13h45

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo