From 0a141eca6f655e73f01b16f7772e5d91f57b28ca Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 12 Jan 2013 10:56:45 +0100 Subject: [PATCH] Remove memory leaks Change-Id: Ib919c323a09329c77b027b1699116fa41225a7bc --- scilab/modules/call_scilab/src/c/SendScilabJobs.c | 11 +- scilab/modules/hdf5/src/c/h5_readDataFromFile.c | 3 + .../localization/src/c/InitializeLocalization.c | 178 ++++++++++++-------- scilab/modules/output_stream/src/c/sciprint_full.c | 119 +++++++------ scilab/modules/scicos/src/c/scicos.c | 11 +- 5 files changed, 196 insertions(+), 126 deletions(-) diff --git a/scilab/modules/call_scilab/src/c/SendScilabJobs.c b/scilab/modules/call_scilab/src/c/SendScilabJobs.c index 292308f..9c5c4b5 100644 --- a/scilab/modules/call_scilab/src/c/SendScilabJobs.c +++ b/scilab/modules/call_scilab/src/c/SendScilabJobs.c @@ -2,11 +2,11 @@ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab * Copyright (C) 2007 - INRIA - Allan CORNET * Copyright (C) 2009-2010 - DIGITEO - Allan CORNET -* +* * This file must be used under the terms of the CeCILL. * This source file is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms -* are also available at +* are also available at * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt * */ @@ -175,7 +175,9 @@ BOOL GetLastJob(char *JOB, int nbcharsJOB) strcpy(JOB, lastjob); } else + { strncpy(JOB, lastjob, nbcharsJOB); + } return TRUE; } return FALSE; @@ -221,6 +223,7 @@ int SendScilabJobs(char **jobs, int numberjobs) else { fprintf(stderr, "Error: SendScilabJobs (2) 'jobs[%d] == NULL'.\n", i); + FREE(LOCALJOBS); return retcode; } } @@ -320,7 +323,9 @@ static BOOL RemoveCharsFromEOL(char *line, char CharToRemove) bOK = TRUE; } else + { break; + } } return bOK; } @@ -349,7 +354,9 @@ static BOOL RemoveComments(char *line) } if (idx >= 0) + { line[idx] = '\0'; + } return FALSE; } diff --git a/scilab/modules/hdf5/src/c/h5_readDataFromFile.c b/scilab/modules/hdf5/src/c/h5_readDataFromFile.c index b17cfd1..43b4a74 100644 --- a/scilab/modules/hdf5/src/c/h5_readDataFromFile.c +++ b/scilab/modules/hdf5/src/c/h5_readDataFromFile.c @@ -158,6 +158,7 @@ static char* readAttribute(int _iDatasetId, const char *_pstName) status = H5Tset_size(memtype, iDim); if (status < 0) { + FREE(pstValue); return NULL; } @@ -273,6 +274,7 @@ int getDatasetInfo(int _iDatasetId, int* _iComplex, int* _iDims, int* _piDims) hsize_t* dims = (hsize_t*)MALLOC(sizeof(hsize_t) * *_iDims); if (H5Sget_simple_extent_dims(space, dims, NULL) < 0) { + FREE(dims); return -1; } @@ -507,6 +509,7 @@ int readDoubleComplexMatrix(int _iDatasetId, double *_pdblReal, double *_pdblImg status = H5Dread(_iDatasetId, compoundId, H5S_ALL, H5S_ALL, H5P_DEFAULT, pData); if (status < 0) { + FREE(pData); return -1; } diff --git a/scilab/modules/localization/src/c/InitializeLocalization.c b/scilab/modules/localization/src/c/InitializeLocalization.c index 52f6d9d..daa0ef9 100644 --- a/scilab/modules/localization/src/c/InitializeLocalization.c +++ b/scilab/modules/localization/src/c/InitializeLocalization.c @@ -1,11 +1,11 @@ /* * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab * Copyright (C) 2008 - INRIA - Sylvestre LEDRU - * + * * This file must be used under the terms of the CeCILL. * This source file is licensed as described in the file COPYING, which * you should have received as part of this distribution. The terms - * are also available at + * are also available at * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt * */ @@ -14,17 +14,17 @@ #include #include "machine.h" #ifndef _MSC_VER - #ifdef HAVE_LIBINTL_H - #include - #ifdef HAVE_LOCALE_H - #include - #else - #error "Cannot find locale.h despite that libintl.h is available" - #endif - #endif +#ifdef HAVE_LIBINTL_H +#include +#ifdef HAVE_LOCALE_H +#include +#else +#error "Cannot find locale.h despite that libintl.h is available" +#endif +#endif #else - #include - #include +#include +#include #endif #include "MALLOC.h" @@ -40,78 +40,120 @@ #include "LanguagePreferences_Windows.h" #endif -/*--------------------------------------------------------------------------*/ +/*--------------------------------------------------------------------------*/ BOOL InitializeLocalization(void) { #ifdef HAVE_LIBINTL_H - char *SCIpath = getSCIpath(); - char *pathLocales = NULL, *previousPathLocales = NULL; - char *ret = NULL; + char *SCIpath = getSCIpath(); + char *pathLocales = NULL, *previousPathLocales = NULL; + char *ret = NULL; + + /* set directory containing message catalogs */ + pathLocales = (char *)MALLOC(sizeof(char) * (strlen(SCIpath) + strlen(PATHLOCALIZATIONFILE) + 1)); - /* set directory containing message catalogs */ - pathLocales=(char *)MALLOC(sizeof(char)*(strlen(SCIpath)+strlen(PATHLOCALIZATIONFILE)+1)); + strcpy(pathLocales, SCIpath); + strcat(pathLocales, PATHLOCALIZATIONFILE); - strcpy(pathLocales, SCIpath); - strcat(pathLocales, PATHLOCALIZATIONFILE); + if (bindtextdomain(NAMELOCALIZATIONDOMAIN, pathLocales) == NULL || !isdir(pathLocales)) + { + /* source tree and classic build */ + previousPathLocales = strdup(pathLocales); + if (pathLocales) + { + FREE(pathLocales); + pathLocales = NULL; + } - if (bindtextdomain(NAMELOCALIZATIONDOMAIN,pathLocales)==NULL || !isdir(pathLocales)) - { - /* source tree and classic build */ - previousPathLocales = strdup(pathLocales); - if (pathLocales) {FREE(pathLocales); pathLocales = NULL;} + pathLocales = (char *)MALLOC(sizeof(char) * (strlen(SCIpath) + strlen("/..") + strlen(PATHLOCALIZATIONFILE) + 1)); + strcpy(pathLocales, SCIpath); + strcat(pathLocales, "/.."); + strcat(pathLocales, PATHLOCALIZATIONFILE); + if (bindtextdomain(NAMELOCALIZATIONDOMAIN, pathLocales) == NULL || !isdir(pathLocales)) + { + /* when it is installed on the system for example /usr/share/locale/ */ + fprintf(stderr, "Warning: Localization issue: Error while binding the domain from %s or %s: Switch to the default language (English).\n", pathLocales, previousPathLocales); + if (previousPathLocales) + { + FREE(previousPathLocales); + previousPathLocales = NULL; + } + if (pathLocales) + { + FREE(pathLocales); + pathLocales = NULL; + } + if (SCIpath) + { + FREE(SCIpath); + SCIpath = NULL; + } + return FALSE; + } + if (previousPathLocales) + { + FREE(previousPathLocales); + previousPathLocales = NULL; + } + if (pathLocales) + { + FREE(pathLocales); + pathLocales = NULL; + } + if (SCIpath) + { + FREE(SCIpath); + SCIpath = NULL; + } - pathLocales=(char *)MALLOC(sizeof(char)*(strlen(SCIpath)+strlen("/..")+strlen(PATHLOCALIZATIONFILE)+1)); - strcpy(pathLocales, SCIpath); - strcat(pathLocales, "/.."); - strcat(pathLocales, PATHLOCALIZATIONFILE); - if (bindtextdomain(NAMELOCALIZATIONDOMAIN,pathLocales)==NULL || !isdir(pathLocales)) - { - /* when it is installed on the system for example /usr/share/locale/ */ - fprintf(stderr, "Warning: Localization issue: Error while binding the domain from %s or %s: Switch to the default language (English).\n", pathLocales, previousPathLocales); - if (previousPathLocales) {FREE(previousPathLocales); previousPathLocales = NULL;} - if (pathLocales) {FREE(pathLocales); pathLocales = NULL;} - if (SCIpath) {FREE(SCIpath); SCIpath = NULL;} - return FALSE; - } - if (previousPathLocales) {FREE(previousPathLocales); previousPathLocales = NULL;} - if (pathLocales) {FREE(pathLocales); pathLocales = NULL;} - if (SCIpath) {FREE(SCIpath); SCIpath = NULL;} - - } + } - /* set domain for future gettext() calls */ - ret=textdomain(NAMELOCALIZATIONDOMAIN); - if (ret==NULL) - { - fprintf(stderr, "Localization: Error while declaring the text domain %s\n", NAMELOCALIZATIONDOMAIN); - return FALSE; - } - bind_textdomain_codeset (NAMELOCALIZATIONDOMAIN,"UTF-8"); /*such that gettext and dgettext return UTF8 string*/ + /* set domain for future gettext() calls */ + ret = textdomain(NAMELOCALIZATIONDOMAIN); + if (ret == NULL) + { + fprintf(stderr, "Localization: Error while declaring the text domain %s\n", NAMELOCALIZATIONDOMAIN); + FREE(pathLocales); + return FALSE; + } + bind_textdomain_codeset (NAMELOCALIZATIONDOMAIN, "UTF-8"); /*such that gettext and dgettext return UTF8 string*/ #ifndef _MSC_VER - /* Here, the "" means that we will try to use the language of the system - * first. If it doesn't work, we switch back to default (English) */ - setlanguage(""); + /* Here, the "" means that we will try to use the language of the system + * first. If it doesn't work, we switch back to default (English) */ + setlanguage(""); #else - /* We look if registry value LANGUAGE exists */ - /* If not exists the "" means that we will try to use the language of the system.*/ - { - char *loadLanguage = getLanguagePreferences(); - setlanguage(loadLanguage); - if (loadLanguage) {FREE(loadLanguage); loadLanguage = NULL;} - } + /* We look if registry value LANGUAGE exists */ + /* If not exists the "" means that we will try to use the language of the system.*/ + { + char *loadLanguage = getLanguagePreferences(); + setlanguage(loadLanguage); + if (loadLanguage) + { + FREE(loadLanguage); + loadLanguage = NULL; + } + } #endif - if (previousPathLocales) FREE(previousPathLocales); - if (pathLocales) FREE(pathLocales); - if (SCIpath) FREE(SCIpath); + if (previousPathLocales) + { + FREE(previousPathLocales); + } + if (pathLocales) + { + FREE(pathLocales); + } + if (SCIpath) + { + FREE(SCIpath); + } - return TRUE; + return TRUE; #else - fprintf(stderr, "Localization: setlocale didn't exist on the computer used to compile Scilab ! This is abnormal ! No localization will be working for this distribution of Scilab.\n"); - return FALSE; + fprintf(stderr, "Localization: setlocale didn't exist on the computer used to compile Scilab ! This is abnormal ! No localization will be working for this distribution of Scilab.\n"); + return FALSE; #endif } -/*--------------------------------------------------------------------------*/ +/*--------------------------------------------------------------------------*/ diff --git a/scilab/modules/output_stream/src/c/sciprint_full.c b/scilab/modules/output_stream/src/c/sciprint_full.c index 5a6d935..e60899a 100644 --- a/scilab/modules/output_stream/src/c/sciprint_full.c +++ b/scilab/modules/output_stream/src/c/sciprint_full.c @@ -34,74 +34,83 @@ /* MAXCHARSSCIPRINT_FULL is for sciprint_full - more than this gets truncated */ #define MAXCHARSSCIPRINT_FULL 5000 /*--------------------------------------------------------------------------*/ -void sciprint_full(char *fmt,...) +void sciprint_full(char *fmt, ...) { - int lstr; - va_list ap; - char *s_buf=NULL; - char *split_s_buf=NULL; - int count=0; - int p_s=0; - static int colwidth; + int lstr; + va_list ap; + char *s_buf = NULL; + char *split_s_buf = NULL; + int count = 0; + int p_s = 0; + static int colwidth; - s_buf=MALLOC(sizeof(char)*(MAXCHARSSCIPRINT_FULL+1)); - if (s_buf == (char *) 0) - { - sciprint(_("%s: No more memory.\n"),"sciprint_full"); - return; - } + s_buf = MALLOC(sizeof(char) * (MAXCHARSSCIPRINT_FULL + 1)); + if (s_buf == (char *) 0) + { + sciprint(_("%s: No more memory.\n"), "sciprint_full"); + return; + } - /* number of columns as set by command lines() */ - colwidth = getColumnsSize(); + /* number of columns as set by command lines() */ + colwidth = getColumnsSize(); - split_s_buf=MALLOC(sizeof(char)*(colwidth+1)); - if (split_s_buf == (char *) 0) - { - sciprint(_("%s: No more memory.\n"),"sciprint_full"); - return; - } + split_s_buf = MALLOC(sizeof(char) * (colwidth + 1)); + if (split_s_buf == (char *) 0) + { + sciprint(_("%s: No more memory.\n"), "sciprint_full"); + FREE(s_buf); + return; + } - va_start(ap,fmt); + va_start(ap, fmt); #if defined(linux) || defined(_MSC_VER) - count = vsnprintf (s_buf,MAXCHARSSCIPRINT_FULL-1, fmt, ap ); - if (count == -1) - { - s_buf[MAXCHARSSCIPRINT_FULL-1]='\0'; - } + count = vsnprintf (s_buf, MAXCHARSSCIPRINT_FULL - 1, fmt, ap ); + if (count == -1) + { + s_buf[MAXCHARSSCIPRINT_FULL - 1] = '\0'; + } #else - (void )vsprintf(s_buf, fmt, ap ); + (void )vsprintf(s_buf, fmt, ap ); #endif - va_end(ap); + va_end(ap); - lstr=(int) strlen(s_buf); + lstr = (int) strlen(s_buf); - if (lstr