my c program crashes on 16.04, but not on 14.04

Bug #1728330 reported by mike jiang
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
linux-hwe (Ubuntu)
New
Undecided
Unassigned

Bug Description

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#define BUF_ROWS 100
#define BUF_COLS 64
/*@
 * this demo code can run on ubuntu 14.04 (32bit & 64bit) and mac osx (64bit)
 * But NOT on ubuntu 16.04 (64bit) as tested !
 */
#define dbg(...) fprintf(stderr,__VA_ARGS__)
void split_r(char* str, char* delim, char* toks[], size_t* sz)
{
    char* x, *sp;
    int i;
    x = strtok_r(str, delim, &sp);
    i = 0;
    while (x) {
        toks[i] = malloc(strlen(x)+1);
        strcpy(toks[i], x);
        i++;
        x = strtok_r(NULL, delim, &sp);
    }
    *sz = i;
}

int main(int argc, char** argv) {
    int gCnt, done =0;
    char ibuf[4096];

    char delim[] = " ";
    char* toks[8] = {NULL};
    size_t n;
    char** in;
    FILE* fp=fopen(argv[1],"r");
    if(!fp) { fprintf(stderr,"open %s file err. \n",argv[1]); exit(1); }

    while(!done) {
        gCnt=0;
        in = malloc(BUF_ROWS * BUF_COLS * sizeof(char*) );
        while (gCnt < BUF_ROWS*BUF_COLS) {
            if(fgets(ibuf, 4096, fp)) {
                ibuf[strcspn(ibuf, "\n")] = '\0';
                in[gCnt]=malloc(strlen(ibuf)+1);
                strcpy(in[gCnt], ibuf);
                char tm[128];
                strcpy(tm, in[gCnt]);
                dbg("In:'%s'\n", tm);
                split_r(tm, delim, toks, &n);
                if( n != 4) {
                 fprintf(stderr,"Fatal:in parse line '%s' to split n=%ld.\n", tm, n), exit(1);
                }
                gCnt++;
            } else {
                done=1;
                break;
            }

        }
        pid_t pid;
        if((pid=fork()) == 0) {
          // do sth
          exit(0);
        }
        wait(NULL);
        for (int i=0; i<gCnt; i++)
            printf("%s\n",in[i]), fflush(0);
        for(int i=0; i< BUF_ROWS * BUF_COLS; i++) free(in[i]);
        free(in);
 } // end of done
}

This program takes a input file in which each line has 4 tokens (strings). When I run it with a input file that contains 10000 lines, it always chokes at somewhere about 5000 ~ 6000th line in the input file on ubuntu 16.04. But it never chokes on ubuntu 14.04 & macbook pro -10.12.5. the input file contains the data something like that below ( each line has 4 strings only )
1 + 1 = 2
2 * 2 = 4
4 - 1 = 3
6 / 3 = 2
etc...

please let me know if any more information needed via my email
<email address hidden>

Thank you for attentions !!!

Best Regards

MJ

ProblemType: Bug
DistroRelease: Ubuntu 16.04
Package: linux-image-4.10.0-37-generic 4.10.0-37.41~16.04.1
ProcVersionSignature: Ubuntu 4.10.0-37.41~16.04.1-generic 4.10.17
Uname: Linux 4.10.0-37-generic x86_64
ApportVersion: 2.20.1-0ubuntu2.10
Architecture: amd64
CurrentDesktop: GNOME-Flashback:Unity
Date: Sat Oct 28 21:24:13 2017
InstallationDate: Installed on 2016-01-11 (656 days ago)
InstallationMedia: Ubuntu 14.04 LTS "Trusty Tahr" - Release amd64 (20140417)
SourcePackage: linux-hwe
UpgradeStatus: No upgrade log present (probably fresh install)

Revision history for this message
mike jiang (mrj22) wrote :
Revision history for this message
Leonidas S. Barbosa (leosilvab) wrote :

Hello,

It not seems a security issue so I'm changing this to public.
It's probably related with the compile version and ABI between those releases.

information type: Private Security → Public
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.