Whamcloud - gitweb
ss_create_invocation: fix potential unititalized reference in error path
[tools/e2fsprogs.git] / lib / ss / invocation.c
1 /*
2  * Copyright 1987, 1988 by MIT Student Information Processing Board
3  *
4  * Permission to use, copy, modify, and distribute this software and
5  * its documentation for any purpose is hereby granted, provided that
6  * the names of M.I.T. and the M.I.T. S.I.P.B. not be used in
7  * advertising or publicity pertaining to distribution of the software
8  * without specific, written prior permission.  M.I.T. and the
9  * M.I.T. S.I.P.B. make no representations about the suitability of
10  * this software for any purpose.  It is provided "as is" without
11  * express or implied warranty.
12  */
13
14 #include "config.h"
15 #ifdef HAS_STDLIB_H
16 #include <stdlib.h>
17 #endif
18 #include "ss_internal.h"
19 #define size    sizeof(ss_data *)
20 #ifdef HAVE_DLOPEN
21 #include <dlfcn.h>
22 #endif
23 #include <errno.h>
24
25 int ss_create_invocation(const char *subsystem_name, const char *version_string,
26                          void *info_ptr, ss_request_table *request_table_ptr,
27                          int *code_ptr)
28 {
29         int sci_idx;
30         ss_data *new_table = NULL;
31         ss_data **table = NULL;
32         ss_data **realloc_table = NULL;
33
34         *code_ptr = 0;
35         table = _ss_table;
36         new_table = (ss_data *) malloc(sizeof(ss_data));
37         if (!new_table)
38                 goto out;
39         memset(new_table, 0, sizeof(ss_data));
40
41         if (table == (ss_data **) NULL) {
42                 table = (ss_data **) malloc(2 * size);
43                 if (!table)
44                         goto out;
45                 table[0] = table[1] = (ss_data *)NULL;
46         }
47         initialize_ss_error_table ();
48
49         for (sci_idx = 1; table[sci_idx] != (ss_data *)NULL; sci_idx++)
50                 ;
51         realloc_table = (ss_data **) realloc((char *)table,
52                                      ((unsigned)sci_idx+2)*size);
53         if (realloc_table == NULL)
54                 goto out;
55
56         table = realloc_table;
57         table[sci_idx+1] = (ss_data *) NULL;
58         table[sci_idx] = new_table;
59
60         new_table->subsystem_name = subsystem_name;
61         new_table->subsystem_version = version_string;
62         new_table->argv = (char **)NULL;
63         new_table->current_request = (char *)NULL;
64         new_table->info_dirs = (char **)malloc(sizeof(char *));
65         if (!new_table->info_dirs)
66                 goto out;
67
68         *new_table->info_dirs = (char *)NULL;
69         new_table->info_ptr = info_ptr;
70         new_table->prompt = malloc((unsigned)strlen(subsystem_name)+4);
71         if (!new_table->prompt)
72                 goto out;
73
74         strcpy(new_table->prompt, subsystem_name);
75         strcat(new_table->prompt, ":  ");
76 #ifdef silly
77         new_table->abbrev_info = ss_abbrev_initialize("/etc/passwd", code_ptr);
78 #else
79         new_table->abbrev_info = NULL;
80 #endif
81         new_table->flags.escape_disabled = 0;
82         new_table->flags.abbrevs_disabled = 0;
83         new_table->rqt_tables =
84                 (ss_request_table **) calloc(2, sizeof(ss_request_table *));
85         if (!new_table->rqt_tables)
86                 goto out;
87
88         *(new_table->rqt_tables) = request_table_ptr;
89         *(new_table->rqt_tables+1) = (ss_request_table *) NULL;
90
91         new_table->readline_handle = 0;
92         new_table->readline_shutdown = 0;
93         new_table->readline = 0;
94         new_table->add_history = 0;
95         new_table->redisplay = 0;
96         new_table->rl_completion_matches = 0;
97         _ss_table = table;
98 #if defined(HAVE_DLOPEN) && defined(SHARED_ELF_LIB)
99         ss_get_readline(sci_idx);
100 #endif
101         return(sci_idx);
102
103 out:
104         if (new_table) {
105                 free(new_table->prompt);
106                 free(new_table->info_dirs);
107         }
108         free(new_table);
109         free(table);
110         *code_ptr = ENOMEM;
111         return 0;
112
113 }
114
115 void
116 ss_delete_invocation(int sci_idx)
117 {
118         register ss_data *t;
119         int ignored_code;
120
121         t = ss_info(sci_idx);
122         free(t->prompt);
123         free(t->rqt_tables);
124         while(t->info_dirs[0] != (char *)NULL)
125                 ss_delete_info_dir(sci_idx, t->info_dirs[0], &ignored_code);
126         free(t->info_dirs);
127 #if defined(HAVE_DLOPEN) && defined(SHARED_ELF_LIB)
128         if (t->readline_shutdown)
129                 (*t->readline_shutdown)(t);
130 #endif
131         free(t);
132 }