Bug Summary

File:obj-scan-build/libps/../../libps/user.c
Location:line 42, column 16
Description:Call to 'malloc' has an allocation size of 0 bytes

Annotated Source Code

1/* The ps_user type, for per-user info.
2
3 Copyright (C) 1995,96,2001 Free Software Foundation, Inc.
4
5 Written by Miles Bader <miles@gnu.org>
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21#include <hurd.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <assert.h>
26
27#include "ps.h"
28#include "common.h"
29
30static error_t
31install_passwd (struct ps_user *u, struct passwd *pw)
32{
33 int needed = 0;
3
Variable 'needed' initialized to 0
34
35#define COUNT(field)if (pw->field != ((void*)0)) (needed += strlen(pw->field
) + 1)
if (pw->field != NULL((void*)0)) (needed += strlen(pw->field) + 1)
36 COUNT (pw_name)if (pw->pw_name != ((void*)0)) (needed += strlen(pw->pw_name
) + 1)
;
37 COUNT (pw_passwd)if (pw->pw_passwd != ((void*)0)) (needed += strlen(pw->
pw_passwd) + 1)
;
38 COUNT (pw_gecos)if (pw->pw_gecos != ((void*)0)) (needed += strlen(pw->pw_gecos
) + 1)
;
39 COUNT (pw_dir)if (pw->pw_dir != ((void*)0)) (needed += strlen(pw->pw_dir
) + 1)
;
40 COUNT (pw_shell)if (pw->pw_shell != ((void*)0)) (needed += strlen(pw->pw_shell
) + 1)
;
41
42 u->storage = malloc (needed);
4
Call to 'malloc' has an allocation size of 0 bytes
43 if (u->storage != NULL((void*)0))
44 {
45 char *p = u->storage;
46
47 /* Copy each string field into storage allocated in the u
48 structure and point the fields at that instead of the static
49 storage that pw currently points to. */
50#define COPY(field)if (pw->field != ((void*)0)) strcpy(p, pw->field), (pw->
field = p), (p += strlen (p) + 1)
\
51if (pw->field != NULL((void*)0)) \
52strcpy(p, pw->field), (pw->field = p), (p += strlen (p) + 1)
53 COPY (pw_name)if (pw->pw_name != ((void*)0)) strcpy(p, pw->pw_name), (
pw->pw_name = p), (p += strlen (p) + 1)
;
54 COPY (pw_passwd)if (pw->pw_passwd != ((void*)0)) strcpy(p, pw->pw_passwd
), (pw->pw_passwd = p), (p += strlen (p) + 1)
;
55 COPY (pw_gecos)if (pw->pw_gecos != ((void*)0)) strcpy(p, pw->pw_gecos)
, (pw->pw_gecos = p), (p += strlen (p) + 1)
;
56 COPY (pw_dir)if (pw->pw_dir != ((void*)0)) strcpy(p, pw->pw_dir), (pw
->pw_dir = p), (p += strlen (p) + 1)
;
57 COPY (pw_shell)if (pw->pw_shell != ((void*)0)) strcpy(p, pw->pw_shell)
, (pw->pw_shell = p), (p += strlen (p) + 1)
;
58
59 u->passwd = *pw;
60
61 return 0;
62 }
63 else
64 return ENOMEM((0x10 << 26) | ((12) & 0x3fff));
65}
66
67/* Create a ps_user for the user referred to by UID, returning it in U.
68 If a memory allocation error occurs, ENOMEM is returned, otherwise 0. */
69error_t
70ps_user_create (uid_t uid, struct ps_user **u)
71{
72 *u = NEW (struct ps_user)((struct ps_user *)malloc(sizeof(struct ps_user)));
73 if (*u == NULL((void*)0))
74 return ENOMEM((0x10 << 26) | ((12) & 0x3fff));
75
76 (*u)->uid = uid;
77 (*u)->passwd_state = PS_USER_PASSWD_PENDING;
78
79 return 0;
80}
81
82/* Create a ps_user for the user referred to by UNAME, returning it in U.
83 If a memory allocation error occurs, ENOMEM is returned. If no such user
84 is known, EINVAL is returned. */
85error_t
86ps_user_uname_create (char *uname, struct ps_user **u)
87{
88 struct passwd *pw = getpwnam (uname);
89 if (pw)
90 return ps_user_passwd_create (pw, u);
91 else
92 return EINVAL((0x10 << 26) | ((22) & 0x3fff));
93}
94
95/* Makes makes a ps_user containing PW (which is copied). */
96error_t
97ps_user_passwd_create (struct passwd *pw, struct ps_user **u)
98{
99 error_t err = 0;
100
101 *u = NEW (struct ps_user)((struct ps_user *)malloc(sizeof(struct ps_user)));
102 if (*u == NULL((void*)0))
1
Taking false branch
103 err = ENOMEM((0x10 << 26) | ((12) & 0x3fff));
104 else
105 {
106 err = install_passwd (*u, pw);
2
Calling 'install_passwd'
107 if (err)
108 FREE (*u)(void)free((void *)*u);
109 else
110 {
111 (*u)->passwd_state = PS_USER_PASSWD_OK;
112 (*u)->uid = pw->pw_uid;
113 }
114 }
115
116 return err;
117}
118
119/* Free U and any resources it consumes. */
120void
121ps_user_free (struct ps_user *u)
122{
123 if (u->passwd_state == PS_USER_PASSWD_OK)
124 free (u->storage);
125 free (u);
126}
127
128/* ---------------------------------------------------------------- */
129
130/* Returns the password file entry (struct passwd, from <pwd.h>) for the user
131 referred to by U, or NULL if it can't be gotten. */
132struct passwd *ps_user_passwd (struct ps_user *u)
133{
134 if (u->passwd_state == PS_USER_PASSWD_OK)
135 return &u->passwd;
136 else if (u->passwd_state == PS_USER_PASSWD_ERROR)
137 return NULL((void*)0);
138 else
139 {
140 struct passwd *pw = getpwuid (u->uid);
141 if (pw != NULL((void*)0) && install_passwd (u, pw) == 0)
142 {
143 u->passwd_state = PS_USER_PASSWD_OK;
144 return &u->passwd;
145 }
146 else
147 {
148 u->passwd_state = PS_USER_PASSWD_ERROR;
149 return NULL((void*)0);
150 }
151 }
152}
153
154/* Returns the user name for the user referred to by U, or NULL if it can't
155 be gotten. */
156char *ps_user_name (struct ps_user *u)
157{
158 struct passwd *pw = ps_user_passwd (u);
159 if (pw)
160 return pw->pw_name;
161 else
162 return NULL((void*)0);
163}