Thread: Segment Violation (Overflow of memory)

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    1

    Segment Violation (Overflow of memory)

    With the sample.c program I tries to convert a pseudo-code (from mycode.txt file) in an assembly code (to a future file.asm)

    Code:
    root@ferran-F5GL:/home/ferran/Documents/C# cat mycode.txt
    data:
        db msg="Hello, World!\n";
        equ len(msg);
    
    
    start:
        sys_write(len, msg, 1, 4);
        kernel;
        sys_exit(1);
        kernel;
    
    
    root@ferran-F5GL:/home/ferran/Documents/C# ./sample mycode.txt
    section	.data
    
    
    msg	db	'Hello, World',0xa
    len	equ $ - msg
    Violació de segment (s'ha bolcat la memòria)
    root@ferran-F5GL:/home/ferran/Documents/C#
    "Violació de segment (s'ha bolcat la memòria)" in english means "Segment violation (overflow memory)

    the sample.c code is

    Code:
    /*
    This program will tries to convert a pseudo-code
     from a text file to assembly code
     which will -later- be saved in an .asm file.
    
    
    (By the moment don't exists any real conversion.
    The program currently makes a copy of a assembly file
    meanwhile i fix this).
    */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main( int argc, char *argv[] ) {
    
    
        char myfile[20];
        char myline[100];
        char *token;
        const char sptr[10]=" ;()[]\t\n";
    
    
        token = (char*) malloc (sizeof (50));
    
    
        FILE *fp;
    
    
        if ( argc == 2 ) {
            strcpy(myfile, argv[1]);
        }
        else {
            printf("Invalid number or arguments.\n");
        }
    
    
        fp = fopen(myfile,"r");
    
    
        if ( fp == NULL ) {
            fputs("Error in file ", stderr);
            exit(1);
        }
        else {
            while ( feof(fp) == 0 )
            {
                fgets(myline,100,fp);
                token = strtok(myline, sptr);
                   if ( strcmp(token, "data:") == 0 ) {
                        printf("section\t.data\n\n");
                        }
                    else if ( strcmp(token, "bss:") == 0 ) {
                        printf("section\t.bss\n\n");
                        }
                    else if ( strcmp(token, "start:") == 0 ) {
                                printf("section\t.text\n");
                                printf("global\t_start\n\n");
                                printf("\t_start:\n\n");
                        }
                    else if ( strcmp(token, "db") == 0 ) {
                                printf("msg\tdb\t'Hello, World',0xa\n");
                        }
                    else if ( strcmp(token, "equ") == 0 ) {
                                printf("len\tequ $ - msg\n");
                        }
                    else {
                                printf(" \n");                     
                    }
            }
        }
    fclose( fp );
    return 0;
    }


    P.D. I tried to attach here the sample.c program, but is not possible.

    if you could see where is the problem.

    Thank you in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Get rid of this:
    Code:
    token = (char*) malloc (sizeof (50));
    You don't need to malloc as token will be pointing to what strtok returns.

    This:
    Code:
    if ( argc == 2 ) {
        strcpy(myfile, argv[1]);
    }
    else {
        printf("Invalid number or arguments.\n");
    }
    should have been:
    Code:
    if ( argc != 2 ) {
        printf("Invalid number or arguments.\n");
        exit(1);
    }
    strcpy(myfile, argv[1]);
    Otherwise you risk accessing argv[1] even when the command line argument argument was not provided (in such a case, most likely argc == 1, in which case argv[1] is a null pointer, yet strcpy does not accept a null pointer argument, resulting in undefined behaviour).

    Change this:
    Code:
    while ( feof(fp) == 0 )
    {
        fgets(myline,100,fp);
    To this:
    Code:
    while ( fgets(myline, 100, fp) )
    {
    It is usually easier to use the result of the read operation to control the loop instead of using feof because feof will only return true after EOF has been reached, but you usually don't know that it has been reached until you attempt a read operation.

    Aside from these, you need to indent your code more consistently.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > root@
    And create yourself a proper user account for your day to day activities.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Segment
    By Raj 89 in forum C Programming
    Replies: 4
    Last Post: 11-22-2012, 02:40 AM
  2. Segment Fault with shared memory set using mmap
    By AKalair in forum C Programming
    Replies: 12
    Last Post: 11-18-2011, 02:20 PM
  3. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  4. insert linked list - segment violation
    By fractal in forum C Programming
    Replies: 4
    Last Post: 04-05-2008, 07:50 PM
  5. Destroy a shared memory segment
    By g_p in forum C Programming
    Replies: 4
    Last Post: 12-03-2006, 12:30 PM

Tags for this Thread