Bug Summary

File:dmake/dmstring.c
Location:line 186, column 7
Description:Dereference of null pointer (loaded from variable 'dest')

Annotated Source Code

1/* RCS $Id: dmstring.c,v 1.2 2007-01-18 09:29:40 vg Exp $
2--
3-- SYNOPSIS
4-- String handling code
5--
6-- DESCRIPTION
7-- Routines to handle string manipulation. This code is not specific
8-- to dmake and has/and will be used in other programs. The string
9-- "" is considered the NULL string, if NIL(char) is received instead
10-- undefined results may occurr. (In reality NIL(char) is checked for
11-- but in general it is not safe to assume NIL(char) == NULL)
12--
13-- AUTHOR
14-- Dennis Vadura, dvadura@dmake.wticorp.com
15--
16-- WWW
17-- http://dmake.wticorp.com/
18--
19-- COPYRIGHT
20-- Copyright (c) 1996,1997 by WTI Corp. All rights reserved.
21--
22-- This program is NOT free software; you can redistribute it and/or
23-- modify it under the terms of the Software License Agreement Provided
24-- in the file <distribution-root>/readme/license.txt.
25--
26-- LOG
27-- Use cvs log to obtain detailed change logs.
28*/
29
30#include "extern.h"
31
32PUBLIC char *
33DmStrJoin( src, data, n, fr )/*
34===============================
35 Join data to src according to value of n.
36
37 n = -1 - return strcat( src, data )
38 n >= 0 - return strncat( src, data, n )
39
40 FREE original src if fr == TRUE, else leave it alone */
41
42char *src;
43char *data;
44int n;
45int fr;
46{
47 char *t;
48 int l;
49 int flag = FALSE0;
50
51 DB_ENTER( "DmStrJoin" );
52
53 if( src == NIL(char)((char*)((void*)0)) ) { src = ""; flag = TRUE1; }
54 if( data == NIL(char)((char*)((void*)0)) ) data = "";
55 DB_PRINT( "str", ("Joining [%s] [%s] %d", src, data, n) );
56
57 if( n == -1 ) n = strlen( data );
58
59 l = strlen( src ) + n + 1;
60 if( (t = MALLOC( l, char )(char*) malloc((unsigned int)(l)*(size_t)sizeof(char))) == NIL(char)((char*)((void*)0)) ) No_ram();
61
62 strcpy( t, src );
63 if (n) strncat( t, data, n );
64 t[ l-1 ] = '\0';
65
66 if( !flag && fr ) FREE( src )free((char*)(src));
67
68 DB_PRINT( "str", ("Result [%s]", t) );
69 DB_RETURN( t )return (t);
70}
71
72
73
74
75PUBLIC char *
76DmStrAdd( src, data, fr )/*
77===========================
78 append data to src with space in between if src is not NIL(char) or ""
79 and free both src and data if fr == TRUE, otherwise leave them be */
80
81char *src;
82char *data;
83int fr;
84{
85 char *t;
86 int l;
87 int sflag;
88 int dflag;
89
90 DB_ENTER( "DmStrAdd" );
91
92 sflag = dflag = fr;
93
94 if( src == NIL(char)((char*)((void*)0)) ) { src = ""; sflag = FALSE0; }
95 if( data == NIL(char)((char*)((void*)0)) ) { data = ""; dflag = FALSE0; }
96 DB_PRINT( "str", ("Adding [%s] [%s] %d", src, data, fr) );
97
98 l = strlen(src) + strlen(data) + 1;
99 if( *src ) l++;
100
101 if( (t = MALLOC( l, char )(char*) malloc((unsigned int)(l)*(size_t)sizeof(char))) == NIL(char)((char*)((void*)0)) ) No_ram();
102
103 strcpy( t, src );
104
105 if( *data )
106 {
107 if( *src ) strcat( t, " " );
108 strcat( t, data );
109 }
110
111 if( sflag ) FREE( src )free((char*)(src));
112 if( dflag ) FREE( data )free((char*)(data));
113
114 DB_PRINT( "str", ("Result [%s]", t) );
115 DB_RETURN( t )return (t);
116}
117
118
119
120PUBLIC char *
121DmStrApp( src1, src2 )/*
122========================
123 Append two strings together, and return the result with a space between
124 the two strings. FREE the first string if it is not NIL and always
125 leave the second string be. */
126char *src1;
127char *src2;
128{
129 src2 = DmStrAdd( src1, src2, FALSE0 );
130 if( src1 != NIL(char)((char*)((void*)0)) ) FREE( src1 )free((char*)(src1));
131 return( src2 );
132}
133
134
135PUBLIC char *
136DmStrDup( str )/*
137================= Duplicate the contents of a string, by using malloc */
138char *str;
139{
140 char *t;
141
142 if( str == NIL(char)((char*)((void*)0)) ) return( NIL(char)((char*)((void*)0)) );
143
144 if( (t = MALLOC( strlen( str )+1, char )(char*) malloc((unsigned int)(strlen( str )+1)*(size_t)sizeof
(char))
) == NIL(char)((char*)((void*)0)) ) No_ram();
145 strcpy( t, str );
146
147 return( t );
148}
149
150
151
152PUBLIC char *
153DmStrDup2( str )/*
154==================
155 This function is used solely to properly quote command line arguments when
156 they are reinserted int MAKEMACROS so that they can be used further in
157 a processing line. */
158char *str;
159{
160 char *t;
161 size_t size;
162 size_t alloced;
163 char *tmp;
164 char *dest;
165 int seen_equal = 0;
166
167 if(str == NIL(char)((char*)((void*)0))) return(NIL(char)((char*)((void*)0)));
1
Taking false branch
168 size = strlen(str) + 1;
169 alloced = size + 2; /* for two quotes */
170
171 for(tmp = str; *tmp; tmp++)
2
Loop condition is true. Entering loop body
4
Loop condition is false. Execution continues on line 175
172 if(*tmp == '"')
3
Taking false branch
173 alloced++;
174
175 if((t = MALLOC(alloced, char)(char*) malloc((unsigned int)(alloced)*(size_t)sizeof(char))) == NIL(char)((char*)((void*)0))) No_ram();
5
Taking true branch
176
177 for(tmp = str, dest = t; *tmp; tmp++, dest++) {
6
Null pointer value stored to 'dest'
7
Loop condition is true. Entering loop body
178 if(*tmp == '=' && !seen_equal) {
179 seen_equal = 1;
180 *dest++ = *tmp;
181 *dest = '"';
182 continue;
183 }
184 if(*tmp == '"')
8
Taking false branch
185 *dest++ = '\\';
186 *dest = *tmp;
9
Dereference of null pointer (loaded from variable 'dest')
187 }
188
189 if(!seen_equal)
190 Fatal("DmStrDup2 invoked without argument of form x=y\n");
191
192 *dest++ = '"';
193 *dest = 0;
194
195 return t;
196}
197
198
199
200PUBLIC char *
201DmStrPbrk( s1, s2 )/*
202====================
203 find first occurrence of char in s2 in string s1.
204 Returns a pointer to the first occurrence. NOTE '\0' is considered part
205 of s2 and a pointer to it is returned if no other chars match. */
206
207char *s1;
208char *s2;
209{
210 register char *t;
211
212 if( s1 == NIL(char)((char*)((void*)0)) || s2 == NIL(char)((char*)((void*)0)) ) return( "" );
213
214 for( t=s1; *t && (strchr( s2, *t ) == NIL(char)((char*)((void*)0))); t++ );
215 return( t );
216}
217
218
219
220
221PUBLIC char *
222DmStrSpn( s1, s2 )/*
223====================
224 return pointer to first char in s1 that does not belong to s2.
225 Returns the pointer if match found, else returns pointer to null char
226 in s1. (ie. "" ) */
227
228char *s1;
229char *s2;
230{
231 register char *t;
232
233 if( s1 == NIL(char)((char*)((void*)0)) || s2 == NIL(char)((char*)((void*)0)) ) return( "" );
234
235 for( t=s1; *t && (strchr( s2, *t ) != NIL(char)((char*)((void*)0))); t++ );
236 return( t );
237}
238
239
240
241
242PUBLIC char *
243DmStrStr( s1, s2 )/*
244==================== find first occurrence in s1 of s2 */
245char *s1;
246char *s2;
247{
248 register char *s;
249 register char *p;
250 register char *r;
251
252 if( s1 != NIL(char)((char*)((void*)0)) && s2 != NIL(char)((char*)((void*)0)) )
253 for( s=s1; *s; s++ )
254 if( *s == *s2 )
255 {
256 for( r=s+1, p = s2+1; *p && (*r == *p); r++, p++ );
257 if( !*p ) return( s );
258 }
259
260 return( NIL(char)((char*)((void*)0)) );
261}
262
263
264
265PUBLIC char *
266DmSubStr( s, e )/*
267==================
268 Return the string between the two pointers s and e, not including the
269 char that e points to. NOTE: This routine assumes that s and e point
270 into the same string. */
271
272char *s;
273char *e;
274{
275 char save;
276 int len = e-s;
277
278 if( len < 0 || len > strlen(s) )
279 Fatal( "Internal Error: SubStr fails consistency test" );
280
281 save = *e;
282 *e = '\0';
283 s = DmStrDup( s );
284 *e = save;
285
286 return( s );
287}
288
289
290/* Provide "missing" string function. */
291#ifndef HAVE_STRLWR
292char *
293strlwr(char *s)
294{
295 char *p;
296 for(p=s; *p; p++ )
297 *p = tolower(*p);
298 return s;
299}
300#endif