Home | History | Annotate | Line # | Download | only in lint1
      1 /*	$NetBSD: msg_302.c,v 1.8 2023/07/07 19:45:22 rillig Exp $	*/
      2 # 3 "msg_302.c"
      3 
      4 // Test for message: '%s' returns pointer to automatic object [302]
      5 
      6 /* lint1-extra-flags: -X 351 */
      7 
      8 void *
      9 return_arg(int arg)
     10 {
     11 	/* expect+1: warning: 'return_arg' returns pointer to automatic object [302] */
     12 	return &arg;
     13 }
     14 
     15 void *
     16 return_local(void)
     17 {
     18 	int local = 3;
     19 	/* expect+1: warning: 'return_local' returns pointer to automatic object [302] */
     20 	return &local;
     21 }
     22 
     23 void *
     24 return_local_array(int x)
     25 {
     26 	int local[5], *indirect = local;
     27 
     28 	switch (x) {
     29 	case 0:
     30 		/* expect+1: warning: 'return_local_array' returns pointer to automatic object [302] */
     31 		return local;
     32 	case 1:
     33 		/* expect+1: warning: 'return_local_array' returns pointer to automatic object [302] */
     34 		return &local[3];
     35 	case 2:
     36 		/* expect+1: warning: 'return_local_array' returns pointer to automatic object [302] */
     37 		return 5 + local;
     38 	case 3:
     39 		/* expect+1: warning: 'return_local_array' returns pointer to automatic object [302] */
     40 		return local + 5;
     41 	case 4:
     42 		/* XXX: lint only checks '+' but not '-'. */
     43 		return local - -3;
     44 	case 5:
     45 		/* XXX: lint doesn't track this indirection, but Clang-tidy does. */
     46 		return indirect;
     47 	case 6:
     48 		/* expect+1: warning: 'return_local_array' returns pointer to automatic object [302] */
     49 		return (local);
     50 	case 7:
     51 		/* C99 6.5.2.5p6 */
     52 		/* expect+1: warning: 'return_local_array' returns pointer to automatic object [302] */
     53 		return (char[]){"local string"};
     54 	default:
     55 		return "OK";
     56 	}
     57 }
     58 
     59 void *
     60 return_static(void)
     61 {
     62 	static int long_lived = 3;
     63 	return &long_lived;
     64 }
     65