Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 44aca8a

Browse files
committed
C++: Prepare BufferWrite.qll for autoformat
The autoformatter cannot process these long end-of-line comments properly when the line starts with `or`.
1 parent 29c8353 commit 44aca8a

1 file changed

Lines changed: 117 additions & 62 deletions

File tree

cpp/ql/src/semmle/code/cpp/security/BufferWrite.qll

Lines changed: 117 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -109,26 +109,43 @@ class StrCopyBW extends BufferWriteCall
109109
{
110110
StrCopyBW()
111111
{
112-
exists(TopLevelFunction fn, string name | (fn = getTarget()) and (name = fn.getName()) and (
113-
(name = "strcpy") // strcpy(dst, src)
114-
or (name = "wcscpy") // wcscpy(dst, src)
115-
or (name = "_mbscpy") // _mbscpy(dst, src)
116-
or (
117-
(
118-
name = "strcpy_s" or // strcpy_s(dst, max_amount, src)
119-
name = "wcscpy_s" or // wcscpy_s(dst, max_amount, src)
120-
name = "_mbscpy_s" // _mbscpy_s(dst, max_amount, src)
121-
) and
122-
fn.getNumberOfParameters() = 3 // exclude the 2-parameter template versions
123-
// that find the size of a fixed size destination buffer.
124-
)
125-
or (name = "strncpy") // strncpy(dst, src, max_amount)
126-
or (name = "strncpy_l") // strncpy_l(dst, src, max_amount, locale)
127-
or (name = "wcsncpy") // wcsncpy(dst, src, max_amount)
128-
or (name = "_wcsncpy_l") // _wcsncpy_l(dst, src, max_amount, locale)
129-
or (name = "_mbsncpy") // _mbsncpy(dst, src, max_amount)
130-
or (name = "_mbsncpy_l") // _mbsncpy_l(dst, src, max_amount, locale)
131-
))
112+
exists(TopLevelFunction fn, string name | fn = getTarget() and name = fn.getName() |
113+
// strcpy(dst, src)
114+
name = "strcpy"
115+
or
116+
// wcscpy(dst, src)
117+
name = "wcscpy"
118+
or
119+
// _mbscpy(dst, src)
120+
name = "_mbscpy"
121+
or
122+
(
123+
name = "strcpy_s" or // strcpy_s(dst, max_amount, src)
124+
name = "wcscpy_s" or // wcscpy_s(dst, max_amount, src)
125+
name = "_mbscpy_s" // _mbscpy_s(dst, max_amount, src)
126+
) and
127+
// exclude the 2-parameter template versions
128+
// that find the size of a fixed size destination buffer.
129+
fn.getNumberOfParameters() = 3
130+
or
131+
// strncpy(dst, src, max_amount)
132+
name = "strncpy"
133+
or
134+
// strncpy_l(dst, src, max_amount, locale)
135+
name = "strncpy_l"
136+
or
137+
// wcsncpy(dst, src, max_amount)
138+
name = "wcsncpy"
139+
or
140+
// _wcsncpy_l(dst, src, max_amount, locale)
141+
name = "_wcsncpy_l"
142+
or
143+
// _mbsncpy(dst, src, max_amount)
144+
name = "_mbsncpy"
145+
or
146+
// _mbsncpy_l(dst, src, max_amount, locale)
147+
name = "_mbsncpy_l"
148+
)
132149
}
133150

134151
int getParamSize()
@@ -248,19 +265,39 @@ class SprintfBW extends BufferWriteCall
248265
{
249266
SprintfBW()
250267
{
251-
exists(TopLevelFunction fn, string name | (fn = getTarget()) and (name = fn.getName()) and (
252-
// C sprintf variants
253-
(name = "sprintf") // sprintf(dst, format, args...)
254-
or (name = "vsprintf") // vsprintf(dst, format, va_list)
255-
or (name = "wsprintf") // wsprintf(dst, format, args...)
256-
or (name = "vwsprintf") // vwsprintf(dst, format, va_list)
257-
258-
// Microsoft sprintf variants
259-
or (name.regexpMatch("_sprintf_l")) // _sprintf_l(dst, format, locale, args...)
260-
or (name.regexpMatch("_vsprintf_l")) // _vsprintf_l(dst, format, locale, va_list))
261-
or (name.regexpMatch("__swprintf_l")) // __swprintf_l(dst, format, locale, args...)
262-
or (name.regexpMatch("__vswprintf_l")) // __vswprintf_l(dst, format, locale, va_list)
263-
))
268+
exists(TopLevelFunction fn, string name | fn = getTarget() and name = fn.getName() |
269+
/*
270+
* C sprintf variants
271+
*/
272+
273+
// sprintf(dst, format, args...)
274+
name = "sprintf"
275+
or
276+
// vsprintf(dst, format, va_list)
277+
name = "vsprintf"
278+
or
279+
// wsprintf(dst, format, args...)
280+
name = "wsprintf"
281+
or
282+
// vwsprintf(dst, format, va_list)
283+
name = "vwsprintf"
284+
or
285+
/*
286+
* Microsoft sprintf variants
287+
*/
288+
289+
// _sprintf_l(dst, format, locale, args...)
290+
name.regexpMatch("_sprintf_l")
291+
or
292+
// _vsprintf_l(dst, format, locale, va_list))
293+
name.regexpMatch("_vsprintf_l")
294+
or
295+
// __swprintf_l(dst, format, locale, args...)
296+
name.regexpMatch("__swprintf_l")
297+
or
298+
// __vswprintf_l(dst, format, locale, va_list)
299+
name.regexpMatch("__vswprintf_l")
300+
)
264301
}
265302

266303
override Type getBufferType()
@@ -307,24 +344,40 @@ class SnprintfBW extends BufferWriteCall
307344
{
308345
SnprintfBW()
309346
{
310-
exists(TopLevelFunction fn, string name | (fn = getTarget()) and (name = fn.getName()) and (
311-
// C snprintf variants
312-
(name = "snprintf") // snprintf(dst, max_amount, format, args...)
313-
or (name = "vsnprintf") // vsnprintf(dst, max_amount, format, va_list)
314-
or (name = "swprintf") // swprintf(dst, max_amount, format, args...)
315-
or (name = "vswprintf") // vswprintf(dst, max_amount, format, va_list)
316-
317-
// Microsoft snprintf variants
318-
or (name = "sprintf_s") // sprintf_s(dst, max_amount, format, locale, args...)
319-
or (name = "vsprintf_s") // vsprintf_s(dst, max_amount, format, va_list)
320-
or (name = "swprintf_s") // swprintf_s(dst, max_amount, format, args...)
321-
or (name = "vswprintf_s") // vswprintf_s(dst, max_amount, format, va_list)
322-
323-
// Microsoft snprintf variants with '_'
324-
or (
325-
(name.regexpMatch("_v?sn?w?printf(_s)?(_p)?(_l)?"))
326-
and (not this instanceof SprintfBW)
327-
)
347+
exists(TopLevelFunction fn, string name | fn = getTarget() and name = fn.getName() |
348+
/*
349+
* C snprintf variants
350+
*/
351+
352+
// snprintf(dst, max_amount, format, args...)
353+
name = "snprintf"
354+
or
355+
// vsnprintf(dst, max_amount, format, va_list)
356+
name = "vsnprintf"
357+
or
358+
// swprintf(dst, max_amount, format, args...)
359+
name = "swprintf"
360+
or
361+
// vswprintf(dst, max_amount, format, va_list)
362+
name = "vswprintf"
363+
or
364+
/*
365+
* Microsoft snprintf variants
366+
*/
367+
368+
// sprintf_s(dst, max_amount, format, locale, args...)
369+
name = "sprintf_s"
370+
or
371+
// vsprintf_s(dst, max_amount, format, va_list)
372+
name = "vsprintf_s"
373+
or
374+
// swprintf_s(dst, max_amount, format, args...)
375+
name = "swprintf_s"
376+
or
377+
// vswprintf_s(dst, max_amount, format, va_list)
378+
name = "vswprintf_s"
379+
or
380+
// Microsoft snprintf variants with '_':
328381
// _sprintf_s_l(dst, max_amount, format, locale, args...)
329382
// _swprintf_l(dst, max_amount, format, locale, args...)
330383
// _swprintf_s_l(dst, max_amount, format, locale, args...)
@@ -343,7 +396,9 @@ class SnprintfBW extends BufferWriteCall
343396
// _vsnprintf_l(dst, max_amount, format, locale, va_list)
344397
// _vsnwprintf(dst, max_amount, format, va_list)
345398
// _vsnwprintf_l(dst, max_amount, format, locale, va_list)
346-
))
399+
name.regexpMatch("_v?sn?w?printf(_s)?(_p)?(_l)?") and
400+
not this instanceof SprintfBW
401+
)
347402
}
348403

349404
int getParamSize()
@@ -405,11 +460,11 @@ class GetsBW extends BufferWriteCall
405460
{
406461
GetsBW()
407462
{
408-
exists(TopLevelFunction fn, string name | (fn = getTarget()) and (name = fn.getName()) and (
409-
(name = "gets") // gets(dst)
410-
or (name = "fgets") // fgets(dst, max_amount, src_stream)
411-
or (name = "fgetws") // fgetws(dst, max_amount, src_stream)
412-
))
463+
exists(TopLevelFunction fn, string name | fn = getTarget() and name = fn.getName() |
464+
name = "gets" or // gets(dst)
465+
name = "fgets" or // fgets(dst, max_amount, src_stream)
466+
name = "fgetws" // fgetws(dst, max_amount, src_stream)
467+
)
413468
}
414469

415470
int getParamSize()
@@ -428,11 +483,11 @@ class GetsBW extends BufferWriteCall
428483

429484
override Expr getASource()
430485
{
431-
if exists(getArgument(2)) then (
432-
result = getArgument(2)
433-
) else (
434-
result = this // the source is input inside the 'gets' call itself
435-
)
486+
if exists(getArgument(2))
487+
then result = getArgument(2)
488+
else
489+
// the source is input inside the 'gets' call itself
490+
result = this
436491
}
437492

438493
override Expr getDest()

0 commit comments

Comments
 (0)