@@ -40,9 +40,9 @@ extern void mock_assert(const int result, const char* const expression,
4040 mock_assert((int)(expression), #expression, __FILE__, __LINE__);
4141#endif
4242
43- #define SUBSYSTEM_LEN 257
44- #define CATEGORY_LEN 257
45- #define MESSAGE_LEN 8193
43+ const int subsystem_len = 257 ;
44+ const int category_len = 257 ;
45+ const int message_len = 8193 ;
4646
4747FlogConfigLevel flog_config_parse_level (const char * str );
4848
@@ -60,10 +60,10 @@ static struct poptOption options[] = {
6060struct FlogConfigData {
6161 FlogConfigLevel level ;
6262 FlogConfigMessageType message_type ;
63- char subsystem [SUBSYSTEM_LEN ];
64- char category [CATEGORY_LEN ];
63+ char subsystem [subsystem_len ];
64+ char category [category_len ];
6565 char output_file [PATH_MAX ];
66- char message [MESSAGE_LEN ];
66+ char message [message_len ];
6767 bool version ;
6868 bool help ;
6969};
@@ -83,7 +83,7 @@ flog_config_new(int argc, char *argv[], FlogError *error) {
8383 }
8484
8585 flog_config_set_level (config , LVL_DEFAULT );
86- flog_config_set_message_type (config , Public );
86+ flog_config_set_message_type (config , MSG_PUBLIC );
8787 flog_config_set_version_flag (config , false);
8888 flog_config_set_help_flag (config , false);
8989
@@ -104,7 +104,12 @@ flog_config_new(int argc, char *argv[], FlogError *error) {
104104 poptFreeContext (context );
105105 return config ;
106106 case 'a' :
107- flog_config_set_output_file (config , option_argument );
107+ * error = flog_config_set_output_file (config , option_argument );
108+ if (* error != FLOG_ERROR_NONE ) {
109+ flog_config_free (config );
110+ poptFreeContext (context );
111+ return NULL ;
112+ }
108113 break ;
109114 case 'l' :
110115 flog_config_set_level (config , flog_config_parse_level (option_argument ));
@@ -117,13 +122,12 @@ flog_config_new(int argc, char *argv[], FlogError *error) {
117122 break ;
118123 case 's' :
119124 flog_config_set_subsystem (config , option_argument );
120-
121125 break ;
122126 case 'c' :
123127 flog_config_set_category (config , option_argument );
124128 break ;
125129 case 'p' :
126- flog_config_set_message_type (config , Private );
130+ flog_config_set_message_type (config , MSG_PRIVATE );
127131 break ;
128132 }
129133
@@ -187,8 +191,8 @@ flog_config_set_subsystem(FlogConfig *config, const char *subsystem) {
187191 assert (config != NULL );
188192 assert (subsystem != NULL );
189193
190- if (strlcpy (config -> subsystem , subsystem , SUBSYSTEM_LEN ) >= SUBSYSTEM_LEN ) {
191- fprintf (stderr , "%s: subsystem name truncated to %d bytes\n" , PROGRAM_NAME , SUBSYSTEM_LEN - 1 );
194+ if (strlcpy (config -> subsystem , subsystem , subsystem_len ) >= subsystem_len ) {
195+ fprintf (stderr , "%s: subsystem name truncated to %d bytes\n" , PROGRAM_NAME , subsystem_len - 1 );
192196 }
193197}
194198
@@ -204,8 +208,8 @@ flog_config_set_category(FlogConfig *config, const char *category) {
204208 assert (config != NULL );
205209 assert (category != NULL );
206210
207- if (strlcpy (config -> category , category , CATEGORY_LEN ) >= CATEGORY_LEN ) {
208- fprintf (stderr , "%s: category name truncated to %d bytes\n" , PROGRAM_NAME , CATEGORY_LEN - 1 );
211+ if (strlcpy (config -> category , category , category_len ) >= category_len ) {
212+ fprintf (stderr , "%s: category name truncated to %d bytes\n" , PROGRAM_NAME , category_len - 1 );
209213 }
210214}
211215
@@ -216,15 +220,16 @@ flog_config_get_output_file(const FlogConfig *config) {
216220 return config -> output_file ;
217221}
218222
219- void
223+ FlogError
220224flog_config_set_output_file (FlogConfig * config , const char * output_file ) {
221225 assert (config != NULL );
222226 assert (output_file != NULL );
223227
224228 if (strlcpy (config -> output_file , output_file , PATH_MAX ) >= PATH_MAX ) {
225- fprintf (stderr , "%s: specify an output file path up to a maximum of %d characters\n" , PROGRAM_NAME , PATH_MAX - 1 );
226- exit (EXIT_FAILURE );
229+ return FLOG_ERROR_FILE ;
227230 }
231+
232+ return FLOG_ERROR_NONE ;
228233}
229234
230235FlogConfigLevel
@@ -274,8 +279,8 @@ flog_config_set_message(FlogConfig *config, const char *message) {
274279 assert (config != NULL );
275280 assert (message != NULL );
276281
277- if (strlcpy (config -> message , message , MESSAGE_LEN ) >= MESSAGE_LEN ) {
278- fprintf (stderr , "%s: message string was truncated to %d bytes\n" , PROGRAM_NAME , MESSAGE_LEN - 1 );
282+ if (strlcpy (config -> message , message , message_len ) >= message_len ) {
283+ fprintf (stderr , "%s: message string was truncated to %d bytes\n" , PROGRAM_NAME , message_len - 1 );
279284 }
280285}
281286
@@ -284,14 +289,16 @@ flog_config_set_message_from_args(FlogConfig *config, const char **args) {
284289 assert (config != NULL );
285290 assert (args != NULL );
286291
292+ config -> message [0 ] = '\0' ;
293+
287294 bool message_truncated = false;
288295 while (* args != NULL ) {
289- if (strlcat (config -> message , * args , MESSAGE_LEN ) >= MESSAGE_LEN ) {
296+ if (strlcat (config -> message , * args , message_len ) >= message_len ) {
290297 message_truncated = true;
291298 break ;
292299 }
293300 if (* (args + 1 ) != NULL ) {
294- if (strlcat (config -> message , " " , MESSAGE_LEN ) >= MESSAGE_LEN ) {
301+ if (strlcat (config -> message , " " , message_len ) >= message_len ) {
295302 message_truncated = true;
296303 break ;
297304 }
@@ -300,7 +307,7 @@ flog_config_set_message_from_args(FlogConfig *config, const char **args) {
300307 }
301308
302309 if (message_truncated ) {
303- fprintf (stderr , "%s: message was truncated to %d bytes\n" , PROGRAM_NAME , MESSAGE_LEN - 1 );
310+ fprintf (stderr , "%s: message was truncated to %d bytes\n" , PROGRAM_NAME , message_len - 1 );
304311 }
305312}
306313
@@ -309,8 +316,9 @@ flog_config_set_message_from_stream(FlogConfig *config, FILE *restrict stream) {
309316 assert (config != NULL );
310317 assert (stream != NULL );
311318
312- if (fread (config -> message , sizeof (char ), MESSAGE_LEN , stream ) >= MESSAGE_LEN ) {
313- fprintf (stderr , "%s: message was truncated to %d bytes\n" , PROGRAM_NAME , MESSAGE_LEN - 1 );
319+ if (fread (config -> message , sizeof (char ), message_len , stream ) >= message_len ) {
320+ fprintf (stderr , "%s: message was truncated to %d bytes\n" , PROGRAM_NAME , message_len - 1 );
321+ config -> message [message_len - 1 ] = '\0' ;
314322 }
315323}
316324
0 commit comments