comparison cinst/main.c @ 60:6acb1dae6185

Use strn functions and improve error handling. Even if we know the strings are NULL terminated we use length terminated string functions after a first strlen this makes it easier to assert that at one point we know the string is terminated and afterwards use the length of that.
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 18 Mar 2014 11:28:02 +0000
parents 3f6378647371
children fb9f78f7ab2f
comparison
equal deleted inserted replaced
59:3f6378647371 60:6acb1dae6185
32 #include <string.h> 32 #include <string.h>
33 #include <assert.h> 33 #include <assert.h>
34 34
35 #include "strhelp.h" 35 #include "strhelp.h"
36 #include "listutil.h" 36 #include "listutil.h"
37 #include "errorcodes.h"
37 38
38 /* @brief Read stdin into data structures. 39 /* @brief Read stdin into data structures.
39 * 40 *
40 * Reads from stdin and sorts the input into the respective 41 * Reads from stdin and sorts the input into the respective
41 * variables. The pointers returned need to be freed by the caller. 42 * variables. The pointers returned need to be freed by the caller.
42 * Terminates in OOM conditions. 43 * Terminates in OOM conditions.
43 * 44 *
44 * @returns: 0 on success. -1 otherwise. 45 * The caller needs to free the memory allocated by this function
46 * even when an error is returned.
47 *
48 * @returns: 0 on success. An error code otherwise.
45 */ 49 */
46 void readInput(char **certificateList, char ***to_install, 50 int readInput(char **certificateList, char ***to_install,
47 char ***to_remove) 51 char ***to_remove)
48 { 52 {
49 int lines_read = 0; 53 int lines_read = 0;
50 int readingList = 0; 54 int readingList = 0;
51 char buf[MAX_LINE_LENGTH + 1]; 55 char buf[MAX_LINE_LENGTH + 1];
52 56
53 while (fgets(buf, MAX_LINE_LENGTH + 1, stdin)) { 57 while (fgets(buf, MAX_LINE_LENGTH + 1, stdin)) {
58 size_t len = strlen(buf); /* fgets ensures buf is terminated */
54 if (lines_read ++ > MAX_LINES) { 59 if (lines_read ++ > MAX_LINES) {
55 printf("Too many lines\n"); 60 printf("Too many lines\n");
61 return ERR_TOO_MUCH_INPUT;
56 } 62 }
57 if (strcmp("-----BEGIN CERTIFICATE LIST-----", buf) == 0){ 63 if (strcmp("-----BEGIN CERTIFICATE LIST-----", buf) == 0){
58 readingList = 1; 64 readingList = 1;
59 continue; 65 continue;
60 } 66 }
61 if (strcmp("-----END CERTIFICATE LIST-----", buf) == 0){ 67 if (strcmp("-----END CERTIFICATE LIST-----", buf) == 0){
62 readingList = 0; 68 readingList = 0;
63 continue; 69 continue;
64 } 70 }
65 if (readingList) { 71 if (readingList) {
66 str_append_str(certificateList, buf); 72 str_append_str(certificateList, buf, len);
67 continue; 73 continue;
68 } 74 }
69 if (*buf == 'I') { 75 if (*buf == 'I') {
70 array_append_str(to_install, buf+2); 76 array_append_str(to_install, buf+2, len - 2);
71 continue; 77 continue;
72 } 78 }
73 if (*buf == 'R') { 79 if (*buf == 'R') {
74 array_append_str(to_remove, buf+2); 80 array_append_str(to_remove, buf+2, len - 2);
75 continue; 81 continue;
76 } 82 }
77 if (strcmp("UNINSTALL", buf) == 0) { 83 if (strcmp("UNINSTALL", buf) == 0) {
78 array_append_str(to_remove, buf); 84 array_append_str(to_remove, buf, len - 2);
79 } 85 }
80 } 86 }
81 87
82 return; 88 return 0;
83 } 89 }
84 90
85 int main() { 91 int main() {
86 92
87 char **to_install = NULL; 93 char **to_install = NULL;
88 char **to_remove = NULL; 94 char **to_remove = NULL;
89 char *certificateList = NULL; 95 char *certificateList = NULL;
90 int ret = -1; 96 int ret = -1;
91 97
92 readInput(&certificateList, &to_install, &to_remove); 98 ret = readInput(&certificateList, &to_install, &to_remove);
99
100 if (ret != 0) {
101 return ret;
102 }
103
104 if (!certificateList) {
105 return ERR_INVALID_INPUT_NO_LIST;
106 }
93 107
94 ret = verify_list(certificateList, strlen(certificateList)); 108 ret = verify_list(certificateList, strlen(certificateList));
95 109
96 printf ("Verify List returned %i\n", ret); 110 printf ("Verify List returned %i\n", ret);
97 111

http://wald.intevation.org/projects/trustbridge/