1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
package main
// Checks for patch files.
import (
"path"
"strings"
)
func ChecklinesPatch(lines []*Line) {
if G.opts.Debug {
defer tracecall1(lines[0].Fname)()
}
(&PatchChecker{lines, NewExpecter(lines), false, false}).Check()
}
type PatchChecker struct {
lines []*Line
exp *Expecter
seenDocumentation bool
previousLineEmpty bool
}
const (
rePatchUniFileDel = `^---\s(\S+)(?:\s+(.*))?$`
rePatchUniFileAdd = `^\+\+\+\s(\S+)(?:\s+(.*))?$`
rePatchUniHunk = `^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@(.*)$`
)
func (ck *PatchChecker) Check() {
if G.opts.Debug {
defer tracecall0()()
}
if ck.lines[0].CheckRcsid(``, "") {
ck.exp.Advance()
}
ck.previousLineEmpty = ck.exp.ExpectEmptyLine()
patchedFiles := 0
for !ck.exp.EOF() {
line := ck.exp.CurrentLine()
if ck.exp.AdvanceIfMatches(rePatchUniFileDel) {
if ck.exp.AdvanceIfMatches(rePatchUniFileAdd) {
ck.checkBeginDiff(line, patchedFiles)
ck.checkUnifiedDiff(ck.exp.m[1])
patchedFiles++
continue
}
ck.exp.StepBack()
}
if ck.exp.AdvanceIfMatches(rePatchUniFileAdd) {
patchedFile := ck.exp.m[1]
if ck.exp.AdvanceIfMatches(rePatchUniFileDel) {
ck.checkBeginDiff(line, patchedFiles)
ck.exp.PreviousLine().Warn0("Unified diff headers should be first ---, then +++.")
ck.checkUnifiedDiff(patchedFile)
patchedFiles++
continue
}
ck.exp.StepBack()
}
if ck.exp.AdvanceIfMatches(`^\*\*\*\s(\S+)(.*)$`) {
if ck.exp.AdvanceIfMatches(`^---\s(\S+)(.*)$`) {
ck.checkBeginDiff(line, patchedFiles)
line.Warn0("Please use unified diffs (diff -u) for patches.")
return
}
ck.exp.StepBack()
}
ck.exp.Advance()
ck.previousLineEmpty = line.Text == "" || hasPrefix(line.Text, "diff ") || hasPrefix(line.Text, "=============")
if !ck.previousLineEmpty {
ck.seenDocumentation = true
}
}
if patchedFiles > 1 {
NewLineWhole(ck.lines[0].Fname).Warnf("Contains patches for %d files, should be only one.", patchedFiles)
} else if patchedFiles == 0 {
NewLineWhole(ck.lines[0].Fname).Error0("Contains no patch.")
}
ChecklinesTrailingEmptyLines(ck.lines)
SaveAutofixChanges(ck.lines)
}
// See http://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html
func (ck *PatchChecker) checkUnifiedDiff(patchedFile string) {
if G.opts.Debug {
defer tracecall0()()
}
patchedFileType := guessFileType(ck.exp.CurrentLine(), patchedFile)
if G.opts.Debug {
traceStep("guessFileType(%q) = %s", patchedFile, patchedFileType)
}
hasHunks := false
for ck.exp.AdvanceIfMatches(rePatchUniHunk) {
hasHunks = true
linesToDel := toInt(ck.exp.m[2], 1)
linesToAdd := toInt(ck.exp.m[4], 1)
if G.opts.Debug {
traceStep("hunk -%d +%d", linesToDel, linesToAdd)
}
ck.checktextUniHunkCr()
for linesToDel > 0 || linesToAdd > 0 || hasPrefix(ck.exp.CurrentLine().Text, "\\") {
line := ck.exp.CurrentLine()
ck.exp.Advance()
text := line.Text
switch {
case text == "":
linesToDel--
linesToAdd--
case hasPrefix(text, " "), hasPrefix(text, "\t"):
linesToDel--
linesToAdd--
ck.checklineContext(text[1:], patchedFileType)
case hasPrefix(text, "-"):
linesToDel--
case hasPrefix(text, "+"):
linesToAdd--
ck.checklineAdded(text[1:], patchedFileType)
case hasPrefix(text, "\\"):
// \ No newline at end of file
default:
line.Error0("Invalid line in unified patch hunk")
return
}
}
}
if !hasHunks {
ck.exp.CurrentLine().Error1("No patch hunks for %q.", patchedFile)
}
if !ck.exp.EOF() {
line := ck.exp.CurrentLine()
if line.Text != "" && !matches(line.Text, rePatchUniFileDel) && !hasPrefix(line.Text, "Index:") && !hasPrefix(line.Text, "diff ") {
line.Warn0("Empty line or end of file expected.")
Explain3(
"This empty line makes the end of the patch clearly visible.",
"Otherwise the reader would have to count lines to see where",
"the patch ends.")
}
}
}
func (ck *PatchChecker) checkBeginDiff(line *Line, patchedFiles int) {
if G.opts.Debug {
defer tracecall0()()
}
if !ck.seenDocumentation && patchedFiles == 0 {
line.Error0("Each patch must be documented.")
Explain(
"Pkgsrc tries to have as few patches as possible. Therefore, each",
"patch must document why it is necessary. Typical reasons are",
"portability or security.",
"",
"Patches that are related to a security issue should mention the",
"corresponding CVE identifier.",
"",
"Each patch should be sent to the upstream maintainers of the",
"package, so that they can include it in future versions. After",
"submitting a patch upstream, the corresponding bug report should",
"be mentioned in this file, to prevent duplicate work.")
}
if G.opts.WarnSpace && !ck.previousLineEmpty {
if !line.AutofixInsertBefore("") {
line.Note0("Empty line expected.")
}
}
}
func (ck *PatchChecker) checklineContext(text string, patchedFileType FileType) {
if G.opts.Debug {
defer tracecall2(text, patchedFileType.String())()
}
if G.opts.WarnExtra {
ck.checklineAdded(text, patchedFileType)
} else {
ck.checktextRcsid(text)
}
}
func (ck *PatchChecker) checklineAdded(addedText string, patchedFileType FileType) {
if G.opts.Debug {
defer tracecall2(addedText, patchedFileType.String())()
}
ck.checktextRcsid(addedText)
line := ck.exp.PreviousLine()
switch patchedFileType {
case ftShell, ftIgnore:
break
case ftMakefile:
checklineOtherAbsolutePathname(line, addedText)
case ftSource:
checklineSourceAbsolutePathname(line, addedText)
case ftConfigure:
if hasSuffix(addedText, ": Avoid regenerating within pkgsrc") {
line.Error0("This code must not be included in patches.")
Explain4(
"It is generated automatically by pkgsrc after the patch phase.",
"",
"For more details, look for \"configure-scripts-override\" in",
"mk/configure/gnu-configure.mk.")
}
default:
checklineOtherAbsolutePathname(line, addedText)
}
}
func (ck *PatchChecker) checktextUniHunkCr() {
if G.opts.Debug {
defer tracecall0()()
}
line := ck.exp.PreviousLine()
if hasSuffix(line.Text, "\r") {
if !line.AutofixReplace("\r\n", "\n") {
line.Error0("The hunk header must not end with a CR character.")
Explain1(
"The MacOS X patch utility cannot handle these.")
}
}
}
func (ck *PatchChecker) checktextRcsid(text string) {
if strings.IndexByte(text, '$') == -1 {
return
}
if m, tagname := match1(text, `\$(Author|Date|Header|Id|Locker|Log|Name|RCSfile|Revision|Source|State|NetBSD)(?::[^\$]*)?\$`); m {
if matches(text, rePatchUniHunk) {
ck.exp.PreviousLine().Warn1("Found RCS tag \"$%s$\". Please remove it.", tagname)
} else {
ck.exp.PreviousLine().Warn1("Found RCS tag \"$%s$\". Please remove it by reducing the number of context lines using pkgdiff or \"diff -U[210]\".", tagname)
}
}
}
type FileType uint8
const (
ftSource FileType = iota
ftShell
ftMakefile
ftText
ftConfigure
ftIgnore
ftUnknown
)
func (ft FileType) String() string {
return [...]string{
"source code",
"shell code",
"Makefile",
"text file",
"configure file",
"ignored",
"unknown",
}[ft]
}
// This is used to select the proper subroutine for detecting absolute pathnames.
func guessFileType(line *Line, fname string) (fileType FileType) {
if G.opts.Debug {
defer tracecall(fname, "=>", &fileType)()
}
basename := path.Base(fname)
basename = strings.TrimSuffix(basename, ".in") // doesn’t influence the content type
ext := strings.ToLower(strings.TrimLeft(path.Ext(basename), "."))
switch {
case matches(basename, `^I?[Mm]akefile|\.ma?k$`):
return ftMakefile
case basename == "configure" || basename == "configure.ac":
return ftConfigure
}
switch ext {
case "m4", "sh":
return ftShell
case "c", "cc", "cpp", "cxx", "el", "h", "hh", "hpp", "l", "pl", "pm", "py", "s", "t", "y":
return ftSource
case "conf", "html", "info", "man", "po", "tex", "texi", "texinfo", "txt", "xml":
return ftText
case "":
return ftUnknown
}
if G.opts.Debug {
traceStep1("Unknown file type for %q", fname)
}
return ftUnknown
}
func checkwordAbsolutePathname(line *Line, word string) {
if G.opts.Debug {
defer tracecall1(word)()
}
switch {
case matches(word, `^/dev/(?:null|tty|zero)$`):
// These are defined by POSIX.
case word == "/bin/sh":
// This is usually correct, although on Solaris, it's pretty feature-crippled.
case matches(word, `^/s\W`):
// Probably a sed(1) command
case matches(word, `^/(?:[a-z]|\$[({])`):
// Absolute paths probably start with a lowercase letter.
line.Warn1("Found absolute pathname: %s", word)
Explain(
"Absolute pathnames are often an indicator for unportable code. As",
"pkgsrc aims to be a portable system, absolute pathnames should be",
"avoided whenever possible.",
"",
"A special variable in this context is ${DESTDIR}, which is used in",
"GNU projects to specify a different directory for installation than",
"what the programs see later when they are executed. Usually it is",
"empty, so if anything after that variable starts with a slash, it is",
"considered an absolute pathname.")
}
}
// Looks for strings like "/dev/cd0" appearing in source code
func checklineSourceAbsolutePathname(line *Line, text string) {
if !strings.ContainsAny(text, "\"'") {
return
}
if matched, before, _, str := match3(text, `^(.*)(["'])(/\w[^"']*)["']`); matched {
if G.opts.Debug {
traceStep2("checklineSourceAbsolutePathname: before=%q, str=%q", before, str)
}
switch {
case matches(before, `[A-Z_]\s*$`):
// ok; C example: const char *echo_cmd = PREFIX "/bin/echo";
case matches(before, `\+\s*$`):
// ok; Python example: libdir = prefix + '/lib'
default:
checkwordAbsolutePathname(line, str)
}
}
}
func checklineOtherAbsolutePathname(line *Line, text string) {
if G.opts.Debug {
defer tracecall1(text)()
}
if hasPrefix(text, "#") && !hasPrefix(text, "#!") {
// Don't warn for absolute pathnames in comments, except for shell interpreters.
} else if m, before, path, _ := match3(text, `^(.*?)((?:/[\w.]+)*/(?:bin|dev|etc|home|lib|mnt|opt|proc|sbin|tmp|usr|var)\b[\w./\-]*)(.*)$`); m {
switch {
case hasSuffix(before, "@"): // Example: @PREFIX@/bin
case matches(before, `[)}]$`) && !matches(before, `DESTDIR[)}]$`): // Example: ${prefix}/bin
case matches(before, `\+\s*["']$`): // Example: prefix + '/lib'
case matches(before, `\$\w$`): // Example: libdir=$prefix/lib
case hasSuffix(before, "."): // Example: ../dir
// XXX new: case matches(before, `s.$`): // Example: sed -e s,/usr,@PREFIX@,
default:
if G.opts.Debug {
traceStep1("before=%q", before)
}
checkwordAbsolutePathname(line, path)
}
}
}
|