Bug Summary

File:lwan/main.c
Location:line 111, column 9
Description:Potential leak of memory pointed to by 'c.listener'

Annotated Source Code

1/*
2 * lwan - simple web server
3 * Copyright (c) 2012 Leandro A. F. Pereira <leandro@hardinfo.org>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#define _GNU_SOURCE
21#include <getopt.h>
22#include <stdlib.h>
23#include <unistd.h>
24#include <limits.h>
25
26#include "lwan.h"
27#include "lwan-serve-files.h"
28
29enum args {
30 ARGS_FAILED,
31 ARGS_USE_CONFIG,
32 ARGS_SERVE_FILES
33};
34
35static enum args
36parse_args(int argc, char *argv[], lwan_config_t *config, char *root)
37{
38 static const struct option opts[] = {
39 { .name = "root", .has_arg = 1, .val = 'r' },
40 { .name = "listen", .has_arg = 1, .val = 'l' },
41 { .name = "help", .val = 'h' },
42 { }
43 };
44 int c, optidx = 0;
45 enum args result = ARGS_USE_CONFIG;
46
47 while ((c = getopt_long(argc, argv, "hr:l:", opts, &optidx)) != -1) {
48 switch (c) {
49 case 'l':
50 free(config->listener);
51 config->listener = strdup(optarg);
52 result = ARGS_SERVE_FILES;
53 break;
54
55 case 'r':
56 memcpy(root, optarg, strnlen(optarg, PATH_MAX4096 - 1) + 1);
57 result = ARGS_SERVE_FILES;
58 break;
59
60 case 'h':
61 printf("Usage: %s [--root /path/to/root/dir] [--listener addr:port]\n", argv[0]);
62 printf("\t[--config]\n");
63 printf("Serve files through HTTP.\n\n");
64 printf("Defaults to listening on %s, serving from ./wwwroot.\n\n", config->listener);
65 printf("Options:\n");
66 printf("\t-r, --root Path to serve files from (default: ./wwwroot).\n");
67 printf("\t-l, --listener Listener (default: %s).\n", config->listener);
68 printf("\t-h, --help This.\n");
69 printf("\n");
70 printf("Examples:\n");
71 printf(" Serve system-wide documentation: %s -r /usr/share/doc\n", argv[0]);
72 printf(" Serve on a different port: %s -l '*:1337'\n", argv[0]);
73 printf("\n");
74 printf("Report bugs at <https://github.com/lpereira/lwan>.\n");
75 return ARGS_FAILED;
76
77 default:
78 printf("Run %s --help for usage information.\n", argv[0]);
79 return ARGS_FAILED;
80 }
81 }
82
83 return result;
84}
85
86int
87main(int argc, char *argv[])
88{
89 lwan_t l;
90 lwan_config_t c;
91 char root[PATH_MAX4096];
92
93 if (!getcwd(root, PATH_MAX4096))
1
Taking false branch
94 return 1;
95
96 c = *lwan_get_default_config();
97 c.listener = strdup("*:8080");
2
Memory is allocated
98
99 switch (parse_args(argc, argv, &c, root)) {
3
Control jumps to 'case ARGS_USE_CONFIG:' at line 110
100 case ARGS_SERVE_FILES:
101 lwan_status_info("Serving files from %s", root)lwan_status_info_debug("/home/buildbot/lwan-slave/clang-analyze/build/lwan/main.c"
, 101, __FUNCTION__, "Serving files from %s", root)
;
102 lwan_init_with_config(&l, &c);
103
104 const lwan_url_map_t map[] = {
105 { .prefix = "/", SERVE_FILES(root).module = lwan_module_serve_files(), .args = ((struct lwan_serve_files_settings_t
[]) {{ .root_path = root, .index_html = ((void*)0), .serve_precompressed_files
= 1, .directory_list_template = ((void*)0), .auto_index = 1 }
}), .flags = (lwan_handler_flags_t)0
},
106 { }
107 };
108 lwan_set_url_map(&l, map);
109 break;
110 case ARGS_USE_CONFIG:
111 lwan_init(&l);
4
Potential leak of memory pointed to by 'c.listener'
112 break;
113 case ARGS_FAILED:
114 return EXIT_FAILURE1;
115 }
116
117 lwan_main_loop(&l);
118 lwan_shutdown(&l);
119
120 return EXIT_SUCCESS0;
121}