Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from valeriyvan/uninit
Eliminates execution path with using uninitialised pointer. This is a tricky case to parse. The code is correct, but not something the compiler would be able to figure out automatically. I will adjust after pulling to make the token NULL when it is created and then add an explicit check for nullness later on which should make automated code analysis happy. Also note that humlib is tab indented (I will edit to add them after merging). Background: There are two types of line types in HumdrumLine class: lines with "spines" and lines without "spines". Lines with spines contain one or data fields (that are separated by tabs when there is more than one of them). Lines without spines contain only one field, and are not considered data. There are only two possible non-spine line types: those that start with `!!` characters, and also an empty line containing no characters. All other line types are "spined", meaning they may have internal structure, similar to tracks in a MIDI file. The code: ```cpp if (hasSpines()) { line->appendToken(token); } ``` was causing a problem since `token` could theoretically be undefined. However, the previous lines: ```cpp } else if (hasSpines()) { token = new HumdrumToken("55"); empty = "!z"; } ``` will guarantee that `token` is initialized to `55` if such a case were to occur. The code: ```cpp if (recip) { if (isNoteSlice()) { ... } else if (isClefSlice()) { ... } else if (isMeasureSlice()) { ... } else if (isInterpretationSlice()) { ... } else if (isGraceSlice()) { ... } else if (hasSpines()) { token = new HumdrumToken("55"); empty = "!z"; } if (hasSpines()) { line->appendToken(token); } } ``` Will only append the token if the line has spines, and the token is guaranteed to be allocated properly to "55" in the case of a strange problem. The tricky/subtle thing is that all of the other cases in the if-statement are expected spine types. By adding the extra case to initialize token at the end of the first if/else if statement, a memory leak would occur when a non-spined HumdrumLine is processed: The token would be allocated, but since the line is not spined, the token would not be stored and the allocated space would never be freed.
- Loading branch information