Coverity: fileio module memory errors fixed.
[scilab.git] / scilab / modules / fileio / src / c / readline.c
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) INRIA
4 * Copyright (C) DIGITEO - 2010 - Allan CORNET
5 *
6  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
8  * This file is hereby licensed under the terms of the GNU GPL v2.0,
9  * pursuant to article 5.3.4 of the CeCILL v.2.1.
10  * This file was originally licensed under the terms of the CeCILL v2.1,
11  * and continues to be available under such terms.
12  * For more information, see the COPYING file which you should have received
13  * along with this program.
14 *
15 */
16 /*--------------------------------------------------------------------------*/
17 #include <stdio.h>
18 #include <string.h>
19 #include "readline.h"
20 #include "mgetl.h"
21 #include "freeArrayOfString.h"
22 #include "sci_malloc.h"
23 /*--------------------------------------------------------------------------*/
24 #define EMPTYSTR ""
25 /*--------------------------------------------------------------------------*/
26 int LineRead(int fd, char buf[], int n, int *cnt, int *nr)
27 {
28     int returnedInfo = READNEXTLINE_ERROR_ERROR_UNMANAGED;
29     int nbLinesToRead = 1;
30     int nbLinesReaded = 0;
31     int mgetIerr = MGETL_ERROR;
32
33     wchar_t **lines = mgetl(fd, nbLinesToRead, &nbLinesReaded, &mgetIerr);
34     char* line = wide_string_to_UTF8(lines[0]);
35     freeArrayOfWideString(lines, nbLinesReaded);
36
37     *cnt = 0;
38     *nr = 0;
39
40     memset(buf, 0, n);
41     strcpy(buf, EMPTYSTR);
42
43     switch (mgetIerr)
44     {
45         case MGETL_NO_ERROR:
46         {
47             if (line && nbLinesReaded == 1)
48             {
49                 /* current limitation (bsiz) of line readed by scilab */
50                 if ((int)strlen(line) < bsiz)
51                 {
52                     strcpy(buf, line);
53                     returnedInfo = READNEXTLINE_ERROR_EOL;
54                 }
55                 else
56                 {
57                     strncpy(buf, line, bsiz);
58                     returnedInfo = READNEXTLINE_ERROR_BUFFER_FULL;
59                 }
60             }
61             else
62             {
63                 returnedInfo = READNEXTLINE_ERROR_EOF_REACHED;
64             }
65         }
66         break;
67
68         case MGETL_EOF:
69         {
70             if (line)
71             {
72                 if (nbLinesReaded == 0)
73                 {
74                     returnedInfo = READNEXTLINE_ERROR_EOF_REACHED;
75                 }
76                 else
77                 {
78                     /* current limitation (bsiz) of line readed by scilab */
79                     if ((int)strlen(line) >= bsiz)
80                     {
81                         strcpy(buf, line);
82                         returnedInfo = READNEXTLINE_ERROR_EOF_REACHED_AFTER_EOL;
83                     }
84                     else
85                     {
86                         strncpy(buf, line, bsiz);
87                         returnedInfo = READNEXTLINE_ERROR_BUFFER_FULL;
88                     }
89                 }
90             }
91             else
92             {
93                 returnedInfo = READNEXTLINE_ERROR_EOF_REACHED_BEFORE_EOL;
94             }
95         }
96         break;
97
98         case MGETL_MEMORY_ALLOCATION_ERROR:
99         case MGETL_ERROR:
100         default:
101         {
102             returnedInfo = READNEXTLINE_ERROR_ERROR_UNMANAGED;
103         }
104         break;
105     }
106
107     *cnt = (int)strlen(buf) + 1;
108     *nr = *cnt;
109
110     if (line)
111     {
112         FREE(line);
113     }
114
115     return returnedInfo;
116 }
117 /*--------------------------------------------------------------------------*/
118 void C2F(readnextline)(int *fd, char buf[], int *n, int *count, int *nr, int *ierr)
119 {
120     *ierr = LineRead(*fd, buf, *n, count, nr);
121 }
122 /*--------------------------------------------------------------------------*/