Skip to content

Commit a03dc57

Browse files
committed
Introduce some usage of the returns_nonnull attribute
This is currently limited to the ksh wrappers for malloc functions and the sh_sub* functions used for creating subshell-local trees.
1 parent 3856035 commit a03dc57

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

src/cmd/ksh93/include/defs.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ extern Sfio_t *sh_sfeval(char*[]);
131131
extern void sh_setmatch(const char*,ptrdiff_t,ptrdiff_t,ssize_t[],int);
132132
extern void sh_scope(struct argnod*, int);
133133
extern Namval_t *sh_scoped(Namval_t*);
134-
extern Dt_t *sh_subtracktree(int);
135-
extern Dt_t *sh_subfuntree(int);
136134
extern void sh_subjobcheck(pid_t);
137135
extern int sh_subsavefd(int);
138136
extern void sh_subtmpfile(void);
@@ -144,6 +142,8 @@ extern void sh_trim(char*);
144142
extern int sh_type(const char*);
145143
extern void sh_unscope(void);
146144
extern void sh_clear_subshell_pwdfd(void);
145+
extern returns_nonnull Dt_t *sh_subtracktree(int);
146+
extern returns_nonnull Dt_t *sh_subfuntree(int);
147147
#if _lib_openat
148148
extern int sh_diropenat(int,const char *);
149149
extern void sh_pwdupdate(int);
@@ -154,12 +154,12 @@ extern void sh_clear_subshell_pwdfd(void);
154154
#endif /* SHOPT_NAMESPACE */
155155

156156
/* malloc related wrappers */
157-
extern void *sh_malloc(size_t size);
158-
extern void *sh_realloc(void *ptr, size_t size);
159-
extern void *sh_calloc(size_t nmemb, size_t size);
160-
extern char *sh_strdup(const char *s);
161-
extern void *sh_memdup(const void *s, size_t n);
162-
extern char *sh_getcwd(void);
157+
extern returns_nonnull void *sh_malloc(size_t size);
158+
extern returns_nonnull void *sh_realloc(void *ptr, size_t size);
159+
extern returns_nonnull void *sh_calloc(size_t nmemb, size_t size);
160+
extern returns_nonnull char *sh_strdup(const char *s);
161+
extern returns_nonnull void *sh_memdup(const void *s, size_t n);
162+
extern char *sh_getcwd(void);
163163
#define new_of(type,x) ((type*)sh_malloc(sizeof(type)+(x)))
164164
#define sh_newof(p,t,n,x) ((p)?(t*)sh_realloc((char*)(p),sizeof(t)*(n)+(x)):(t*)sh_calloc(1,sizeof(t)*(n)+(x)))
165165

src/cmd/ksh93/sh/init.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ static noreturn void *nomemory(size_t s)
231231
* The following are wrapper functions for memory allocation.
232232
* These functions will error out if the allocation fails.
233233
*/
234-
void *sh_malloc(size_t size)
234+
returns_nonnull void *sh_malloc(size_t size)
235235
{
236236
void *cp;
237237
cp = malloc(size);
@@ -240,7 +240,7 @@ void *sh_malloc(size_t size)
240240
return cp;
241241
}
242242

243-
void *sh_realloc(void *ptr, size_t size)
243+
returns_nonnull void *sh_realloc(void *ptr, size_t size)
244244
{
245245
void *cp;
246246
cp = realloc(ptr, size);
@@ -253,7 +253,7 @@ void *sh_realloc(void *ptr, size_t size)
253253
return cp;
254254
}
255255

256-
void *sh_calloc(size_t nmemb, size_t size)
256+
returns_nonnull void *sh_calloc(size_t nmemb, size_t size)
257257
{
258258
void *cp;
259259
cp = calloc(nmemb, size);
@@ -262,7 +262,7 @@ void *sh_calloc(size_t nmemb, size_t size)
262262
return cp;
263263
}
264264

265-
char *sh_strdup(const char *s)
265+
returns_nonnull char *sh_strdup(const char *s)
266266
{
267267
char *dup;
268268
dup = strdup(s);
@@ -271,7 +271,7 @@ char *sh_strdup(const char *s)
271271
return dup;
272272
}
273273

274-
void *sh_memdup(const void *s, size_t n)
274+
returns_nonnull void *sh_memdup(const void *s, size_t n)
275275
{
276276
void *dup;
277277
dup = memdup(s, n);

src/cmd/ksh93/sh/subshell.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ static void nv_restore(struct subshell *sp)
397397
* Return pointer to tracked alias tree (a.k.a. hash table, i.e. cached $PATH search results).
398398
* Create new one if in a subshell and one doesn't exist and 'create' is non-zero.
399399
*/
400-
Dt_t *sh_subtracktree(int create)
400+
returns_nonnull Dt_t *sh_subtracktree(int create)
401401
{
402402
struct subshell *sp = subshell_data;
403403
if(create && sh.subshell && !sh.subshare)
@@ -416,7 +416,7 @@ Dt_t *sh_subtracktree(int create)
416416
* return pointer to function tree
417417
* create new one if in a subshell and one doesn't exist and create is non-zero
418418
*/
419-
Dt_t *sh_subfuntree(int create)
419+
returns_nonnull Dt_t *sh_subfuntree(int create)
420420
{
421421
struct subshell *sp = subshell_data;
422422
if(create && sh.subshell && !sh.subshare)

src/lib/libast/features/common

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ std noreturn note{ noreturn ok }end compile{
99
int main(void) { foo(); }
1010
}end
1111

12+
tst returns_nonnull - -Werror=attributes note{ returns_nonnull ok }end compile{
13+
#include <stdlib.h>
14+
__attribute__((__returns_nonnull__)) char *foobar(void)
15+
{
16+
return "barfoo";
17+
}
18+
int main(void)
19+
{
20+
if(foobar())
21+
return 0;
22+
return 1;
23+
}
24+
}end
25+
1226
cat{
1327
#if __clang__
1428
#pragma clang diagnostic ignored "-Wmissing-braces"
@@ -76,6 +90,12 @@ cat{
7690
#endif /* _std_noreturn */
7791

7892
#include <stdbool.h>
93+
94+
#if _returns_nonnull
95+
#define returns_nonnull __attribute__((__returns_nonnull__))
96+
#else
97+
#define returns_nonnull
98+
#endif
7999
}end
80100

81101
if tst - note{ <stdarg.h>+<wchar.h> works }end compile{

0 commit comments

Comments
 (0)