๎€ƒ๎€๎€•๎€•๎€‹๎‡‚๎€’๎€ƒ๎‡‚๎ˆ๎ˆ–๎ˆ’
๎ˆ‘๎ˆ“๎‡š๎ˆ๎ˆ’๎‡š๎ˆ‘๎ˆ๎ˆ‘๎ˆ‘
RULES FOR SECURE C LANGUAGE
SOFTWARE DEVELOPMENT
ANSSI GUIDELINES
TARGETED AUDIENCE:
Developers Administrators IT security managers IT managers Users
Information
Warning
๎€–๎‚Œ๎‚๎‚— ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎†ฝ ๎‚›๎‚–๎‚๎‚˜๎‚˜๎‚‰๎‚’ ๎‚†๎‚ ๎€ƒ๎€๎€•๎€•๎€‹๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎€ˆ๎‚–๎‚‰๎‚’๎‚‡๎‚Œ ๎€๎‚…๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎€‹๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎€•๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚ ๎€ƒ๎‚‹๎‚‰๎‚’๎‚‡๎‚๎†ฝ
๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‡Œ๎€”๎‚™๎‚๎‚‰๎‚— ๎‚Š๎‚“๎‚– ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜๎‡๎‡€ ๎€‹๎‚˜ ๎‚๎‚— ๎‚Š๎‚–๎‚‰๎‚‰๎‚๎‚ ๎‚…๎‚š๎‚…๎‚๎‚๎‚…๎‚†๎‚๎‚‰
๎‚…๎‚˜ www.ssi.gouv.fr/en/๎‡€
๎€‹๎‚˜ ๎‚๎‚— ๎‚…๎‚’ ๎‚“๎‚–๎‚๎‚‹๎‚๎‚’๎‚…๎‚ ๎‚‡๎‚–๎‚‰๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚–๎‚“๎‚‘ ๎€ƒ๎€๎€•๎€•๎€‹ ๎‚…๎‚’๎‚ˆ ๎‚๎‚˜ ๎‚๎‚— ๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚ˆ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‡Œ๎€‘๎‚”๎‚‰๎‚’ ๎€Ž๎‚๎‚‡๎‚‰๎‚’๎‚‡๎‚‰ ๎‚š๎ˆ‘๎‡€๎ˆ๎‡ ๎‚”๎‚™๎‚†๎‚๎‚๎‚—๎‚Œ๎‚‰๎‚ˆ
๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎€‡๎‚˜๎‚…๎‚๎‚…๎‚† ๎‚‘๎‚๎‚—๎‚—๎‚๎‚“๎‚’ ๎‡ฌ๎€‡๎€–๎€ƒ๎€Ž๎€ƒ๎€„๎‡ญ๎‡€
๎€ƒ๎‚‡๎‚‡๎‚“๎‚–๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎€‘๎‚”๎‚‰๎‚’ ๎€Ž๎‚๎‚‡๎‚‰๎‚’๎‚‡๎‚‰ ๎‚š๎ˆ‘๎‡€๎ˆ๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚Š๎‚–๎‚‰๎‚‰๎‚๎‚ ๎‚–๎‚‰๎‚™๎‚—๎‚‰๎‚ˆ๎†ฝ ๎‚—๎‚™๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚“ ๎‚‘๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚’๎‚๎‚’๎‚‹ ๎‚๎‚˜๎‚—
๎‚”๎‚…๎‚˜๎‚‰๎‚–๎‚’๎‚๎‚˜๎‚ ๎‡ฆ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚…๎‚˜๎‚‰ ๎‚“๎‚Š ๎‚๎‚…๎‚—๎‚˜ ๎‚™๎‚”๎‚ˆ๎‚…๎‚˜๎‚‰๎‡ง๎‡€ ๎€”๎‚‰๎‚™๎‚—๎‚‰ ๎‚‘๎‚‰๎‚…๎‚’๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚๎‚‹๎‚Œ๎‚˜ ๎‚˜๎‚“ ๎‚‡๎‚“๎‚‘๎‚‘๎‚™๎‚’๎‚๎‚‡๎‚…๎‚˜๎‚‰๎†ฝ ๎‚ˆ๎‚๎‚—๎‚˜๎‚–๎‚๎‚†๎‚™๎‚˜๎‚‰๎†ฝ
๎‚–๎‚‰๎‚ˆ๎‚๎‚—๎‚˜๎‚–๎‚๎‚†๎‚™๎‚˜๎‚‰๎†ฝ ๎‚”๎‚™๎‚†๎‚๎‚๎‚—๎‚Œ๎†ฝ ๎‚˜๎‚–๎‚…๎‚’๎‚—๎‚‘๎‚๎‚˜๎†ฝ ๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎†ฝ ๎‚‡๎‚“๎‚”๎‚๎†ฝ ๎‚…๎‚ˆ๎‚…๎‚”๎‚˜๎†ฝ ๎‚‘๎‚“๎‚ˆ๎‚๎‚Š๎‚๎†ฝ ๎‚‰๎‚œ๎‚˜๎‚–๎‚…๎‚‡๎‚˜๎†ฝ ๎‚˜๎‚–๎‚…๎‚’๎‚—๎‚Š๎‚“๎‚–๎‚‘ ๎‚…๎‚’๎‚ˆ ๎‚™๎‚—๎‚‰๎†ฝ
๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚๎‚’๎‚‹ ๎‚Š๎‚“๎‚– ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚–๎‚‡๎‚๎‚…๎‚ ๎‚”๎‚™๎‚–๎‚”๎‚“๎‚—๎‚‰๎‚—
๎€–๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚”๎‚–๎‚“๎‚š๎‚๎‚ˆ๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚๎‚— ๎‚…๎‚’๎‚ˆ ๎‚…๎‚–๎‚‰ ๎‚–๎‚‰๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚–๎‚‰๎‚…๎‚˜๎‚— ๎‚๎‚’๎‚“๎‚›๎‚’ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚™๎‚†๎‚๎‚๎‚‡๎‚…๎‡‚
๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚๎‚‘๎‚‰๎‡€ ๎€…๎‚“๎‚’๎‚—๎‚๎‚ˆ๎‚‰๎‚–๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚— ๎‚ˆ๎‚๎‚š๎‚‰๎‚–๎‚—๎‚๎‚˜๎‚๎†ฝ ๎€ƒ๎€๎€•๎€•๎€‹ ๎‚‡๎‚…๎‚’๎‚’๎‚“๎‚˜ ๎‚‹๎‚™๎‚…๎‚–๎‚…๎‚’๎‚˜๎‚‰๎‚‰ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜
๎‚…๎‚”๎‚”๎‚๎‚๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚’ ๎‚˜๎‚…๎‚–๎‚‹๎‚‰๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚—๎‡€ ๎€ƒ๎‚”๎‚”๎‚๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‡‚
๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰๎†ฝ ๎‚…๎‚˜ ๎Šฑ๎‚–๎‚—๎‚˜๎†ฝ ๎‚š๎‚…๎‚๎‚๎‚ˆ๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎€‹๎€– ๎‚…๎‚ˆ๎‚‘๎‚๎‚’๎‚๎‚—๎‚˜๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚…๎‚’๎‚ˆ๎‡š๎‚“๎‚– ๎€‹๎€– ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚
๎‚‘๎‚…๎‚’๎‚…๎‚‹๎‚‰๎‚–๎‚—๎‡€
๎€–๎‚Œ๎‚๎‚— ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚— ๎‚… ๎‚‡๎‚“๎‚™๎‚–๎‚˜๎‚‰๎‚—๎‚ ๎‚˜๎‚–๎‚…๎‚’๎‚—๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚ ๎€ˆ๎‚–๎‚‰๎‚’๎‚‡๎‚Œ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜ ๎‡Œ๎€”๎‚ฒ๎‚‹๎‚๎‚‰๎‚— ๎‚ˆ๎‚‰ ๎‚”๎‚–๎‚“๎‡‚
๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚“๎‚™๎‚– ๎‚๎‚‰ ๎‚ˆ๎‚ณ๎‚š๎‚‰๎‚๎‚“๎‚”๎‚”๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚—๎‚ณ๎‚‡๎‚™๎‚–๎‚๎‚—๎‚ณ ๎‚ˆ๎‚‰ ๎‚๎‚“๎‚‹๎‚๎‚‡๎‚๎‚‰๎‚๎‚— ๎‚‰๎‚’ ๎‚๎‚…๎‚’๎‚‹๎‚…๎‚‹๎‚‰ ๎€…๎‡๎†ฝ ๎‚…๎‚š๎‚…๎‚๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚…๎‚˜
www.ssi.gouv.fr๎‡€ ๎€‹๎‚’ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚’๎Šท๎‚๎‚‡๎‚˜๎‚— ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚˜๎‚›๎‚“ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚…๎‚˜๎‚˜๎‚‰๎‚– ๎‚๎‚— ๎‚‡๎‚“๎‚’๎‚—๎‚๎‚ˆ๎‚‰๎‚–๎‚‰๎‚ˆ
๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚’๎‚๎‚ ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰๎‡€
Document changelog:
VERSION DATE CHANGELOG
๎ˆ๎‡€๎ˆ“ ๎ˆ‘๎ˆ“๎‡š๎ˆ๎ˆ’๎‡š๎ˆ‘๎ˆ๎ˆ‘๎ˆ‘ ๎€ˆ๎‚๎‚–๎‚—๎‚˜ ๎€‡๎‚’๎‚‹๎‚๎‚๎‚—๎‚Œ ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 1
Contents
๎ˆ ๎€‹๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ•
๎ˆ‘ ๎€…๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’ ๎ˆ—
๎ˆ’ ๎€—๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚™๎‚’๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‚— ๎ˆ˜
๎ˆ“ ๎€’๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚— ๎ˆ๎ˆ
๎ˆ“๎‡€๎ˆ ๎€‹๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ
๎ˆ“๎‡€๎ˆ‘ ๎€๎‚“๎‚’๎‡‚๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ“
๎ˆ“๎‡€๎ˆ’ ๎€ˆ๎‚“๎‚–๎‚‘๎‚…๎‚˜ ๎‚“๎‚Š ๎‚… ๎Šฑ๎‚๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ”
๎ˆ“๎‡€๎ˆ“ ๎€…๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚†๎‚๎‚“๎‚‡๎‚๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ•
๎ˆ“๎‡€๎ˆ” ๎€—๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎ˆ‡ ๎‚…๎‚’๎‚ˆ ๎ˆ‡๎ˆ‡ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ—
๎ˆ“๎‡€๎ˆ• ๎€•๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‡ ๎‚’๎‚…๎‚‘๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ˜
๎ˆ“๎‡€๎ˆ– ๎€ƒ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚“๎‚˜ ๎‚‰๎‚’๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚—๎‚‰๎‚‘๎‚๎‚‡๎‚“๎‚๎‚“๎‚’ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ‘๎ˆ
๎ˆ“๎‡€๎ˆ— ๎€‰๎‚๎‚š๎‚‰ ๎‚”๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰ ๎‚˜๎‚“ static ๎‚๎‚’๎‚๎‚๎‚’๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚๎‚’ ๎‡Œ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚๎‚”๎‚‰ ๎‡ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ‘๎ˆ
๎ˆ“๎‡€๎ˆ˜ ๎€๎‚™๎‚๎‚˜๎‚๎‡‚๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ‘๎ˆ‘
๎ˆ“๎‡€๎ˆ๎ˆ ๎€ƒ๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚…๎‚’๎‚ˆ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚Š ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ‘๎ˆ’
๎ˆ“๎‡€๎ˆ๎ˆ ๎€—๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ #undef ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ‘๎ˆ”
๎ˆ“๎‡€๎ˆ๎ˆ‘ ๎€–๎‚–๎‚๎‚‹๎‚–๎‚…๎‚”๎‚Œ ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚“๎‚™๎‚†๎‚๎‚‰ ๎‚•๎‚™๎‚‰๎‚—๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚…๎‚–๎‚ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ‘๎ˆ”
๎ˆ” ๎€…๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎ˆ‘๎ˆ–
๎ˆ”๎‡€๎ˆ ๎€๎‚…๎‚—๎‚˜๎‚‰๎‚–๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚Œ๎‚…๎‚—๎‚‰ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ‘๎ˆ–
๎ˆ”๎‡€๎ˆ‘ ๎€…๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚’๎‚“๎‚– ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ‘๎ˆ—
๎ˆ”๎‡€๎ˆ’ ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚ ๎‚Š๎‚‰๎‚…๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚”๎‚–๎‚“๎‚š๎‚๎‚ˆ๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ’๎ˆ
๎ˆ”๎‡€๎ˆ“ Debug ๎‚…๎‚’๎‚ˆ release ๎‚‘๎‚“๎‚ˆ๎‚‰๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ’๎ˆ•
๎ˆ• ๎€†๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎ˆ’๎ˆ—
๎ˆ•๎‡€๎ˆ ๎€๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ’๎ˆ—
๎ˆ•๎‡€๎ˆ‘ ๎€ˆ๎‚–๎‚‰๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ’๎ˆ˜
๎ˆ•๎‡€๎ˆ’ ๎€†๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ“๎ˆ
๎ˆ•๎‡€๎ˆ“ ๎€Ž๎‚๎‚‘๎‚๎‚˜๎‚‰๎‚ˆ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ“๎ˆ’
๎ˆ•๎‡€๎ˆ” ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ static ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ“๎ˆ“
๎ˆ•๎‡€๎ˆ• ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ volatile ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ“๎ˆ”
๎ˆ•๎‡€๎ˆ– ๎€‹๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚˜๎‚๎‚”๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ“๎ˆ•
๎ˆ•๎‡€๎ˆ— Compound literals ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ“๎ˆ–
๎ˆ•๎‡€๎ˆ˜ ๎€‡๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ“๎ˆ—
๎ˆ•๎‡€๎ˆ๎ˆ ๎€‹๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚๎‚’๎‚‹ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚™๎‚—๎‚‰ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ”๎ˆ
๎ˆ•๎‡€๎ˆ๎ˆ ๎€‹๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ”๎ˆ
๎ˆ•๎‡€๎ˆ๎ˆ‘ ๎€๎‚…๎‚’๎‚ˆ๎‚…๎‚˜๎‚“๎‚–๎‚ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ”๎ˆ’
๎ˆ•๎‡€๎ˆ๎ˆ’ ๎€๎‚…๎‚‘๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚Š๎‚“๎‚– ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚… ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ”๎ˆ“
๎ˆ– ๎€–๎‚๎‚”๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎ˆ”๎ˆ–
๎ˆ–๎‡€๎ˆ ๎€‡๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚—๎‚๎‚ž๎‚‰ ๎‚Š๎‚“๎‚– ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚–๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ”๎ˆ–
๎ˆ–๎‡€๎ˆ‘ ๎€–๎‚๎‚”๎‚‰ ๎‚…๎‚๎‚๎‚…๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ”๎ˆ—
๎ˆ–๎‡€๎ˆ’ ๎€–๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ”๎ˆ˜
2 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎ˆ–๎‡€๎ˆ“ ๎€–๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚˜๎‚“ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚“๎‚Š ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ•๎ˆ‘
๎ˆ— ๎€’๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚…๎‚’๎‚ˆ ๎‚…๎‚–๎‚–๎‚…๎‚๎‚— ๎ˆ•๎ˆ“
๎ˆ—๎‡€๎ˆ ๎€•๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‚๎‚—๎‚‰๎‚ˆ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ•๎ˆ“
๎ˆ—๎‡€๎ˆ‘ ๎€๎‚“๎‚’๎‡‚๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎€˜๎€Ž๎€ƒ๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ•๎ˆ•
๎ˆ—๎‡€๎ˆ’ ๎€‡๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚—๎‚๎‚ž๎‚‰ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ•๎ˆ–
๎ˆ—๎‡€๎ˆ“ ๎€•๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚ ๎‚Š๎‚“๎‚– ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚› ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ•๎ˆ—
๎ˆ—๎‡€๎ˆ” ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚ˆ๎‚‰๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰ ๎€๎€—๎€Ž๎€Ž ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ•๎ˆ˜
๎ˆ—๎‡€๎ˆ• ๎€ƒ๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚˜๎‚“ ๎€๎€—๎€Ž๎€Ž ๎‚“๎‚Š ๎‚ˆ๎‚‰๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ–๎ˆ
๎ˆ—๎‡€๎ˆ– ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ restrict ๎‚˜๎‚๎‚”๎‚‰ ๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚– ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ–๎ˆ
๎ˆ—๎‡€๎ˆ— ๎€Ž๎‚๎‚‘๎‚๎‚˜ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚๎‚’๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ–๎ˆ’
๎ˆ—๎‡€๎ˆ˜ ๎€‰๎‚๎‚š๎‚‰ ๎‚”๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‡‚๎Š• ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ–๎ˆ’
๎ˆ—๎‡€๎ˆ๎ˆ ๎€’๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ–๎ˆ“
๎ˆ˜ ๎€•๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚™๎‚’๎‚๎‚“๎‚’๎‚— ๎ˆ–๎ˆ–
๎ˆ˜๎‡€๎ˆ ๎€†๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ–๎ˆ–
๎ˆ˜๎‡€๎ˆ‘ ๎€•๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ–๎ˆ—
๎ˆ˜๎‡€๎ˆ’ ๎‚†๎‚๎‚˜๎‡‚๎Šฑ๎‚‰๎‚๎‚ˆ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ–๎ˆ˜
๎ˆ˜๎‡€๎ˆ“ ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎€ˆ๎€ƒ๎€๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ—๎ˆ
๎ˆ˜๎‡€๎ˆ” ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚™๎‚’๎‚๎‚“๎‚’๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ—๎ˆ
๎ˆ๎ˆ ๎€‡๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎ˆ—๎ˆ‘
๎ˆ๎ˆ๎‡€๎ˆ ๎€‹๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ—๎ˆ‘
๎ˆ๎ˆ๎‡€๎ˆ‘ ๎€”๎‚‰๎‚…๎‚ˆ๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ—๎ˆ“
๎ˆ๎ˆ๎‡€๎ˆ’ ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚— ๎‚˜๎‚“ ๎‚‘๎‚…๎‚๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ—๎ˆ”
๎ˆ๎ˆ๎‡€๎ˆ“ ๎€๎‚“ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’ ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ—๎ˆ•
๎ˆ๎ˆ๎‡€๎ˆ” ๎€’๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚— ๎‚…๎‚–๎‚“๎‚™๎‚’๎‚ˆ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚… ๎‚†๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ—๎ˆ–
๎ˆ๎ˆ๎‡€๎ˆ• ๎€‹๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’ ๎‚›๎‚๎‚˜๎‚Œ ๎ˆ ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ—๎ˆ—
๎ˆ๎ˆ๎‡€๎ˆ– ๎€„๎‚๎‚˜๎‚›๎‚๎‚—๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ˜๎ˆ
๎ˆ๎ˆ๎‡€๎ˆ— ๎€„๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ˜๎ˆ
๎ˆ๎ˆ๎‡€๎ˆ˜ ๎€๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ˜๎ˆ‘
๎ˆ๎ˆ๎‡€๎ˆ๎ˆ ๎€‘๎‚’๎‚๎‚ ๎‚“๎‚’๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚”๎‚‰๎‚– ๎‚๎‚๎‚’๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ˜๎ˆ’
๎ˆ๎ˆ๎‡€๎ˆ๎ˆ ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ˜๎ˆ“
๎ˆ๎ˆ๎‡€๎ˆ๎ˆ‘ ๎€…๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ˜๎ˆ•
๎ˆ๎ˆ ๎€…๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚…๎‚’๎‚ˆ ๎‚๎‚˜๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚š๎‚‰ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎ˆ˜๎ˆ–
๎ˆ๎ˆ๎‡€๎ˆ ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚†๎‚–๎‚…๎‚‡๎‚‰๎‚— ๎‚Š๎‚“๎‚– ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚๎‚— ๎‚…๎‚’๎‚ˆ ๎‚๎‚“๎‚“๎‚”๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ˜๎ˆ–
๎ˆ๎ˆ๎‡€๎ˆ‘ ๎€…๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚—๎‚›๎‚๎‚˜๎‚‡๎‚Œ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ˜๎ˆ—
๎ˆ๎ˆ๎‡€๎ˆ’ ๎€…๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š for ๎‚๎‚“๎‚“๎‚”๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ๎ˆ
๎ˆ๎ˆ๎‡€๎ˆ“ ๎€…๎‚Œ๎‚…๎‚’๎‚‹๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚“๎‚– ๎‚๎‚“๎‚“๎‚” ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚– ๎‚Š๎‚“๎‚–๎‚†๎‚๎‚ˆ๎‚ˆ๎‚‰๎‚’ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚“๎‚ˆ๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚“๎‚“๎‚” ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ๎ˆ‘
๎ˆ๎ˆ‘ ๎€Œ๎‚™๎‚‘๎‚”๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎ˆ๎ˆ๎ˆ“
๎ˆ๎ˆ‘๎‡€๎ˆ ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚†๎‚…๎‚‡๎‚๎‚›๎‚…๎‚–๎‚ˆ goto ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ๎ˆ“
๎ˆ๎ˆ‘๎‡€๎ˆ‘ ๎€Ž๎‚๎‚‘๎‚๎‚˜๎‚‰๎‚ˆ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚Š๎‚“๎‚–๎‚›๎‚…๎‚–๎‚ˆ ๎‚‹๎‚“๎‚˜๎‚“ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ๎ˆ”
๎ˆ๎ˆ’ ๎€ˆ๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ๎ˆ๎ˆ–
๎ˆ๎ˆ’๎‡€๎ˆ ๎€…๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚’๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚’๎‚˜ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ๎ˆ–
๎ˆ๎ˆ’๎‡€๎ˆ‘ ๎€†๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ๎ˆ˜
๎ˆ๎ˆ’๎‡€๎ˆ’ ๎€˜๎‚…๎‚๎‚๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚๎‚’๎‚”๎‚™๎‚˜ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ๎ˆ
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 3
๎ˆ๎ˆ’๎‡€๎ˆ“ ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚– const ๎‚Š๎‚“๎‚– ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡‚๎‚˜๎‚๎‚”๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ๎ˆ
๎ˆ๎ˆ’๎‡€๎ˆ” ๎€—๎‚—๎‚๎‚’๎‚‹ inline ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ๎ˆ‘
๎ˆ๎ˆ’๎‡€๎ˆ• ๎€”๎‚‰๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚’๎‚‹ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ๎ˆ’
๎ˆ๎ˆ’๎‡€๎ˆ– ๎€๎‚…๎‚’๎‚ˆ๎‚…๎‚˜๎‚“๎‚–๎‚ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ๎ˆ’
๎ˆ๎ˆ’๎‡€๎ˆ— ๎€‹๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚’๎‚“๎‚’๎‡‚void ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ๎ˆ“
๎ˆ๎ˆ’๎‡€๎ˆ˜ ๎€๎‚“ ๎‚”๎‚…๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚†๎‚ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚…๎‚— ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ๎ˆ•
๎ˆ๎ˆ’๎‡€๎ˆ๎ˆ ๎€’๎‚…๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚…๎‚— ๎‚… ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚Š๎‚“๎‚– ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ๎ˆ–
๎ˆ๎ˆ’๎‡€๎ˆ๎ˆ ๎€๎‚…๎‚’๎‚ˆ๎‚…๎‚˜๎‚“๎‚–๎‚ ๎‚™๎‚—๎‚‰ ๎‚๎‚’ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚…๎‚๎‚ ๎‚๎‚˜๎‚— ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ๎ˆ—
๎ˆ๎ˆ’๎‡€๎ˆ๎ˆ‘ ๎€˜๎‚…๎‚–๎‚๎‚…๎‚ˆ๎‚๎‚‡ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ๎ˆ˜
๎ˆ๎ˆ“ ๎€•๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎ˆ๎ˆ‘๎ˆ
๎ˆ๎ˆ“๎‡€๎ˆ ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚‘๎‚… ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ‘๎ˆ
๎ˆ๎ˆ“๎‡€๎ˆ‘ ๎€—๎‚—๎‚๎‚’๎‚‹ ๎‚”๎‚–๎‚‰๎‡š๎‚”๎‚“๎‚—๎‚˜๎Šฑ๎‚œ ++ ๎‚…๎‚’๎‚ˆ -- ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚”๎‚“๎‚™๎‚’๎‚ˆ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ‘๎ˆ‘
๎ˆ๎ˆ“๎‡€๎ˆ’ ๎€๎‚“ ๎‚’๎‚‰๎‚—๎‚˜๎‚‰๎‚ˆ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚–๎‚ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‡Œ๎‡‡๎†ฟ ๎‡ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ‘๎ˆ’
๎ˆ๎ˆ” ๎€๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚‘๎‚…๎‚’๎‚…๎‚‹๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎ˆ๎ˆ‘๎ˆ”
๎ˆ๎ˆ”๎‡€๎ˆ ๎€†๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ‘๎ˆ”
๎ˆ๎ˆ”๎‡€๎ˆ‘ ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
sizeof
๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ‘๎ˆ–
๎ˆ๎ˆ”๎‡€๎ˆ’ ๎€๎‚…๎‚’๎‚ˆ๎‚…๎‚˜๎‚“๎‚–๎‚ ๎‚š๎‚‰๎‚–๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚™๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚… ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ‘๎ˆ˜
๎ˆ๎ˆ”๎‡€๎ˆ“ ๎€‹๎‚—๎‚“๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚… ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ’๎ˆ
๎ˆ๎ˆ• ๎€‡๎‚–๎‚–๎‚“๎‚– ๎‚‘๎‚…๎‚’๎‚…๎‚‹๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎ˆ๎ˆ’๎ˆ’
๎ˆ๎ˆ•๎‡€๎ˆ ๎€…๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š errno ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ’๎ˆ’
๎ˆ๎ˆ•๎‡€๎ˆ‘ ๎€•๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚‡๎‚“๎‚’๎‚—๎‚๎‚ˆ๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ’๎ˆ“
๎ˆ๎ˆ•๎‡€๎ˆ’ ๎€†๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ’๎ˆ”
๎ˆ๎ˆ•๎‡€๎ˆ“ ๎€”๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚Š ๎‚… ๎€… ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚’ ๎‚›๎‚Œ๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚๎‚˜ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚‰๎‚ˆ ๎‚—๎‚™๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚Š๎‚™๎‚๎‚๎‚ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ’๎ˆ•
๎ˆ๎ˆ•๎‡€๎ˆ” ๎€‡๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚… ๎€… ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚…๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ’๎ˆ–
๎ˆ๎ˆ– ๎€•๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎ˆ๎ˆ“๎ˆ
๎ˆ๎ˆ–๎‡€๎ˆ ๎€’๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ“๎ˆ
๎ˆ๎ˆ–๎‡€๎ˆ‘ ๎€๎‚“๎‚˜ ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚‰๎‚ˆ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‚‰๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ“๎ˆ
๎ˆ๎ˆ–๎‡€๎ˆ’ ๎€’๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ“๎ˆ
๎ˆ๎ˆ–๎‡€๎ˆ“ ๎€…๎‚Œ๎‚“๎‚๎‚‡๎‚‰ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ“๎ˆ‘
๎ˆ๎ˆ— ๎€ƒ๎‚’๎‚…๎‚๎‚๎‚—๎‚๎‚—๎†ฝ ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎ˆ๎ˆ“๎ˆ“
๎ˆ๎ˆ—๎‡€๎ˆ ๎€’๎‚–๎‚“๎‚“๎‚Š๎‚–๎‚‰๎‚…๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ“๎ˆ“
๎ˆ๎ˆ—๎‡€๎ˆ‘ ๎€‹๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚๎‚“๎‚’๎‚‹ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ“๎ˆ“
๎ˆ๎ˆ—๎‡€๎ˆ’ ๎€‹๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎‚Š๎‚๎‚๎‚’๎‚‹ ๎‚…๎‚’๎‚ˆ ๎‚–๎‚‰๎‚‘๎‚“๎‚š๎‚๎‚’๎‚‹ ๎‚…๎‚’๎‚ ๎‚ˆ๎‚‰๎‚…๎‚ˆ ๎‚“๎‚– ๎‚™๎‚’๎‚–๎‚‰๎‚…๎‚‡๎‚Œ๎‚…๎‚†๎‚๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ“๎ˆ”
๎ˆ๎ˆ—๎‡€๎ˆ“ ๎€–๎‚“๎‚“๎‚๎‡‚๎‚†๎‚…๎‚—๎‚‰๎‚ˆ ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚˜๎‚“ ๎‚๎‚๎‚‘๎‚๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚๎‚—๎‚ ๎‚“๎‚Š ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚๎‚“๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ“๎ˆ•
๎ˆ๎ˆ—๎‡€๎ˆ” ๎€Ž๎‚๎‚‘๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚‡๎‚๎‚‡๎‚๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ๎‚๎‚˜๎‚ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ“๎ˆ•
๎ˆ๎ˆ—๎‡€๎ˆ• ๎€Ž๎‚๎‚‘๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‰๎‚’๎‚‹๎‚˜๎‚Œ ๎‚“๎‚Š ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ“๎ˆ–
๎ˆ๎ˆ—๎‡€๎ˆ– ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎€…๎ŠŠ๎ŠŠ ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ“๎ˆ–
๎ˆ๎ˆ˜ ๎€๎‚๎‚—๎‚‡๎‚‰๎‚๎‚๎‚…๎‚’๎‚‰๎‚“๎‚™๎‚— ๎ˆ๎ˆ“๎ˆ˜
๎ˆ๎ˆ˜๎‡€๎ˆ ๎€…๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜ ๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ“๎ˆ˜
๎ˆ๎ˆ˜๎‡€๎ˆ‘ ๎€‹๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‡Œ๎‚‡๎‚…๎‚’๎‚…๎‚–๎‚ ๎‡ ๎‚‘๎‚‰๎‚‡๎‚Œ๎‚…๎‚’๎‚๎‚—๎‚‘ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ“๎ˆ˜
๎ˆ๎ˆ˜๎‡€๎ˆ’ ๎€ƒ๎‚—๎‚—๎‚‰๎‚–๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚—๎‚—๎‚‰๎‚–๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚–๎‚๎‚˜๎‚ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ”๎ˆ
๎ˆ๎ˆ˜๎‡€๎ˆ“ ๎€Ž๎‚…๎‚—๎‚˜ ๎‚๎‚๎‚’๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚’๎‚“๎‚’๎‡‚๎‚‰๎‚‘๎‚”๎‚˜๎‚ ๎Šฑ๎‚๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚‰๎‚’๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚๎‚๎‚’๎‚‰ ๎‚†๎‚–๎‚‰๎‚…๎‚ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ”๎ˆ
4 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎€ƒ๎‚”๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚œ ๎€ƒ ๎€ƒ๎‚‡๎‚–๎‚“๎‚’๎‚๎‚‘๎‚— ๎ˆ๎ˆ”๎ˆ‘
๎€ƒ๎‚”๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚œ ๎€„ ๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚– ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚’ ๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ๎ˆ”๎ˆ’
๎€„๎‡€๎ˆ ๎€†๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚’ ๎‚™๎‚—๎‚‰ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ”๎ˆ’
๎€„๎‡€๎ˆ‘ ๎€ƒ๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ”๎ˆ’
๎€„๎‡€๎ˆ’ ๎€…๎„‘๎„†๎„“๎„Œ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ -Weverything ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ”๎ˆ•
๎€ƒ๎‚”๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚œ ๎€… ๎€…๎ŠŠ๎ŠŠ ๎‚–๎‚‰๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ ๎‚›๎‚“๎‚–๎‚ˆ๎‚— ๎ˆ๎ˆ”๎ˆ–
๎€ƒ๎‚”๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚œ ๎€† ๎€‘๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚”๎‚–๎‚๎‚“๎‚–๎‚๎‚˜๎‚ ๎ˆ๎ˆ”๎ˆ—
๎€ƒ๎‚”๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚œ ๎€‡ ๎€‡๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚“๎‚Š ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ๎ˆ•๎ˆ
๎€‡๎‡€๎ˆ ๎€ˆ๎‚๎‚๎‚‰๎‚— ๎‚‰๎‚’๎‚‡๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ•๎ˆ
๎€‡๎‡€๎ˆ‘ ๎€…๎‚“๎‚ˆ๎‚‰ ๎‚๎‚…๎‚๎‚“๎‚™๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ•๎ˆ
๎€‡๎‡€๎ˆ’ ๎€•๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ•๎ˆ
๎€‡๎‡€๎ˆ“ ๎€๎‚…๎‚‘๎‚๎‚’๎‚‹ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ•๎ˆ‘
๎€‡๎‡€๎ˆ” ๎€†๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎‡€ ๎ˆ๎ˆ•๎ˆ”
๎€‹๎‚’๎‚ˆ๎‚‰๎‚œ ๎ˆ๎ˆ•๎ˆ—
๎€Ž๎‚๎‚—๎‚˜ ๎‚“๎‚Š ๎‚–๎‚™๎‚๎‚‰๎‚—๎†ฝ ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ ๎‚‹๎‚“๎‚“๎‚ˆ ๎‚”๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚‡๎‚‰๎‚— ๎ˆ๎ˆ–๎ˆ
๎€„๎‚๎‚†๎‚๎‚๎‚“๎‚‹๎‚–๎‚…๎‚”๎‚Œ๎‚ ๎ˆ๎ˆ–๎ˆ•
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 5
1
Introduction
The C language o๎Šฎers great freedom to developers. However, it contains ambigu-
ous or risky constructions that may introduce errors during development. The
C language standard does not specify all the desired behaviours, and therefore
some remain unde๎Šฑned or unspeci๎Šฑed
๎ˆ
. Developers of compilers, libraries or
operating systems are therefore free to make their own choices.
Restrictions must therefore be set on the use of C language in order to identify the
various risky or non-portable constructions and to limit or even prohibit their use.
The restrictions de๎Šฑned in this guide are intended to encourage the production of
more secure, safer, more robust so๎Šนware, and also to encourage their portability
from one system to another, whether PC type or embedded.
๎€–๎‚Œ๎‚๎‚— ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚— ๎‚… ๎‚—๎‚‰๎‚˜ ๎‚“๎‚Š ๎‚–๎‚™๎‚๎‚‰๎‚—๎†ฝ ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ ๎‚‹๎‚“๎‚“๎‚ˆ ๎‚”๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚‡๎‚‰๎‚— ๎‚ˆ๎‚‰๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‡‚
๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜๎‡€ In this document, we currently restrict ourselves to the 2 standards C90
๎ˆ‘
and
C99, which are the most widely used.
๎€™๎‚Œ๎‚‰๎‚’ ๎‚… ๎‚–๎‚™๎‚๎‚‰ ๎‚๎‚— ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚“๎‚’๎‚‰ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‡ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚๎‚— ๎‚‡๎‚๎‚‰๎‚…๎‚–๎‚๎‚ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚…๎‚’๎‚
๎‚‡๎‚“๎‚’๎‚Š๎‚™๎‚—๎‚๎‚“๎‚’๎‡€ ๎€‹๎‚Š ๎‚’๎‚“๎‚˜ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ๎†ฝ ๎‚†๎‚“๎‚˜๎‚Œ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‚— ๎‚…๎‚–๎‚‰ ๎‚‡๎‚“๎‚’๎‚‡๎‚‰๎‚–๎‚’๎‚‰๎‚ˆ๎‡€
Rule
๎€ƒ rule ๎‚‘๎‚™๎‚—๎‚˜ always ๎‚†๎‚‰ ๎‚–๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ๎†พ ๎‚’๎‚“ ๎‚‰๎‚œ๎‚‡๎‚‰๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚˜๎‚“๎‚๎‚‰๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ๎‡€
Recommendation
๎€ƒ recommendation ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚‰๎‚œ๎‚‡๎‚‰๎‚”๎‚˜ ๎‚๎‚’ ๎‚‡๎‚‰๎‚–๎‚˜๎‚…๎‚๎‚’ ๎‚‰๎‚œ๎‚‡๎‚‰๎‚”๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚‡๎‚…๎‚—๎‚‰๎‚—๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ
๎‚๎‚‘๎‚”๎‚๎‚๎‚‰๎‚— ๎‚… ๎‚‡๎‚๎‚‰๎‚…๎‚– ๎‚…๎‚’๎‚ˆ ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚‰ ๎‚Ž๎‚™๎‚—๎‚˜๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–๎‡€ ๎€”๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰
๎‚…๎‚†๎‚†๎‚–๎‚‰๎‚š๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‡Œ๎€”๎€‡๎€…๎€‘๎‡๎‡€
๎€–๎‚Œ๎‚๎‚— ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰ ๎‚…๎‚๎‚—๎‚“ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚— ๎‚‹๎‚“๎‚“๎‚ˆ ๎‚”๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚‡๎‚‰๎‚—๎‡€ ๎€–๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚…๎‚–๎‚‰ ๎‚“๎Šน๎‚‰๎‚’ ๎‚—๎‚“๎‚‘๎‚‰๎‚›๎‚Œ๎‚…๎‚˜ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚—๎‚™๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚— ๎‚๎‚๎‚๎‚‰
๎‚‡๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎‡€
๎ˆ๎‡€ ๎€–๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚‡๎‚“๎‚’๎‚‡๎‚‰๎‚”๎‚˜๎‚— ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚”๎‚…๎‚‹๎‚‰๎ˆ˜๎‡€
๎ˆ‘๎‡€ ๎€๎‡€๎€„๎‡€ ๎‚˜๎‚Œ๎‚‰ ๎€…๎ˆ˜๎ˆ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚— ๎‚…๎‚๎‚—๎‚“ ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚–๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚— ๎€…๎ˆ—๎ˆ˜ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚‡๎‚“๎‚‘๎‚‘๎‚™๎‚’๎‚๎‚˜๎‚๎‡€
6 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
Good practice
๎€–๎‚Œ๎‚‰ good practices ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰ ๎‚…๎‚–๎‚‰ ๎‚Œ๎‚๎‚‹๎‚Œ๎‚๎‚ ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚‰๎‚ˆ๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰
๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚“๎‚—๎‚‰ ๎‚…๎‚๎‚–๎‚‰๎‚…๎‚ˆ๎‚ ๎‚๎‚’ ๎‚”๎‚๎‚…๎‚‡๎‚‰ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–๎‡‹๎‚— ๎‚“๎‚–๎‚‹๎‚…๎‚’๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚– ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜
๎‚˜๎‚‰๎‚…๎‚‘ ๎‚๎‚Š ๎‚‰๎‚•๎‚™๎‚๎‚š๎‚…๎‚๎‚‰๎‚’๎‚˜ ๎‚–๎‚™๎‚๎‚‰๎‚— ๎‚‰๎‚œ๎‚๎‚—๎‚˜๎‡€
๎€–๎‚Œ๎‚๎‚— ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰ ๎‚Œ๎‚…๎‚— ๎‚š๎‚…๎‚–๎‚๎‚“๎‚™๎‚— ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚—๎†ฟ
n
๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚…๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚๎†ฝ ๎‚•๎‚™๎‚…๎‚๎‚๎‚˜๎‚ ๎‚…๎‚’๎‚ˆ ๎‚–๎‚‰๎‚๎‚๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ๎†ฝ ๎‚†๎‚ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎‚Š๎‚๎‚๎‚’๎‚‹ ๎‚†๎‚…๎‚ˆ
๎‚“๎‚– ๎‚ˆ๎‚…๎‚’๎‚‹๎‚‰๎‚–๎‚“๎‚™๎‚— ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹ ๎‚”๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚‡๎‚‰๎‚—๎†พ
n ๎‚Š๎‚…๎‚‡๎‚๎‚๎‚๎‚˜๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚’๎‚…๎‚๎‚๎‚—๎‚๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚ˆ๎‚™๎‚–๎‚๎‚’๎‚‹ ๎‚”๎‚‰๎‚‰๎‚– ๎‚–๎‚‰๎‚š๎‚๎‚‰๎‚› ๎‚“๎‚– ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚—๎‚˜๎‚…๎‚˜๎‚๎‚‡ ๎‚…๎‚’๎‚…๎‚๎‚๎‚—๎‚๎‚— ๎‚˜๎‚“๎‚“๎‚๎‚—๎†พ
n ๎‚‰๎‚—๎‚˜๎‚…๎‚†๎‚๎‚๎‚—๎‚Œ๎‚๎‚’๎‚‹ ๎‚… ๎‚๎‚‰๎‚š๎‚‰๎‚ ๎‚“๎‚Š ๎‚‡๎‚“๎‚’๎Šฑ๎‚ˆ๎‚‰๎‚’๎‚‡๎‚‰ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚๎†ฝ ๎‚–๎‚‰๎‚๎‚๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚…๎‚’๎‚ˆ ๎‚–๎‚“๎‚†๎‚™๎‚—๎‚˜๎‚’๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚… ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜๎†พ
n
๎‚‘๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚‘๎‚…๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚…๎‚’๎‚‡๎‚‰ ๎‚‰๎‚…๎‚—๎‚๎‚‰๎‚–๎†ฝ ๎‚…๎‚— ๎‚›๎‚‰๎‚๎‚ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚Š๎‚‰๎‚…๎‚˜๎‚™๎‚–๎‚‰๎‚—๎‡€
๎€–๎‚Œ๎‚‰ ๎‚๎‚ˆ๎‚‰๎‚… ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚˜๎‚“ ๎‚–๎‚‰๎‚๎‚’๎‚š๎‚‰๎‚’๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚›๎‚Œ๎‚‰๎‚‰๎‚๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚–๎‚…๎‚˜๎‚Œ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰ ๎‚‰๎‚œ๎‚๎‚—๎‚˜๎‚๎‚’๎‚‹ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‡ฆ๎‚‘๎‚‰๎‚˜๎‚Œ๎‚“๎‚ˆ๎‡‚
๎‚“๎‚๎‚“๎‚‹๎‚๎‚‡๎‚…๎‚ ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰๎‚—๎†ฝ ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰๎‚—๎†ฝ etc.๎‡ง ๎‚˜๎‚“ ๎‚‰๎‚œ๎‚˜๎‚–๎‚…๎‚‡๎‚˜๎†ฝ ๎‚‘๎‚“๎‚ˆ๎‚๎‚Š๎‚ ๎‚…๎‚’๎‚ˆ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚Š๎‚ ๎‚… ๎‚—๎‚‰๎‚˜ ๎‚“๎‚Š ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‡‚
๎‚‘๎‚‰๎‚’๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚…๎‚–๎‚‰ ๎‚…๎‚—
๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚—๎†ฟ
n
๎€๎€‹๎€•๎€”๎€ƒ๎‡‚๎€…๎†ฟ ๎ˆ‘๎ˆ๎ˆ๎ˆ‘ ๎€‰๎‚™๎‚๎‚ˆ๎‚‰๎‚๎‚๎‚’๎‚‰๎‚— ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚๎‚’ ๎‚‡๎‚–๎‚๎‚˜๎‚๎‚‡๎‚…๎‚ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚— ๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ๎†ฝ
n ๎€… ๎€ƒ๎€๎€•๎€‹ ๎ˆ˜๎ˆ ๎‡ฌ๎€ƒ๎‚’๎‚—๎‚๎€…๎ˆ˜๎ˆ๎‡ญ๎†ฝ
n ๎€… ๎€ƒ๎€๎€•๎€‹ ๎ˆ˜๎ˆ˜ ๎‡ฌ๎€ƒ๎‚’๎‚—๎‚๎€…๎ˆ˜๎ˆ˜๎‡ญ๎†ฝ
n
๎€‰๎€…๎€…๎†ฟ ๎€”๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰ ๎€†๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡ฌ๎€‰๎‚‡๎‚‡๎€”๎‚‰๎‚Š๎‡ญ๎†ฝ
n
๎€…๎€Ž๎€ƒ๎€๎€‰๎‡‹๎€• ๎€†๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡ฌ๎€…๎‚๎‚…๎‚’๎‚‹๎€”๎‚‰๎‚Š๎‡ญ๎†ฝ
n
๎€•๎€‡๎€‹ ๎€…๎€‡๎€”๎€– ๎€… ๎€…๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎€•๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ๎†ฝ
n
๎€‹๎€•๎€‘ ๎ˆ๎ˆ–๎ˆ˜๎ˆ•๎ˆ ๎€… ๎€•๎‚‰๎‚‡๎‚™๎‚–๎‚‰ ๎€…๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎€”๎‚™๎‚๎‚‰๎‚— ๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ๎†ฝ
n
๎€…๎€™๎€‡ ๎€๎€‹๎€–๎€”๎€‡ ๎€…๎‚“๎‚‘๎‚‘๎‚“๎‚’ ๎€™๎‚‰๎‚…๎‚๎‚’๎‚‰๎‚—๎‚— ๎€‡๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ๎‡€
๎€–๎‚Œ๎‚๎‚— ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚…๎‚๎‚‘๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎‚…๎‚’๎‚ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚– ๎‚…๎‚”๎‚”๎‚๎‚๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎Šฑ๎‚‰๎‚๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚ˆ๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‡‚
๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚–๎‚…๎‚๎‚’๎‚˜๎‚— ๎‚๎‚‘๎‚”๎‚“๎‚—๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚…๎‚’๎‚ ๎‚’๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚š๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚‰๎‚œ๎‚˜ ๎‡ฆ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚“๎‚˜๎‚๎‚š๎‚‰๎†ฝ ๎‚…๎‚‰๎‚–๎‚“๎‚’๎‚…๎‚™๎‚˜๎‚๎‚‡๎‚—๎†ฝ ๎‚‡๎‚–๎‚๎‚˜๎‚๎‚‡๎‚…๎‚ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚—๎†ฝ
etc.๎‡ง๎‡€ ๎€‹๎‚˜๎‚— ๎‚…๎‚๎‚‘ ๎‚๎‚— ๎‚˜๎‚“ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚‰๎‚๎‚ ๎‚˜๎‚Œ๎‚“๎‚—๎‚‰ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚‰ ๎€… ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚’๎‚“๎‚˜ ๎‚‡๎‚“๎‚š๎‚‰๎‚–๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚’๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚š๎‚‰
๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚–๎‚…๎‚๎‚’๎‚˜๎‚—๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 7
2
Coding convention
๎€ƒ๎‚†๎‚“๎‚š๎‚‰ ๎‚…๎‚๎‚๎†ฝ ๎‚…๎‚’๎‚ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚”๎‚–๎‚“๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚“๎‚Š ๎‚…๎‚’๎‚ ๎‚๎‚๎‚’๎‚ˆ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚› ๎‚… ๎‚‡๎‚๎‚‰๎‚…๎‚–๎†ฝ ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‡‚
๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚๎‚’๎‚“๎‚›๎‚’ ๎‚˜๎‚“ ๎‚…๎‚๎‚ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–๎‚— ๎‚…๎‚’๎‚ˆ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚ˆ
๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚๎‡€
๎€‡๎‚…๎‚‡๎‚Œ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚Œ๎‚…๎‚— ๎‚Œ๎‚๎‚— ๎‚“๎‚– ๎‚Œ๎‚‰๎‚–๎‚— ๎‚“๎‚›๎‚’ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹ ๎‚Œ๎‚…๎‚†๎‚๎‚˜๎‚—๎†ฝ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚…๎‚๎‚“๎‚™๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚’๎‚…๎‚‘๎‚๎‚’๎‚‹๎‡€ ๎€Š๎‚“๎‚›๎‡‚
๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚๎‚’๎‚‹ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹ ๎‚Œ๎‚…๎‚†๎‚๎‚˜๎‚— ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–๎‚— ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚๎‚’
๎‚… ๎‚Œ๎‚‰๎‚˜๎‚‰๎‚–๎‚“๎‚‹๎‚‰๎‚’๎‚‰๎‚“๎‚™๎‚— ๎‚—๎‚‰๎‚˜ ๎‚“๎‚Š ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰๎‚—๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚…๎‚–๎‚‰ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚…๎‚™๎‚ˆ๎‚๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚…๎‚๎‚’๎‚˜๎‚…๎‚๎‚’๎‡€
RULE
1
RULE โ€” Application of clear and explicit coding conventions
๎€…๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚”๎‚–๎‚“๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚๎‚‰๎‚š๎‚‰๎‚๎‡€
๎€–๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰ ๎‚…๎‚˜ ๎‚๎‚‰๎‚…๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚—๎†ฟ ๎‚‰๎‚’๎‚‡๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰๎‚—๎†ฝ
๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚…๎‚๎‚“๎‚™๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎†ฝ ๎‚’๎‚…๎‚‘๎‚๎‚’๎‚‹ ๎‡ฆ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‚‰๎‚—๎†ฝ ๎Šฑ๎‚๎‚‰๎‚—๎†ฝ ๎‚Š๎‚™๎‚’๎‚‡๎‡‚
๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚˜๎‚๎‚”๎‚‰๎‚—๎†ฝ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎†ฝ etc.๎‡ง๎†ฝ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‡€
๎€–๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–๎‡€
๎€–๎‚Œ๎‚๎‚— ๎‚–๎‚™๎‚๎‚‰ ๎‚–๎‚‰๎‚‹๎‚…๎‚–๎‚ˆ๎‚๎‚’๎‚‹ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚๎‚— ๎‚‡๎‚‰๎‚–๎‚˜๎‚…๎‚๎‚’๎‚๎‚ ๎‚“๎‚†๎‚š๎‚๎‚“๎‚™๎‚— ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚๎‚‘ ๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚˜๎‚“ ๎‚๎‚‘๎‚”๎‚“๎‚—๎‚‰๎†ฝ
๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚… ๎‚‡๎‚Œ๎‚“๎‚๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚’๎‚…๎‚‘๎‚๎‚’๎‚‹ ๎‡ฆ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— ๎‚—๎‚’๎‚…๎‚๎‚‰๎‡‚๎‚‡๎‚…๎‚—๎‚‰ ๎‚š๎‚‰๎‚–๎‚—๎‚™๎‚— ๎‚‡๎‚…๎‚‘๎‚‰๎‚๎‡‚๎‚‡๎‚…๎‚—๎‚‰๎‡ง๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚˜๎‚“ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜
๎‚… ๎‚‡๎‚Œ๎‚“๎‚๎‚‡๎‚‰ ๎‚Œ๎‚…๎‚— ๎‚๎‚’๎‚ˆ๎‚‰๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚‰๎‚’ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚‰๎‚‹๎‚๎‚’๎‚’๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚”๎‚–๎‚“๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚˜ ๎‚๎‚— ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‡€
๎€ƒ๎‚”๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚œ ๎€‡ ๎‚”๎‚–๎‚“๎‚š๎‚๎‚ˆ๎‚‰๎‚— ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎‚— ๎‚“๎‚Š ๎‚‡๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚“๎‚– ๎‚…๎‚ˆ๎‚…๎‚”๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚–๎‚‰๎‚•๎‚™๎‚๎‚–๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€†๎‚๎‚–๎‡€ ๎ˆ“๎‡€๎ˆ” ๎€‹๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚“๎‚š๎‚‰๎‚–๎‚๎‚…๎‚”๎‚”๎‚๎‚’๎‚‹ ๎‚š๎‚๎‚—๎‚๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚˜๎‚๎‚”๎‚“๎‡‚
๎‚‹๎‚–๎‚…๎‚”๎‚Œ๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚™๎‚’๎‚…๎‚‘๎‚†๎‚๎‚‹๎‚™๎‚“๎‚™๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ๎ˆ‘๎‡‚๎€… ๎€—๎‚—๎‚‰ ๎‚š๎‚๎‚—๎‚™๎‚…๎‚๎‚๎‚ ๎‚ˆ๎‚๎‚—๎‚˜๎‚๎‚’๎‚‡๎‚˜ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚—๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€—๎‚—๎‚๎‚’๎‚‹ ๎€‹๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚–๎‚‰๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡ฌ๎‚–๎‚‰๎‚—๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‡ญ๎‡€
8 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
3
Unde๎‚พned and unspeci๎‚พed behaviours
๎€ƒ๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚‘๎‚…๎‚๎‚’๎‚ˆ๎‚‰๎‚– ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰ ๎‚›๎‚๎‚๎‚ ๎‚Š๎‚–๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚˜๎‚๎‚ ๎‚‘๎‚…๎‚๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚‡๎‚‰๎‚”๎‚˜๎‚— ๎‚“๎‚Š unde๎Šฑned behaviour ๎‚…๎‚’๎‚ˆ
unspeci๎Šฑed behaviour๎†ฝ ๎‚›๎‚‰ ๎‚–๎‚‰๎‚‡๎‚…๎‚๎‚ ๎‚˜๎‚Œ๎‚‰๎‚‘ ๎‚Œ๎‚‰๎‚–๎‚‰๎‚๎‚’๎‚†๎‚‰๎‚๎‚“๎‚›๎‡€
Unde๎‚พned behaviour
๎€ƒ๎‚’ unde๎Šฑned behaviour ๎‚๎‚— ๎‚… ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚Š๎‚“๎‚– ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚’๎‚“๎‚˜๎‚Œ๎‚๎‚’๎‚‹ ๎‚๎‚— ๎‚๎‚‘๎‚”๎‚“๎‚—๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚—๎‚˜๎‚…๎‚’๎‡‚
๎‚ˆ๎‚…๎‚–๎‚ˆ๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚— ๎‚…๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎†ฝ ๎‚… ๎‚’๎‚“๎‚’๎‡‚๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚†๎‚๎‚‰
๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚– ๎‚…๎‚’ ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚ˆ๎‚…๎‚˜๎‚…๎‡€ ๎€ƒ๎‚’ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚๎‚— ๎‚… signed integer over๎Šทow๎‡€
Unspeci๎‚พed behaviour
๎€ƒ๎‚’ unspeci๎Šฑed behaviour ๎‚๎‚— ๎‚… ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚Š๎‚“๎‚– ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚”๎‚–๎‚“๎‚š๎‚๎‚ˆ๎‚‰๎‚— ๎‚…๎‚˜ ๎‚๎‚‰๎‚…๎‚—๎‚˜
๎‚˜๎‚›๎‚“ ๎‚…๎‚๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚˜๎‚๎‚š๎‚‰ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚…๎‚‡๎‚‡๎‚‰๎‚”๎‚˜๎‚‰๎‚ˆ๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚’๎‚“๎‚’๎‚‰ ๎‚“๎‚Š ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚…๎‚–๎‚‰ ๎‚๎‚‘๎‚”๎‚“๎‚—๎‚‰๎‚ˆ๎‡€ ๎€ƒ๎‚’
๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚“๎‚Š ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ‡ ๎‚…๎‚’๎‚ˆ ๎ˆ‡๎ˆ‡ ๎‚ˆ๎‚™๎‚–๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚™๎‚†๎‚—๎‚˜๎‚๎‚˜๎‚™๎‚˜๎‚๎‚“๎‚’
๎‚“๎‚Š ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‡€
Information
๎€–๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚Œ๎‚…๎‚™๎‚—๎‚˜๎‚๎‚š๎‚‰ ๎‚๎‚๎‚—๎‚˜ ๎‚“๎‚Š ๎‚…๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚’๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‚— ๎‚…๎‚’๎‚ˆ ๎‚…๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‚—
๎‚๎‚— ๎‚…๎‚š๎‚…๎‚๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚๎‚’ ๎‚…๎‚”๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚‡๎‚‰๎‚— ๎€‰ ๎‚…๎‚’๎‚ˆ ๎€Œ ๎‚“๎‚Š ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‚— ๎€…๎ˆ˜๎ˆ ๎‡ฌ
๎€ƒ๎‚’๎‚—๎‚๎€…๎ˆ˜๎ˆ๎‡ญ ๎‚…๎‚’๎‚ˆ ๎€…๎ˆ˜๎ˆ˜ ๎‡ฌ๎€ƒ๎‚’๎‚—๎‚๎€…๎ˆ˜๎ˆ˜๎‡ญ๎‡€
๎€–๎‚Œ๎‚๎‚— ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰ ๎‚“๎‚’๎‚๎‚ ๎‚‡๎‚“๎‚’๎‚—๎‚๎‚ˆ๎‚‰๎‚–๎‚— ๎‚… ๎€… ๎‚‡๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎‚‰๎‚’๎‚š๎‚๎‚–๎‚“๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚’๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎€…๎ˆ˜๎ˆ ๎‚“๎‚– ๎€…๎ˆ˜๎ˆ˜ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‚—๎‡€
Information
๎€…๎‚“๎‚’๎‚‡๎‚™๎‚–๎‚–๎‚‰๎‚’๎‚˜ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚‡๎‚“๎‚š๎‚‰๎‚–๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚๎‚— ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚›๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚š๎‡‚
๎‚‰๎‚–๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚… ๎‚๎‚…๎‚˜๎‚‰๎‚– ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‡€
RULE
2
RULE โ€” Only C coding in accordance with the standard is authorised
๎€๎‚“ ๎‚š๎‚๎‚“๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎€… ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚–๎‚…๎‚๎‚’๎‚˜๎‚— ๎‚…๎‚’๎‚ˆ ๎‚—๎‚๎‚’๎‚˜๎‚…๎‚œ ๎‚…๎‚— ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎€…๎ˆ˜๎ˆ ๎‚“๎‚– ๎€…๎ˆ˜๎ˆ˜ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‚— ๎‚๎‚—
๎‚…๎‚™๎‚˜๎‚Œ๎‚“๎‚–๎‚๎‚—๎‚‰๎‚ˆ๎‡€
References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎‡€๎ˆ ๎€–๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚’๎‚“ ๎‚š๎‚๎‚“๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎€… ๎‚—๎‚๎‚’๎‚˜๎‚…๎‚œ ๎‚…๎‚’๎‚ˆ con-
straints ๎‚…๎‚’๎‚ˆ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚‰๎‚œ๎‚‡๎‚‰๎‚‰๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚–๎‚…๎‚’๎‚—๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚๎‚‘๎‚๎‚˜๎‚—๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎‡€๎ˆ‘ ๎€Ž๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚‰๎‚œ๎‚˜๎‚‰๎‚’๎‚—๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎‡€๎ˆ’ ๎€–๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚’๎‚“ ๎‚“๎‚‡๎‚‡๎‚™๎‚–๎‚–๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚“๎‚– ๎‚‡๎‚–๎‚๎‚˜๎‚๎‚‡๎‚…๎‚ ๎‚™๎‚’๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ–๎ˆ๎ˆ ๎€‹๎‚‘๎‚”๎‚–๎‚“๎‚”๎‚‰๎‚– ๎€ƒ๎‚ˆ๎‚Œ๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰ ๎‚˜๎‚“ ๎‚‡๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 9
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ–๎ˆ”๎ˆ— ๎€”๎‚‰๎‚๎‚๎‚…๎‚’๎‚‡๎‚‰ ๎‚“๎‚’ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ๎†ฝ ๎‚™๎‚’๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚“๎‚– ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‡‚๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚–๎‡€
10 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
4
Preprocessor and macros
4.1 Inclusion of the necessary header ๎‚พles
๎€‘๎‚’๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚— ๎‚’๎‚‰๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚ˆ๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚—๎‚“๎‚‘๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚–๎‚™๎‚๎‚‰๎‚— ๎‚’๎‚‰๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚“๎‚†๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ๎‡€
๎€™๎‚Œ๎‚‰๎‚’ ๎‚… ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚๎‚˜๎‚—๎‚‰๎‚๎‚Š ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚— ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚›๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚”๎‚–๎‚“๎‚”๎‚…๎‚‹๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚๎‚
๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚“๎‚– ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰ ๎‚˜๎‚Œ๎‚๎‚— ๎Šฑ๎‚–๎‚—๎‚˜ ๎Šฑ๎‚๎‚‰๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚‘๎‚‰๎‚‡๎‚Œ๎‚…๎‚’๎‚๎‚—๎‚‘ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‡๎‚…๎‚ˆ๎‚‰๎‚ˆ
๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎€‹๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚— ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚‘๎‚๎‚’๎‚๎‚‘๎‚๎‚—๎‚‰๎‚ˆ๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚˜๎‚‰๎‚— ๎‚™๎‚’๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚‰๎‚’๎‚‡๎‚๎‚‰๎‚—๎†ฝ ๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚…๎‚—๎‚‰๎‚—
๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚๎‚‘๎‚‰๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚™๎‚†๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚˜ ๎‚…๎‚’๎‚…๎‚๎‚๎‚—๎‚๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ ๎‡ฆ๎‚›๎‚Œ๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚‘๎‚…๎‚’๎‚™๎‚…๎‚
๎‚“๎‚– ๎‚˜๎‚“๎‚“๎‚๎‡‚๎‚†๎‚…๎‚—๎‚‰๎‚ˆ๎‡ง๎‡€ ๎€‹๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚–๎‚‰๎‚ˆ๎‚™๎‚‡๎‚‰ ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚‰๎‚’๎‚‡๎‚๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚™๎‚’๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚”๎‚–๎‚“๎‚”๎‚…๎‚‹๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ
๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚๎‚’ ๎‚… ๎‡Œ๎‡€๎‚‡๎‡ ๎Šฑ๎‚๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚๎‚’ ๎‚… ๎‡Œ๎‡€๎‚Œ๎‡ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‡€ ๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚๎‚’ ๎‚—๎‚“๎‚‘๎‚‰
๎‚‡๎‚…๎‚—๎‚‰๎‚—๎†ฝ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— ๎‚˜๎‚๎‚”๎‚๎‚‡๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚— ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‡ฆ๎‚—๎‚™๎‚‡๎‚Œ
๎‚…๎‚— stddef.h ๎‚…๎‚’๎‚ˆ stdint.h๎‡ง ๎‚๎‚’ ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚๎‚— ๎‚Ž๎‚™๎‚—๎‚˜๎‚๎Šฑ๎‚…๎‚†๎‚๎‚‰๎‡€
RECO
3
RECOMMENDATION โ€” Limit and justify header ๎‚พle inclusions in another
header ๎‚พle
๎€Š๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚’๎‚‰๎‚‰๎‚ˆ๎‚‰๎‚ˆ ๎‚ˆ๎‚™๎‚–๎‚๎‚’๎‚‹ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‡Œ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚๎‡ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰
๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–๎‡€
RULE
4
RULE โ€” Only the necessary header ๎‚พles should be included
๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚‘๎‚‰๎‚‡๎‚Œ๎‚…๎‚’๎‚๎‚—๎‚‘ ๎‚‡๎‚…๎‚’ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚๎‚’ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰
๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎†ฝ ๎‚‘๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚”๎‚–๎‚“๎‚“๎‚Š๎‚–๎‚‰๎‚…๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚…๎‚˜ ๎‚†๎‚‰๎‚—๎‚˜๎‡€ ๎€†๎‚‰๎Šฑ๎‚’๎‚๎‚’๎‚‹ ๎‚… ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‡ ๎‚—๎‚๎‚‘๎‚†๎‚“๎‚ ๎‚Š๎‚“๎‚– ๎‚‰๎‚…๎‚‡๎‚Œ
๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‡ฆ#define๎‡ง ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚๎‚— ๎‚—๎‚๎‚‘๎‚†๎‚“๎‚ ๎‚Œ๎‚…๎‚— ๎‚’๎‚“๎‚˜ ๎‚…๎‚๎‚–๎‚‰๎‚…๎‚ˆ๎‚
๎‚†๎‚‰๎‚‰๎‚’ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‡ฆ#ifndef๎‡ง ๎‚…๎‚š๎‚“๎‚๎‚ˆ๎‚— ๎‚–๎‚‰๎‚”๎‚‰๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚๎‚— ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚–๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚— ๎‚… multiple
include guard macro๎‡€ ๎€„๎‚‰ ๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰ ๎‚… ๎‚™๎‚’๎‚๎‚•๎‚™๎‚‰ ๎‚—๎‚๎‚‘๎‚†๎‚“๎‚ ๎‚Š๎‚“๎‚– ๎‚‰๎‚…๎‚‡๎‚Œ ๎Šฑ๎‚๎‚‰๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚—๎‚๎‚‘๎‚†๎‚“๎‚ ๎‚‡๎‚…๎‚’
๎‚†๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚—๎‚™๎‚†๎‚—๎‚˜๎‚๎‚˜๎‚™๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‡Œ๎‡€๎‡ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‡Œ๎‡ ๎‡๎‡€
RULE
5
RULE โ€” Use multiple include guard macros for a ๎‚พle
๎€ƒ ๎‚‹๎‚™๎‚…๎‚–๎‚ˆ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚…๎‚‹๎‚…๎‚๎‚’๎‚—๎‚˜ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚… ๎Šฑ๎‚๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚”๎‚–๎‚‰๎‚š๎‚‰๎‚’๎‚˜ ๎‚˜๎‚Œ๎‚‰
๎‚‡๎‚“๎‚’๎‚˜๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚… ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚†๎‚‰๎‚๎‚’๎‚‹ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚ˆ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚“๎‚’๎‚‡๎‚‰๎†ฟ
// start of header file
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 11
#ifndef HEADER_H
#define HEADER_H
/* file content */
#endif
// end of header file
Warning
๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ #pragma once ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚๎‚— ๎‚›๎‚๎‚ˆ๎‚‰๎‚—๎‚”๎‚–๎‚‰๎‚…๎‚ˆ ๎‚†๎‚™๎‚˜ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‚๎‚ž๎‚‰๎‚ˆ๎‡€ ๎€–๎‚Œ๎‚๎‚—
๎‚—๎‚“๎‚๎‚™๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚‡๎‚“๎‚‹๎‚’๎‚๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰๎†ฝ ๎‚…๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚‹๎‚Œ ๎‚๎‚˜ ๎‚๎‚— ๎‚—๎‚™๎‚”๎‚”๎‚“๎‚–๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚‘๎‚“๎‚—๎‚˜
๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚—๎‡€ ๎€‹๎‚˜๎‚— ๎‚™๎‚—๎‚‰ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚๎‚— ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚๎‚— ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‡ ๎‚˜๎‚“ ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚‡๎‚“๎‚‘๎‡‚
๎‚”๎‚๎‚๎‚‰๎‚– ๎‡ฆ๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚…๎‚๎‚๎‚ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚‘๎‚…๎‚’๎‚…๎‚‹๎‚๎‚’๎‚‹ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚™๎‚”๎‚๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚”๎‚Œ๎‚๎‚—๎‚๎‚‡๎‚…๎‚
๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰๎‚— ๎‚“๎‚– ๎‚‘๎‚“๎‚™๎‚’๎‚˜ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚—๎‡ง๎‡€
๎€ˆ๎‚๎‚’๎‚…๎‚๎‚๎‚๎†ฝ ๎‚Š๎‚“๎‚– ๎‚–๎‚‰๎‚…๎‚—๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚–๎‚‰๎‚…๎‚ˆ๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚ ๎‚›๎‚๎‚˜๎‚Œ ๎‚‡๎‚‰๎‚–๎‚˜๎‚…๎‚๎‚’
๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‡ ๎‚–๎‚™๎‚๎‚‰๎‚—๎‡€
RULE
6
RULE โ€” Header ๎‚พle inclusions are grouped at the beginning of the ๎‚พle
๎€ƒ๎‚๎‚ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‹๎‚–๎‚“๎‚™๎‚”๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚‰๎‚‹๎‚๎‚’๎‚’๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚“๎‚– ๎‚Ž๎‚™๎‚—๎‚˜ ๎‚…๎Šน๎‚‰๎‚–
๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚– ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚—๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‡‚
๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚“๎‚– ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
RECO
7
RECOMMENDATION โ€” System header ๎‚พle inclusions are made before user
header ๎‚พle inclusions
GOOD
PRACTICE
8
GOOD PRACTICE โ€” Use alphabetical order in the inclusion of each type of
header ๎‚พles
๎€–๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚…๎‚’๎‚ ๎‚–๎‚‰๎‚ˆ๎‚™๎‚’๎‚ˆ๎‚…๎‚’๎‚‡๎‚ ๎‚๎‚’ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘ ๎‚“๎‚– ๎‚™๎‚—๎‚‰๎‚– ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚‡๎‚…๎‚’
๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚๎‚”๎‚Œ๎‚…๎‚†๎‚‰๎‚˜๎‚๎‚‡๎‚…๎‚ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚–๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚“๎Šฎ๎‚‰๎‚–๎‚— ๎‚… ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚๎‚—๎‚˜๎‚๎‚‡ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚Š๎‚…๎‚‡๎‚๎‚๎‚๎‚˜๎‚…๎‚˜๎‚‰๎‚—
๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚–๎‚‰๎‚š๎‚๎‚‰๎‚›๎‡€
Information
๎€ƒ๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚‹๎‚Œ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚๎‚…๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚–๎‚‰๎‚‰ ๎‚–๎‚™๎‚๎‚‰๎‚—๎†ฝ ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ ๎‚‹๎‚“๎‚“๎‚ˆ ๎‚”๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚‡๎‚‰๎‚— ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚… ๎‚”๎‚–๎‚“๎‚†๎‡‚
๎‚๎‚‰๎‚‘ ๎‚“๎‚Š ๎‚–๎‚‰๎‚…๎‚ˆ๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚…๎‚๎‚’๎‚˜๎‚…๎‚๎‚’๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚… ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚ ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰
๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜ ๎‚—๎‚‰๎‚’๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚‰๎‚–๎‚‘๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎Šฑ๎‚–๎‚—๎‚˜ ๎‚˜๎‚›๎‚“ ๎‚…๎‚—๎‚”๎‚‰๎‚‡๎‚˜๎‚— ๎‚–๎‚‰๎‚‘๎‚…๎‚๎‚’ ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚Š๎‚“๎‚– ๎‚…๎‚’๎‚ ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‡‚
๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜๎‡€
๎€™๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚๎‚— ๎‚“๎‚‘๎‚๎‚˜๎‚˜๎‚‰๎‚ˆ๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚‘๎‚…๎‚ ๎‚”๎‚–๎‚“๎‚š๎‚๎‚ˆ๎‚‰ ๎‚… ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹ ๎‚…๎‚†๎‚“๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰
๎‚“๎‚Š ๎‚…๎‚’ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
Information
๎€‹๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’
-Wimplicit-function-declaration๎‡€
12 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚†๎‚‰๎‚๎‚“๎‚›๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š string.h ๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚— ๎‚’๎‚“ ๎‚”๎‚™๎‚–๎‚”๎‚“๎‚—๎‚‰ ๎‚๎‚’ file.h ๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰
๎‚“๎‚’๎‚๎‚ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š string.h ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚†๎‚ file.c ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰ memcpy ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’
๎‚“๎‚Š string.h ๎‚‘๎‚™๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚†๎‚‰ ๎‚‘๎‚“๎‚š๎‚‰๎‚ˆ ๎‚˜๎‚“ file.c ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰ ๎‚‹๎‚™๎‚…๎‚–๎‚ˆ ๎‚๎‚’
file.h๎‡€
/* header .h */
#include <string .h> /* should only be included in file.c */
void foo( uint8_t* val , uint32_t length );
/* file.c */
#include <string .h>
#include <stdint .h>
#include <stdlib .h>
#include "header .h"
#define BUFFER_LEN 8U
void foo( uint8_t *val , uint32_t length ) {
uint8_t buffer[BUFFER_LEN];
if (NULL != val) {
memcpy (buffer , val , min( BUFFER_LEN , length));
...
}
}
Good example
๎€–๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚†๎‚‰๎‚๎‚“๎‚› ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚“๎‚’๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚’
๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰ ๎‚‹๎‚™๎‚…๎‚–๎‚ˆ ๎‚๎‚— ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‡€ ๎€„๎‚‰ ๎‚‡๎‚…๎‚–๎‚‰๎‚Š๎‚™๎‚ ๎‚Œ๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰ ๎‚‹๎‚™๎‚…๎‚–๎‚ˆ๎†ฝ ๎‚’๎‚“๎‚˜ ๎‚˜๎‚“
๎‚™๎‚—๎‚‰ ๎‚…๎‚’ ๎‚…๎‚๎‚–๎‚‰๎‚…๎‚ˆ๎‚ ๎‚–๎‚‰๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚– ๎‚…๎‚— ๎‚… ๎‚’๎‚…๎‚‘๎‚‰ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚๎‚— ๎‚… ๎‚‡๎‚๎‚…๎‚—๎‚—๎‚๎‚‡ ๎‚‰๎‚–๎‚–๎‚“๎‚–
๎‚›๎‚Œ๎‚‰๎‚’ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰ ๎‚‹๎‚™๎‚…๎‚–๎‚ˆ๎‡€
/* header .h */
#ifndef HEADER_H /* include guard to avoid ultiple inclusion */
#define HEADER_H
void foo( uint8_t *val , uint32_t length );
#endif /* HEADER_H */
/* file.c */
#include <string .h>
#include <stdint .h>
#include <stdlib .h>
#include "header .h"
#define BUFFER_LEN 8U
void foo( uint8_t *val , uint32_t length ) {
uint8_t buffer[BUFFER_LEN];
if (NULL != val) {
memcpy (buffer , val , min( BUFFER_LEN , length));
...
}
}
4.1.1 References
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 13
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€†๎‚๎‚–๎‡€ ๎ˆ“๎‡€๎ˆ๎ˆ๎‡€ ๎€’๎‚–๎‚‰๎‚‡๎‚…๎‚™๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚˜๎‚…๎‚๎‚‰๎‚’ ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚”๎‚–๎‚‰๎‚š๎‚‰๎‚’๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚… ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰
๎‚†๎‚‰๎‚๎‚’๎‚‹ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚ˆ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚“๎‚’๎‚‡๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎ˆ๎‡€๎ˆ๎‡€ #include ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚“๎‚’๎‚๎‚ ๎‚†๎‚‰ ๎‚”๎‚–๎‚‰๎‚‡๎‚‰๎‚ˆ๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚— ๎‚“๎‚–
๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€’๎€”๎€‡๎ˆ๎ˆ•๎‡‚๎€… ๎€‡๎‚’๎‚‡๎‚๎‚“๎‚—๎‚‰ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚— ๎‚๎‚’ ๎‚…๎‚’ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰ ๎‚‹๎‚™๎‚…๎‚–๎‚ˆ๎‡€
4.2 Non-inclusion of source ๎‚พles
๎€–๎‚Œ๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚๎‚’ ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚‡๎‚…๎‚’ ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚˜๎‚‰ ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚๎‚๎‚’๎‚ ๎‚‰๎‚ˆ๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‡ฆ๎‚‘๎‚™๎‚๎‚˜๎‚๎‡‚
๎‚”๎‚๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚“๎‚– ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎‚‡๎‚…๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡ง ๎‚“๎‚– ๎‚ˆ๎‚™๎‚”๎‚๎‚๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚†๎‚๎‚’๎‚…๎‚–๎‚ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‡ฆ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚š๎‚‰๎‚’๎‚˜
๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚ˆ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚†๎‚‰๎‚‰๎‚’ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ static๎‡ง๎‡€ ๎€‹๎‚Š ๎‚… ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚–๎‚‰๎‚•๎‚™๎‚๎‚–๎‚‰๎‚—
๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚Š๎‚–๎‚“๎‚‘ ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰๎†ฝ ๎‚… ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ
๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚’๎‚‰๎‚‰๎‚ˆ๎‚— ๎‚๎‚˜๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚†๎‚–๎‚“๎‚๎‚‰๎‚’ ๎‚ˆ๎‚“๎‚›๎‚’ ๎‚๎‚’๎‚˜๎‚“ ๎‚๎‚’๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜ ๎‚‘๎‚“๎‚ˆ๎‚™๎‚๎‚‰๎‚—
๎‡ฆ๎‡Œ๎‡€๎‚‡๎‡ ๎Šฑ๎‚๎‚‰๎‚—๎‡ง๎‡€
๎€‹๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚™๎‚–๎‚”๎‚“๎‚—๎‚‰ ๎‚“๎‚Š ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚’๎‚‰ ๎‚‘๎‚“๎‚ˆ๎‚™๎‚๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚๎‚— ๎‚˜๎‚“ ๎‚˜๎‚…๎‚๎‚‰ ๎‚…๎‚ˆ๎‚š๎‚…๎‚’๎‚˜๎‚…๎‚‹๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‡‹๎‚— ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡‚
๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚ˆ๎‚™๎‚–๎‚…๎‚ ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‡ฆinlining๎†ฝ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚”๎‚–๎‚“๎‚”๎‚…๎‚‹๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ etc.๎‡ง๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚”๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚…๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚–๎‚‰๎‚๎‚ ๎‚“๎‚’ ๎€Ž๎‚๎‚’๎‚ ๎€–๎‚๎‚‘๎‚‰
๎€‘๎‚”๎‚˜๎‚๎‚‘๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡ฆ๎€Ž๎€–๎€‘๎‡ง๎†ฝ ๎‚Š๎‚“๎‚– ๎‚๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚‡๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎„Œ๎„ˆ๎„ˆ ๎‚†๎‚ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ -flto ๎Šท๎‚…๎‚‹ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚๎‚’๎‚‹ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚˜ ๎‚๎‚๎‚’๎‚ ๎‚˜๎‚๎‚‘๎‚‰๎†ฟ
gcc -o binary -flto file1.c file2.c๎‡€
RULE
9
RULE โ€” Do not include a source ๎‚พle in another source ๎‚พle
๎€‘๎‚’๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚— ๎‚๎‚— ๎‚…๎‚™๎‚˜๎‚Œ๎‚“๎‚–๎‚๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚… ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚… ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚๎‚— ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ๎‡€
/* file1 .c */
#include <stdint .h>
void foo( uint16_t val) {
...
}
/* file2 .c */
#include "file1 .c" /* prohibited */
void bar() {
foo( MAGIC_VALUE);
}
Good example
๎€–๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚†๎‚‰๎‚๎‚“๎‚› ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚†๎‚–๎‚‰๎‚…๎‚๎‚— ๎‚ˆ๎‚“๎‚›๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚’๎‚˜๎‚“ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚‘๎‚“๎‚ˆ๎‚™๎‚๎‚‰๎‚—๎‡€
/* header .h */
#ifndef HEADER_H
#define HEADER_H
void foo( uint16_t val);
#endif
14 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
/* file1 .c */
void foo( uint16_t val) {
...
}
/* file2 .c */
#include <stdint .h>
#include "header .h"
#define MAGIC_VALUE 42U
void bar() {
foo( MAGIC_VALUE);
}
4.3 Format of a ๎‚พle inclusion directive
๎€†๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎Šฑ๎‚๎‚‰ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚— ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚‰ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚›๎‚…๎‚๎†ฟ ๎‚—๎‚“๎‚‘๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚— ๎‚…๎‚–๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰
๎‚—๎‚‰๎‚”๎‚…๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚๎‚˜๎‚™๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚… ๎‚”๎‚…๎‚˜๎‚Œ ๎‚‘๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‡€
๎€™๎‚Œ๎‚‰๎‚’ ๎‚…๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‡‚๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‡ ๎‚”๎‚…๎‚˜๎‚Œ ๎‚—๎‚‰๎‚”๎‚…๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚”๎‚–๎‚‰๎‚š๎‚‰๎‚’๎‚˜๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰
๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€ ๎€™๎‚Œ๎‚‰๎‚’ ๎‚…๎‚’ #include ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚— ๎‚… ๎‚”๎‚…๎‚˜๎‚Œ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚ˆ๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚”๎‚…๎‚–๎‚…๎‚˜๎‚๎‚’๎‚‹
๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚˜๎‚Œ ๎‚‡๎‚“๎‚‘๎‚”๎‚“๎‚’๎‚‰๎‚’๎‚˜๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚…๎‚—๎‚Œ ๎‡Œ๎‡š๎‡๎†ฝ ๎‚’๎‚“๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚…๎‚‡๎‚๎‚—๎‚๎‚…๎‚—๎‚Œ ๎‡Œ๎‡›๎‡๎†ฝ ๎‚˜๎‚“ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚
๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰๎‡€ ๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‡Œ๎‡›๎‡๎†ฝ ๎‚…๎‚— ๎‚›๎‚‰๎‚๎‚ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚– ๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰๎‚— ๎‚“๎‚Š
๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‚—๎†ฟ '๎†ฝ "๎†ฝ /* ๎‚…๎‚’๎‚ˆ // ๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚“๎‚”๎‚‰๎‚’๎‚๎‚’๎‚‹ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚๎‚“๎‚—๎‚๎‚’๎‚‹ ๎‚‡๎‚Œ๎‚‰๎‚š๎‚–๎‚“๎‚’๎‚— ๎‡ฆ< ๎‚…๎‚’๎‚ˆ >๎‡ง ๎‚“๎‚– ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’
๎‚ˆ๎‚“๎‚™๎‚†๎‚๎‚‰ ๎‚•๎‚™๎‚“๎‚˜๎‚‰๎‚— ๎‡ฆi.e. "๎‡ง ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€
๎€†๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚“๎‚–๎‚ ๎‚…๎‚’๎‚ˆ ๎Šฑ๎‚๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚…๎‚๎‚—๎‚“ ๎‚†๎‚‰ ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ๎‡€
RULE
10
RULE โ€” File paths must be portable and case sensitive
๎€ˆ๎‚๎‚๎‚‰ ๎‚”๎‚…๎‚˜๎‚Œ๎‚—๎†ฝ ๎‚›๎‚Œ๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚Š๎‚“๎‚– ๎‚…๎‚’ #include ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚“๎‚– ๎‚’๎‚“๎‚˜๎†ฝ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚†๎‚๎‚‰
๎‚›๎‚Œ๎‚๎‚๎‚‰ ๎‚–๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚“๎‚–๎‚ ๎‚’๎‚…๎‚‘๎‚‰๎‚—๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚…๎‚—๎‚—๎‚™๎‚–๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚๎‚‰๎‚…๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€
#include <sys\stat.h>
#include "Module_A \Sub_Module_A \Header.h"
Good example
๎€–๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚†๎‚‰๎‚๎‚“๎‚› ๎‚™๎‚—๎‚‰๎‚— ๎‚… ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜ ๎‚Š๎‚“๎‚– ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚๎‚’๎‚‹ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚—๎‡€
#include <sys/stat.h>
#include "module_a / sub_module_a/header.h"
๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚‡๎‚‰๎‚–๎‚˜๎‚…๎‚๎‚’ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‡ ๎‚–๎‚™๎‚๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ
๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 15
RULE
11
RULE โ€” The name of a header ๎‚พle must not contain certain characters or
sequences of characters
๎€–๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚’๎‚“๎‚’๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‚— ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‡‚
๎‚˜๎‚‰๎‚– ๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰๎‚—๎†ฟ '๎†ฝ "๎†ฝ \๎†ฝ /* ๎‚…๎‚’๎‚ˆ //๎‡€
4.3.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎ˆ๎‡€๎ˆ‘๎†ฟ ๎€–๎‚Œ๎‚‰ ๎‡๎†ฝ๎‡ ๎‚“๎‚– ๎‡๎‡›๎‡ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‚— ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‡๎‡š๎‡ธ๎‡ ๎‚…๎‚’๎‚ˆ ๎‡๎‡š๎‡š๎‡ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜
๎‚“๎‚‡๎‚‡๎‚™๎‚– ๎‚๎‚’ ๎‚… ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎ˆ๎‡€๎ˆ’๎†ฟ ๎€–๎‚Œ๎‚‰ #include ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚‰๎‚๎‚˜๎‚Œ๎‚‰๎‚– ๎‚… <filename> ๎‚“๎‚–
"filename" ๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰๎‡€
๎‡ฌ๎€ƒ๎‚’๎‚—๎‚๎€…๎ˆ˜๎ˆ˜๎‡ญ ๎€•๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ•๎‡€๎ˆ“๎‡€๎ˆ– ๎‚…๎‚’๎‚ˆ ๎ˆ•๎‡€๎ˆ๎ˆ๎‡€๎ˆ‘๎‡€
4.4 Comment and de๎‚พnition of preprocessor blocks
๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚— #if๎†ฝ #ifdef๎†ฝ#ifndef๎†ฝ #else๎†ฝ #elif ๎‚…๎‚’๎‚ˆ #endif ๎‚Š๎‚“๎‚–๎‚‘ ๎‚†๎‚๎‚“๎‚‡๎‚๎‚—๎‡€ ๎€–๎‚Œ๎‚‰๎‚—๎‚‰
๎‚‘๎‚…๎‚ ๎‚†๎‚‰ ๎‚๎‚“๎‚’๎‚‹ ๎‚…๎‚’๎‚ˆ ๎‚๎‚‘๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚ˆ๎‚๎‚—๎‚”๎‚๎‚…๎‚ ๎‚“๎‚’ ๎‚… ๎‚—๎‚๎‚’๎‚‹๎‚๎‚‰ ๎‚—๎‚‡๎‚–๎‚‰๎‚‰๎‚’๎‡€ ๎€–๎‚Œ๎‚‰๎‚ ๎‚‘๎‚…๎‚ ๎‚…๎‚๎‚—๎‚“ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚’๎‚‰๎‚—๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚๎‚“๎‚‡๎‚๎‚—๎‡€
๎€‹๎‚˜ ๎‚‡๎‚…๎‚’ ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚†๎‚‰ ๎‚š๎‚‰๎‚–๎‚ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚‰๎‚’๎‚‡๎‚๎‚‰๎‚— ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚—๎‡€
๎€–๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚‡๎‚…๎‚–๎‚‰๎‚Š๎‚™๎‚๎‚๎‚ ๎‚˜๎‚“ ๎‚‰๎‚œ๎‚”๎‚๎‚…๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰๎‚— ๎‚ˆ๎‚‰๎‚…๎‚๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰
๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚—๎‡€
RECO
12
RECOMMENDATION โ€” Preprocessor blocks must be commented on
๎€’๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚†๎‚๎‚“๎‚‡๎‚ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚‡๎‚๎‚…๎‚–๎‚๎‚Š๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰๎‚—
๎‚†๎‚‰๎‚๎‚’๎‚‹ ๎‚ˆ๎‚‰๎‚…๎‚๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ ๎‚…๎‚’๎‚ˆ๎†ฝ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‡Œ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚‘๎‚‰๎‚ˆ๎‚๎‚…๎‚˜๎‚‰๎‡ ๎‚…๎‚’๎‚ˆ ๎‡Œ๎‚‡๎‚๎‚“๎‚—๎‚๎‚’๎‚‹๎‡ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰
๎‚‘๎‚™๎‚—๎‚˜ ๎‚…๎‚๎‚—๎‚“ ๎‚†๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‡Œ๎‚“๎‚”๎‚‰๎‚’๎‚๎‚’๎‚‹๎‡ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚†๎‚ ๎‚‘๎‚‰๎‚…๎‚’๎‚— ๎‚“๎‚Š ๎‚…
๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‡€
๎€ˆ๎‚“๎‚– ๎‚–๎‚‰๎‚…๎‚—๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚–๎‚‰๎‚…๎‚ˆ๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚๎†ฝ ๎‚ˆ๎‚“๎‚™๎‚†๎‚๎‚‰ ๎‚’๎‚‰๎‚‹๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚’ ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚…๎‚š๎‚“๎‚๎‚ˆ๎‚‰๎‚ˆ๎†ฝ
๎‚˜๎‚๎‚”๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚†๎‚ ๎‚™๎‚—๎‚๎‚’๎‚‹ #ifndef ๎‚…๎‚’๎‚ˆ ๎‚… ๎‡Œ๎‚’๎‚“๎‚’๎‡‚๎‚‘๎‚“๎‚ˆ๎‚‰๎‡ ๎‡ฆi.e. ๎‚… ๎‚‘๎‚“๎‚ˆ๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚š๎‚๎‚… ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚‰๎‚‹๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚‘๎‚“๎‚ˆ๎‚‰
๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— NDEBUG๎‡ง๎‡€
GOOD
PRACTICE
13
GOOD PRACTICE โ€” Double negation in the expression of preprocessor
block conditions should be avoided
๎€ˆ๎‚๎‚’๎‚…๎‚๎‚๎‚๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚— ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚†๎‚๎‚“๎‚‡๎‚ ๎‡ฆi.e. ๎‡Œ๎‚“๎‚”๎‚‰๎‚’๎‚๎‚’๎‚‹๎‡๎†ฝ
๎‡Œ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚‘๎‚‰๎‚ˆ๎‚๎‚…๎‚˜๎‚‰๎‡ ๎‚…๎‚’๎‚ˆ ๎‡Œ๎‚‡๎‚๎‚“๎‚—๎‚๎‚’๎‚‹๎‡ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚—๎‡ง ๎‚…๎‚–๎‚‰ ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎Šฑ๎‚๎‚‰๎‡€ ๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚–๎‚‘๎‚“๎‚–๎‚‰๎†ฝ ๎‚๎‚˜ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚“๎‚’๎‚๎‚
๎‚†๎‚‰ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚— ๎‚˜๎‚“ ๎ˆ ๎‚“๎‚– ๎ˆ๎‡€
RULE
14
RULE โ€” De๎‚พnition of a preprocessor block in a single ๎‚พle
๎€ˆ๎‚“๎‚– ๎‚… ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚†๎‚๎‚“๎‚‡๎‚๎†ฝ ๎‚…๎‚๎‚ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚Š๎‚“๎‚™๎‚’๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎Šฑ๎‚๎‚‰๎‡€
16 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
RECO
15
RECOMMENDATION โ€” Preprocessor directive control expressions must be
correctly formed
๎€…๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚’๎‚๎‚ ๎‚˜๎‚“ ๎ˆ ๎‚“๎‚– ๎ˆ ๎‚…๎‚’๎‚ˆ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚“๎‚’๎‚๎‚ ๎‚™๎‚—๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ
๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚— ๎‡ฆ๎‚š๎‚๎‚… #define๎‡ง๎‡€
Bad example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚‘๎‚“๎‚ˆ๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚ˆ๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚…๎‚’๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚ˆ๎‚“๎‚™๎‚†๎‚๎‚‰ ๎‚’๎‚‰๎‚‹๎‚…๎‡‚
๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ #else ๎‚…๎‚’๎‚ˆ #endif ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚—๎‡€
/* file1 .c */
#ifdef A /* no #endif */
#include "log.h"
/* log.h */
#endif /* # endif of file1 .c */
#ifndef LOG_H
#define LOG_H
typedef enum {
DEBUG = 0,
WARN ,
INFO ,
ERROR ,
FATAL
} LogLevel_T;
void log_msg( LogLevel_T level , const unsigned char * sLogMessage );
#ifndef NDEBUG /* double negation */
#define LOG_DEBUG(msg) log_msg(DEBUG , ( msg))
#define LOG_WARN(msg) log_msg (WARN , (msg))
#define LOG_INFO(msg) log_msg (INFO , (msg))
#define LOG_ERROR(msg) log_msg(ERROR , ( msg))
#define LOG_FATAL(msg) log_msg(FATAL , ( msg))
#else
#define LOG_DEBUG(msg)
#define LOG_WARN(msg)
#define LOG_INFO(msg)
#define LOG_ERROR(msg)
#define LOG_FATAL(msg)
#endif
#endif
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‡‚
๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎Šฑ๎‚๎‚‰๎‡€
/* file1 .c */
#ifdef A
#include "log.h"
#endif
/* log.h */
#ifndef LOG_H
#define LOG_H
typedef enum {
DEBUG = 0,
WARN ,
INFO ,
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 17
ERROR ,
FATAL
} LogLevel_T;
void log_msg( LogLevel_T level , const unsigned char * sLogMessage );
#ifdef NDEBUG /* double negation removed */
/* Not debug mode */
#define LOG_DEBUG(msg)
#define LOG_WARN(msg)
#define LOG_INFO(msg)
#define LOG_ERROR(msg)
#define LOG_FATAL(msg)
#else /* #ifdef NDEBUG */
/* Debug mode */
#define LOG_DEBUG(msg) log_msg(DEBUG , ( msg))
#define LOG_WARN(msg) log_msg (WARN , (msg))
#define LOG_INFO(msg) log_msg (INFO , (msg))
#define LOG_ERROR(msg) log_msg(ERROR , ( msg))
#define LOG_FATAL(msg) log_msg(FATAL , ( msg))
#endif /* # ifdef NDEBUG */
#endif /* # ifndef LOG_H */
4.4.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎ˆ๎‡€๎ˆ— ๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚๎‚๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎ˆ‡๎‚๎‚Š ๎‚“๎‚– ๎ˆ‡๎‚‰๎‚๎‚๎‚Š ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚‰๎‚š๎‚…๎‚๎‡‚
๎‚™๎‚…๎‚˜๎‚‰ ๎‚˜๎‚“ ๎ˆ ๎‚“๎‚– ๎ˆ๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎ˆ๎‡€๎ˆ˜ ๎€ƒ๎‚๎‚ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚๎‚๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎ˆ‡๎‚๎‚Š ๎‚“๎‚– ๎ˆ‡๎‚‰๎‚๎‚๎‚Š ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹
๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎ˆ‡๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎ˆ๎‡€๎ˆ๎ˆ“ ๎€ƒ๎‚๎‚ ๎ˆ‡๎‚‰๎‚๎‚—๎‚‰๎†ฝ ๎ˆ‡๎‚‰๎‚๎‚๎‚Š ๎‚…๎‚’๎‚ˆ ๎ˆ‡๎‚‰๎‚’๎‚ˆ๎‚๎‚Š ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚–๎‚‰๎‚—๎‚๎‚ˆ๎‚‰ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰
๎Šฑ๎‚๎‚‰ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰ ๎ˆ‡๎‚๎‚Š๎†ฝ ๎ˆ‡๎‚๎‚Š๎‚ˆ๎‚‰๎‚Š ๎‚“๎‚– ๎ˆ‡๎‚๎‚Š๎‚’๎‚ˆ๎‚‰๎‚Š ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚˜๎‚“ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚…๎‚–๎‚‰ ๎‚–๎‚‰๎‚๎‚…๎‚˜๎‚‰๎‚ˆ๎‡€
4.5 Using the preprocessor operators # and ##
๎€–๎‚Œ๎‚‰ ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚“๎‚Š ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎ˆ‡ ๎‡ฆstringi๎Šฑcation ๎‚“๎‚– ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‡ง ๎‚“๎‚– ๎ˆ‡๎ˆ‡
๎‡ฆ๎‚‡๎‚“๎‚’๎‚‡๎‚…๎‚˜๎‚‰๎‚’๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‡ง ๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚๎‚œ๎‚˜๎‚™๎‚–๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚˜๎‚›๎‚“ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ๎‡€
RULE
16
RULE โ€” Do not use more than one of the preprocessor operators # and
## in the same expression
๎€‹๎‚˜ ๎‚๎‚— ๎‚…๎‚๎‚—๎‚“ ๎‚๎‚‘๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚’๎‚˜๎†ฝ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚˜๎‚›๎‚“ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚—๎†ฝ ๎‚˜๎‚“ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚… ๎‚‹๎‚“๎‚“๎‚ˆ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚Œ๎‚“๎‚› ๎‚๎‚˜ ๎‚›๎‚“๎‚–๎‚๎‚—๎†ฝ i.e.
๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚‰๎‚”๎‚— ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€
RULE
17
RULE โ€” Understand the macro replacement when using the preprocessor
operators # and ##
18 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
Bad example
#include <stdio .h>
...
#define MYPRINT(s) printf (#s)
#define TWO 2
int main( void )
{
MYPRINT (TWO); /* prints "TWO" */
return 1;
}
Good example
#include <stdio .h>
...
#define MYPRINT2(s) PRINT(s) /* Additional indirection to expand
"TWO" */
#define PRINT(s) printf (#s)
#define TWO 2
int main( void )
{
MYPRINT2 (TWO); /* prints "2" */
return 1;
}
4.5.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎ˆ๎‡€๎ˆ๎ˆ ๎€–๎‚Œ๎‚‰ ๎ˆ‡ ๎‚…๎‚’๎‚ˆ ๎ˆ‡๎ˆ‡ ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎ˆ๎‡€๎ˆ๎ˆ ๎€ƒ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚๎‚‘๎‚‘๎‚‰๎‚ˆ๎‚๎‚…๎‚˜๎‚‰๎‚๎‚ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚… ๎ˆ‡ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚๎‚‘๎‚‘๎‚‰๎‚ˆ๎‚๎‡‚
๎‚…๎‚˜๎‚‰๎‚๎‚ ๎‚†๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚… ๎ˆ‡๎ˆ‡ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎ˆ๎‡€๎ˆ๎ˆ‘ ๎€ƒ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚…๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎ˆ‡ ๎‚“๎‚– ๎ˆ‡๎ˆ‡ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚๎‚—
๎‚๎‚˜๎‚—๎‚‰๎‚๎‚Š ๎‚—๎‚™๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚“ ๎‚Š๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚– ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎†ฝ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚“๎‚’๎‚๎‚ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚…๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€’๎€”๎€‡๎ˆ๎ˆ”๎‡‚๎€… ๎€—๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚‡๎‚“๎‚’๎‚‡๎‚…๎‚˜๎‚‰๎‚’๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚“๎‚๎‚‰๎‚’๎‚— ๎‚“๎‚– ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚๎‚’๎‚‹ ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹๎‚๎‡‚
๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€ƒ๎‚’๎‚—๎‚๎€…๎ˆ˜๎ˆ๎‡ญ ๎€•๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ•๎‡€๎ˆ—๎‡€๎ˆ’๎‡€
๎‡ฌ๎€ƒ๎‚’๎‚—๎‚๎€…๎ˆ˜๎ˆ˜๎‡ญ ๎€•๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ•๎‡€๎ˆ๎ˆ๎‡€๎ˆ’๎‡€
4.6 Speci๎‚พc naming of macros
๎€‹๎‚˜ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚‰๎‚…๎‚—๎‚ ๎‚˜๎‚“ ๎‚ˆ๎‚๎‚—๎‚˜๎‚๎‚’๎‚‹๎‚™๎‚๎‚—๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š
๎‚—๎‚“๎‚‘๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚— ๎‚‘๎‚…๎‚ ๎‚–๎‚‰๎‚—๎‚‰๎‚‘๎‚†๎‚๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚๎‚๎‚—๎‡€
๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚–๎‚‘๎‚“๎‚–๎‚‰๎†ฝ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚’๎‚…๎‚‘๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚™๎‚”๎‚”๎‚‰๎‚– ๎‚‡๎‚…๎‚—๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚— ๎‚… ๎‚–๎‚๎‚—๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚—
๎‚˜๎‚“ ๎‚… ๎‚–๎‚‰๎‚…๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚– ๎‚‰๎‚š๎‚‰๎‚’ ๎‚… ๎‚–๎‚‰๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ ๎‚›๎‚“๎‚–๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚‡๎‚…๎‚’ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰
๎‚—๎‚™๎‚†๎‚—๎‚˜๎‚๎‚˜๎‚™๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚๎‚ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚–๎†ฝ ๎‚“๎‚– ๎‚‰๎‚š๎‚‰๎‚’ ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ
๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 19
RULE
18
RULE โ€” Macros must be speci๎‚พcally named
๎€–๎‚“ ๎‚‰๎‚…๎‚—๎‚๎‚๎‚ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚˜๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚— ๎‚Š๎‚–๎‚“๎‚‘ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚… ๎‚–๎‚‰๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚…๎‚’๎‡‚
๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎€… ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎†ฝ ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚”๎‚”๎‚‰๎‚–๎‚‡๎‚…๎‚—๎‚‰๎‡€ ๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚›๎‚“๎‚–๎‚ˆ๎‚— ๎‚‘๎‚…๎‚๎‡‚
๎‚๎‚’๎‚‹ ๎‚™๎‚” ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚—๎‚‰๎‚”๎‚…๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚‡๎‚“๎‚–๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‡Œ๎‡ ๎‡๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜
๎‚—๎‚˜๎‚…๎‚–๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰๎‚‘ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚‡๎‚“๎‚–๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎†ฝ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚๎‚— ๎‚๎‚— ๎‚… ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚“๎‚– ๎‚–๎‚‰๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ
๎‚’๎‚…๎‚‘๎‚‰๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚’๎‚…๎‚‘๎‚๎‚’๎‚‹ ๎‚–๎‚™๎‚๎‚‰ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚‰๎‚ˆ๎‡€
#define cte 0x7EU /* lower case */
#define _my_squared (a) ((a)*(a)) /* lower case and starts with _ */
Good example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚—๎‚Œ๎‚“๎‚›๎‚— ๎‚—๎‚™๎‚๎‚˜๎‚…๎‚†๎‚๎‚‰ ๎‚’๎‚…๎‚‘๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚—๎‡€
#define CTE 0x7EU
#define MY_SQUARED(a) ((a)*(a))
4.6.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ”๎‡€๎ˆ“ ๎€๎‚…๎‚‡๎‚–๎‚“ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚ˆ๎‚๎‚—๎‚˜๎‚๎‚’๎‚‡๎‚˜๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ”๎‡€๎ˆ” ๎€‹๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚ˆ๎‚๎‚—๎‚˜๎‚๎‚’๎‚‡๎‚˜ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚’๎‚…๎‚‘๎‚‰๎‚—๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎ˆ๎‡€๎ˆ๎†ฟ ๎€ƒ ๎ˆ‡๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰ ๎‚“๎‚– ๎ˆ‡๎‚™๎‚’๎‚ˆ๎‚‰๎‚Š ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚… ๎‚–๎‚‰๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚– ๎‚“๎‚– ๎‚–๎‚‰๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ
๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚’๎‚…๎‚‘๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎ˆ๎‡€๎ˆ‘๎†ฟ ๎€ƒ ๎‚–๎‚‰๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚– ๎‚“๎‚– ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎ˆ๎‡€๎ˆ“๎†ฟ ๎€ƒ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚…๎‚— ๎‚… ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€—๎‚—๎‚๎‚’๎‚‹ ๎€‹๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚–๎‚‰๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡ฌ๎‚–๎‚‰๎‚—๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‡ญ๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€†๎€…๎€Ž๎ˆ’๎ˆ–๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰ ๎‚“๎‚– ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰ ๎‚… ๎‚–๎‚‰๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€†๎‚๎‚–๎‡€ ๎ˆ“๎‡€๎ˆ” ๎€‹๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚“๎‚š๎‚‰๎‚–๎‚๎‚…๎‚”๎‚”๎‚๎‚’๎‚‹ ๎‚š๎‚๎‚—๎‚๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚˜๎‚๎‚”๎‚“๎‡‚
๎‚‹๎‚–๎‚…๎‚”๎‚Œ๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚™๎‚’๎‚…๎‚‘๎‚†๎‚๎‚‹๎‚™๎‚“๎‚™๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ๎ˆ‘๎‡‚๎€… ๎€—๎‚—๎‚‰ ๎‚š๎‚๎‚—๎‚™๎‚…๎‚๎‚๎‚ ๎‚ˆ๎‚๎‚—๎‚˜๎‚๎‚’๎‚‡๎‚˜ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚—๎‡€
4.7 A macro must not end with a semicolon
๎€๎‚…๎‚‡๎‚–๎‚“๎‚— ๎‚…๎‚–๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚‘๎‚…๎‚๎‚‰ ๎‚๎‚˜ ๎‚‰๎‚…๎‚—๎‚๎‚‰๎‚– ๎‚˜๎‚“ ๎‚–๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚–๎‚‰๎‚”๎‚‰๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚”๎‚…๎‚˜๎‚˜๎‚‰๎‚–๎‚’
๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚˜๎‚๎‚‘๎‚‰๎‚—๎‡€ ๎€™๎‚Œ๎‚‰๎‚’ ๎‚‰๎‚œ๎‚”๎‚…๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎†ฝ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚— ๎‚… ๎‚—๎‚‰๎‚‘๎‚๎‚‡๎‚“๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚“๎‚’๎‚‰ ๎‚๎‚—
๎‚…๎‚๎‚—๎‚“ ๎‚‰๎‚œ๎‚”๎‚…๎‚’๎‚ˆ๎‚‰๎‚ˆ๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚‡๎‚…๎‚’ ๎‚‡๎‚…๎‚™๎‚—๎‚‰ ๎‚… ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚˜๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚™๎‚’๎‚‰๎‚œ๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚ ๎Šท๎‚“๎‚›๎‡€
RULE
19
RULE โ€” Do not end a macro with a semicolon
๎€–๎‚Œ๎‚‰ ๎Šฑ๎‚’๎‚…๎‚ ๎‚—๎‚‰๎‚‘๎‚๎‚‡๎‚“๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚“๎‚‘๎‚๎‚˜๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‡€
20 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
Bad example
๎€–๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚…๎‚”๎‚”๎‚๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚™๎‚๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚‰๎‚’๎‚ˆ๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚—๎‚‰๎‚‘๎‚๎‚‡๎‚“๎‚๎‚“๎‚’๎‡€
#define SQUARED(n) (n) = (n) * (n); /* no semicolon at the end of a macro */
...
if (x >= 0)
SQUARED (x); /* conditional without brace */
else
x = -x;
...
๎‚“๎‚’ ๎‚‰๎‚œ๎‚”๎‚…๎‚’๎‚—๎‚๎‚“๎‚’๎†ฝ ๎‚›๎‚‰ ๎‚Œ๎‚…๎‚š๎‚‰๎†ฟ
#define SQUARED(n) (n) = (n) * (n);
...
if (x >= 0)
x= x * x;
; /* empty statement */
else /* parsing error before the else */
x = -x;
...
Good example
๎€–๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚๎‚— ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ๎†ฟ
#define SQUARED(n) (n) = (n) * (n)
/* ... */
if (x >= 0)
{
SQUARED (x);
}
else
{
x = -x;
}
๎‚“๎‚’ ๎‚‰๎‚œ๎‚”๎‚…๎‚’๎‚—๎‚๎‚“๎‚’๎†ฝ ๎‚›๎‚‰ ๎‚Œ๎‚…๎‚š๎‚‰๎†ฟ
#define SQUARED(n) (n) = (n) * (n)
/* ... */
if (x >= 0)
{
x = x * x;
}
else
{
x = -x;
}
4.7.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€’๎€”๎€‡๎ˆ๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚“๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚—๎‚‰๎‚‘๎‚๎‚‡๎‚“๎‚๎‚“๎‚’๎‡€
4.8 Give preference to static inline functions in
โ€œfunction typeโ€ macros
Inline ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚†๎‚‰๎‚‰๎‚’ ๎‚…๎‚š๎‚…๎‚๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎€…๎ˆ˜๎ˆ˜ ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 21
Information
๎€ƒ๎‚— ๎‚‘๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ๎ˆ’๎‡€๎ˆ”๎†ฝ inline ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚…๎‚๎‚—๎‚“ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚…๎‚— static๎‡€
๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚… static inline ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚“ ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‡Œ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚๎‚”๎‚‰๎‡ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚— ๎‚”๎‚–๎‚‰๎‚š๎‚‰๎‚’๎‚˜๎‚— ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚๎‚’
๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚“๎‚Š ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚›๎‚Œ๎‚‰๎‚’ inlining ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚—๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‰๎‚…๎‚—๎‚๎‚‰๎‚– ๎‚˜๎‚“ ๎‚–๎‚‰๎‚…๎‚ˆ๎‡€
Information
๎€‹๎‚˜ ๎‚๎‚— ๎‚๎‚‘๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚’๎‚“๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚™๎‚’๎‚™๎‚—๎‚‰๎‚ˆ static inline ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚…๎‚ ๎‚‡๎‚…๎‚™๎‚—๎‚‰ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚˜๎‚“
๎‚†๎‚‰ ๎‚๎‚—๎‚—๎‚™๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚“๎‚– ๎‚—๎‚“๎‚‘๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚—๎‡€ ๎€‹๎‚’ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚–๎†ฝ ๎‚—๎‚“๎‚‘๎‚‰ ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š
๎‚˜๎‚Œ๎‚‰ ๎€…๎„‘๎„†๎„“๎„Œ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚๎‚—๎‚—๎‚™๎‚‰ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‡๎‚…๎‚—๎‚‰๎†ฝ ๎‚™๎‚’๎‚๎‚๎‚๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎„Œ๎„ˆ๎„ˆ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‡‚
๎‚‰๎‚’๎‚‡๎‚‰ ๎‚๎‚’ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‚“๎‚‡๎‚‡๎‚™๎‚–๎‚— ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ -Wunused-function ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚—
๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‡‚๎‚˜๎‚๎‚‘๎‚‰๎†ฝ ๎‚‰๎‚๎‚˜๎‚Œ๎‚‰๎‚– ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚“๎‚– ๎‚š๎‚๎‚… ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— -Wall๎‡€ ๎€™๎‚Œ๎‚‰๎‚’
๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚–๎‚‰๎‚…๎‚๎‚๎‚ ๎‚‡๎‚…๎‚’๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚๎‚‰๎‚˜๎‚‰๎‚ˆ ๎‡ฆ๎‚๎‚’ ๎‚… ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎‡ง๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–
๎‚‘๎‚…๎‚ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚‰๎‚œ๎‚˜๎‚‰๎‚’๎‚—๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚“ ๎‚—๎‚๎‚๎‚‰๎‚’๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚—๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚—
๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚๎‚‰๎‚…๎‚–๎‚๎‚ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚Ž๎‚™๎‚—๎‚˜๎‚๎Šฑ๎‚‰๎‚ˆ๎‡€
RECO
20
RECOMMENDATION โ€” Use static inline functions instead of
multi-statement macros
๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚†๎‚“๎‚š๎‚‰ ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ ๎‚–๎‚™๎‚๎‚‰๎‚—๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚๎‚‘๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚…๎‚ˆ๎‚ˆ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚— ๎‚›๎‚Œ๎‚“๎‚—๎‚‰
๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚— ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚–๎‚๎‚—๎‚ ๎‚“๎‚Š ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚๎‚— ๎‚˜๎‚“๎‚“
๎‚‹๎‚–๎‚‰๎‚…๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚…๎‚ˆ๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‡๎‚…๎‚’ ๎‚—๎‚™๎Šฎ๎‚‰๎‚– ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚๎‚— ๎‚๎‚๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚”๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚‡๎‚‰๎‡€
RULE
21
RULE โ€” The replacement of a developer-de๎‚พned macro must not create a
function
4.8.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€†๎‚๎‚–๎‡€ ๎ˆ“๎‡€๎ˆ˜๎†ฟ ๎€ƒ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚”๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰ ๎‚˜๎‚“ ๎‚… function-like macro ๎‚›๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰๎‚
๎‚…๎‚–๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰๎‚…๎‚†๎‚๎‚‰๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€’๎€”๎€‡๎ˆ๎ˆ๎‡‚๎€… ๎€’๎‚–๎‚‰๎‚Š๎‚‰๎‚– ๎‚๎‚’๎‚๎‚๎‚’๎‚‰ ๎‚“๎‚– ๎‚—๎‚˜๎‚…๎‚˜๎‚๎‚‡ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚“ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡‚๎‚๎‚๎‚๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚—๎‡€
4.9 Multi-statement macros
๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚‡๎‚…๎‚’ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚™๎‚’๎‚‰๎‚œ๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€ ๎€‹๎‚’๎‚ˆ๎‚‰๎‚‰๎‚ˆ๎†ฝ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‡‚
๎‚๎‚’๎‚‹ ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚›๎‚๎‚˜๎‚Œ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‡Œ๎‡›๎‡ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚–
๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚… ๎‚๎‚๎‚’๎‚‰ ๎‚†๎‚–๎‚‰๎‚…๎‚ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚๎‚’๎‚—๎‚‰๎‚–๎‚˜๎‚‰๎‚ˆ๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚–๎‚‰๎‚…๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚…๎‚’ ๎‚…๎‚๎‚—๎‚“ ๎‚†๎‚‰
๎‚… ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚—๎‡€
22 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎€–๎‚Œ๎‚‰ ๎‚‹๎‚–๎‚“๎‚™๎‚”๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚๎‚’ ๎‚… do { ๎‡€๎‡€๎‡€ } while(0) ๎‚๎‚“๎‚“๎‚” ๎‚๎‚๎‚‘๎‚๎‚˜๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚๎‚๎‚˜๎‚๎‚‰๎‚— ๎‚“๎‚Š ๎‚™๎‚’๎‚‰๎‚œ๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ
๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€ ๎€ƒ do { ๎‡€๎‡€๎‡€ } while(0) ๎‚๎‚“๎‚“๎‚” ๎‚๎‚— ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚‰๎‚ˆ ๎‚‰๎‚œ๎‚…๎‚‡๎‚˜๎‚๎‚ ๎‚“๎‚’๎‚‡๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚”๎‚–๎‚‰๎‚š๎‚‰๎‚’๎‚˜๎‚— ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰
๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚ ๎Šท๎‚“๎‚› ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚†๎‚ ๎‚‹๎‚–๎‚“๎‚™๎‚”๎‚๎‚’๎‚‹ ๎‚…๎‚๎‚ ๎‚๎‚˜๎‚— ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚๎‚’ ๎‚… ๎‚๎‚“๎‚“๎‚”๎‡€
RULE
22
RULE โ€” Macros containing multiple statements must use a
do { ... } while(0) loop for their de๎‚พnition
Bad example
๎€–๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚™๎‚๎‚‰๎‡€
#define HALF_SUM(a,b,c,d) \
(a) = ((c) + (d)) / 2; \
(b) = ((c) - (d)) / 2
/* leads to a different behaviour to the one required with a call in a conditional
statement without braces */
if(c > d)
HALF_SUM (a, b, c, d);
else
/* ... */
๎€‡๎‚š๎‚‰๎‚’ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚›๎‚‰๎‚–๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚†๎‚–๎‚…๎‚‡๎‚‰๎‚—๎†ฟ
#define HALF_SUM(a,b,c,d) { (a) = ((c) + (d)) / 2; (b) = ((c) - (d)) / 2 }
๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚๎‚— ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚†๎‚‰๎‚‡๎‚…๎‚™๎‚—๎‚‰
๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚…๎‚‡๎‚ ๎‚“๎‚Š ๎‚†๎‚–๎‚…๎‚‡๎‚‰๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‡Œ;๎‡ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎†ฟ
if(c>d)
{ (a)=((c) + (d)) / 2; (b) = ((c) - (d)) / 2 };
else
/* ... */
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚๎‚— ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚… do-while(0) ๎‚๎‚“๎‚“๎‚” ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‡‚
๎‚˜๎‚™๎‚–๎‚‰๎‡€
#define HALF_SUM(a, b, c, d) \
do { \
(a) = ((c) + (d)) / 2; \
(b) = ((c) - (d)) / 2; \
} while (0)
4.9.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€’๎€”๎€‡๎ˆ๎ˆ๎‡‚๎€… ๎€™๎‚–๎‚…๎‚” ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚— ๎‚๎‚’ ๎‚… ๎‚ˆ๎‚“๎‡‚๎‚›๎‚Œ๎‚๎‚๎‚‰ ๎‚๎‚“๎‚“๎‚”๎‡€
4.10 Arguments and parameters of a macro
๎€†๎‚™๎‚–๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚–๎†ฝ ๎‚—๎‚๎‚ˆ๎‚‰ ๎‚‰๎Šฎ๎‚‰๎‚‡๎‚˜๎‚— ๎‚’๎‚“๎‚˜ ๎‚‰๎‚œ๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–
๎‚‘๎‚…๎‚ ๎‚“๎‚‡๎‚‡๎‚™๎‚– ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚”๎‚–๎‚“๎‚˜๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ๎‡€ ๎€’๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚‰๎‚ˆ
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 23
๎‚…๎‚–๎‚“๎‚™๎‚’๎‚ˆ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‡€
RULE
23
RULE โ€” Mandatory parentheses around the parameters used in the body
of a macro
๎€–๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚Š ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚†๎‚‰ ๎‚‰๎‚’๎‚‡๎‚๎‚“๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚— ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚™๎‚—๎‚‰๎‚ˆ๎†ฝ ๎‚๎‚’
๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚–๎‚š๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚—๎‚๎‚–๎‚‰๎‚ˆ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚“๎‚Š ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚—๎‡€
๎€‹๎‚’ ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚†๎‚‰๎‚—๎‚˜ ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚๎‚’ ๎‚…๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚–๎‚“๎‚…๎‚ˆ๎‚‰๎‚—๎‚˜ ๎‚—๎‚‰๎‚’๎‚—๎‚‰๎‡€
๎€ƒ๎‚”๎‚…๎‚–๎‚˜ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ˆ๎‚‰ ๎‚‰๎Šฎ๎‚‰๎‚‡๎‚˜๎‚—๎†ฝ ๎‚‰๎‚š๎‚‰๎‚’ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚…๎‚’ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚— ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚Š๎‚“๎‚– ๎‚… ๎‚‹๎‚๎‚š๎‚‰๎‚’
๎‚๎‚’๎‚”๎‚™๎‚˜๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚…๎‚๎‡€
RECO
24
RECOMMENDATION โ€” Arguments of a macro carrying out an operation
should be avoided
๎€๎‚“๎‚–๎‚‰๎‚“๎‚š๎‚‰๎‚–๎†ฝ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚–๎‚–๎‚๎‚‰๎‚ˆ ๎‚“๎‚™๎‚˜ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚๎‚‰๎‚…๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚… ๎‚—๎‚๎‚ˆ๎‚‰ ๎‚‰๎Šฎ๎‚‰๎‚‡๎‚˜ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰
๎‚—๎‚‰๎‚’๎‚—๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‡๎‚…๎‚’ ๎‚…๎‚๎‚—๎‚“ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚™๎‚’๎‚‰๎‚œ๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚–๎‚— ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š
๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚“๎‚– ๎‚‰๎‚š๎‚‰๎‚’ ๎‚˜๎‚“ ๎‚’๎‚“ ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚˜ ๎‚…๎‚๎‚๎‡€
RULE
25
RULE โ€” Arguments in a macro must not contain side effects
๎€๎‚…๎‚‡๎‚–๎‚“ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚—๎‚๎‚ˆ๎‚‰ ๎‚‰๎Šฎ๎‚‰๎‚‡๎‚˜๎‚— ๎‚‡๎‚…๎‚’ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚™๎‚’๎‚›๎‚…๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎€ˆ๎‚๎‚’๎‚…๎‚๎‚๎‚๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚— ๎‡ฆ#define๎†ฝ #ifdef ๎‡€๎‡€๎‡€๎‡ง ๎‚๎‚’ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚๎‚‰๎‚…๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎‡‚
๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚…๎‚’๎‚ˆ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚…๎‚š๎‚“๎‚๎‚ˆ๎‚‰๎‚ˆ๎‡€
RULE
26
RULE โ€” Do not use preprocessor directives in macro arguments
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚›๎‚๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚’๎‚‰ ๎‚‰๎‚œ๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚™๎‚”๎‚“๎‚’ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚๎‚“๎‚’๎‡€
#define ABS(x) (x >= 0 ? x : -x)
a = c + ABS(a - b) + d;
/* result : a = c + (a - b >= 0 ? a - b : -a -b) + d */
m=ABS(n++) ;
/* additional increment of n: m = ((n++ < 0) ? - n++ : n++) */
Good example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚— ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚›๎‚๎‚˜๎‚Œ ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚— ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚ˆ ๎‚…๎‚–๎‚“๎‚™๎‚’๎‚ˆ ๎‚๎‚˜๎‚—
๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‡€
#define ABS(x) (((x) >= 0) ? (x) : -(x))
a = c + ABS(a - b) + d;
24 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
/* correct result : a = c + (((a - b) >= 0) ? (a - b) : -(a - b)) + d */
p=n++;
m=ABS(p);
/* only one increment of n: m = (((p) < 0) ? - (p) : (p)); */
4.10.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎ˆ๎‡€๎ˆ–๎†ฟ ๎€‡๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚”๎‚…๎‚’๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰
๎‚‰๎‚’๎‚‡๎‚๎‚“๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€’๎€”๎€‡๎ˆ๎ˆ๎‡‚๎€… ๎€—๎‚—๎‚‰ ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚๎‚— ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚— ๎‚…๎‚–๎‚“๎‚™๎‚’๎‚ˆ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚’๎‚…๎‚‘๎‚‰๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€’๎€”๎€‡๎ˆ๎ˆ‘๎‡‚๎€… ๎€๎‚…๎‚‡๎‚–๎‚“ ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚๎‚—๎‚˜๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚๎‚ž๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‡๎€š๎€’๎ˆ’๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚“๎‚Š ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚“๎‚– ๎‚—๎‚๎‚ˆ๎‚‰ ๎‚‰๎Šฎ๎‚‰๎‚‡๎‚˜๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰๎‡€ ๎€’๎€”๎€‡๎ˆ’๎ˆ๎‡‚๎€… ๎€ƒ๎‚š๎‚“๎‚๎‚ˆ ๎‚—๎‚๎‚ˆ๎‚‰ ๎‚‰๎Šฎ๎‚‰๎‚‡๎‚˜๎‚— ๎‚๎‚’ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚˜๎‚“ ๎‚™๎‚’๎‚—๎‚…๎‚Š๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰๎‡€ ๎€’๎€”๎€‡๎ˆ’๎ˆ‘๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚— ๎‚๎‚’ ๎‚๎‚’๎‚š๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡‚๎‚๎‚๎‚๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€’๎€”๎€‡๎ˆ๎ˆ‘๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰ ๎‚™๎‚’๎‚—๎‚…๎‚Š๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚—๎‡€
4.11 Using the #undef directive
๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ #undef ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚Š๎‚–๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚˜๎‚๎‚ ๎‚๎‚‰๎‚…๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚‡๎‚“๎‚’๎‚Š๎‚™๎‚—๎‚๎‚“๎‚’๎‡€ ๎€‹๎‚’๎‚…๎‚ˆ๎‚š๎‚‰๎‚–๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚๎†ฝ ๎‚๎‚˜๎‚— ๎‚™๎‚—๎‚‰ ๎‚‡๎‚…๎‚’ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚…๎‚
๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚ˆ๎‚‰๎‚๎‚‰๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚— ๎‚๎‚’ ๎‚Š๎‚…๎‚‡๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚๎‚๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚‘๎‚†๎‚“๎‚ ๎‚›๎‚Œ๎‚“๎‚—๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’
๎‚๎‚— ๎‚ˆ๎‚‰๎‚๎‚‰๎‚˜๎‚‰๎‚ˆ๎‡€ ๎€‹๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚‰๎‚š๎‚‰๎‚– ๎‚†๎‚‰ ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚๎‚‰๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚—๎‚๎‚‘๎‚†๎‚“๎‚๎‡€ ๎€‹๎‚Š ๎‚˜๎‚Œ๎‚‰
๎‚”๎‚™๎‚–๎‚”๎‚“๎‚—๎‚‰ ๎‚“๎‚Š ๎‚ˆ๎‚‰๎‚๎‚‰๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚‘๎‚†๎‚“๎‚ ๎‚๎‚— ๎‚˜๎‚“ ๎‚๎‚๎‚‘๎‚๎‚˜ ๎‚๎‚˜๎‚— ๎‚—๎‚‡๎‚“๎‚”๎‚‰๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚”๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚…๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚ ๎‚›๎‚Œ๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‡๎‚“๎‚”๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎‚—๎‚๎‚‘๎‚†๎‚“๎‚ ๎‚’๎‚‰๎‚‰๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚๎‚๎‚‘๎‚๎‚˜๎‚‰๎‚ˆ๎‡€
๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š #undef ๎‚‘๎‚…๎‚ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚๎‚—๎‚ ๎‚“๎‚Š ๎‚… ๎‚‡๎‚๎‚…๎‚—๎‚Œ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚‡๎‚Œ๎‚“๎‚—๎‚‰๎‚’ ๎‚Š๎‚“๎‚– ๎‚… ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚—๎‚๎‚‘๎‚†๎‚“๎‚๎‡€
๎€–๎‚Œ๎‚‰ ๎‚—๎‚๎‚‘๎‚†๎‚“๎‚ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚†๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚”๎‚–๎‚‰๎‚š๎‚‰๎‚’๎‚˜ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‡๎‚๎‚…๎‚—๎‚Œ๎‡€
RULE
27
RULE โ€” The #undef directive should not be used
4.11.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎ˆ๎‡€๎ˆ” ๎ˆ‡๎‚™๎‚’๎‚ˆ๎‚‰๎‚Š ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
4.12 Trigraph and double question mark
๎€–๎‚›๎‚“ ๎‚—๎‚™๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚š๎‚‰ ๎‚•๎‚™๎‚‰๎‚—๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚…๎‚–๎‚๎‚— ๎‚๎‚’ ๎€… ๎‚‘๎‚…๎‚–๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚‰๎‚‹๎‚๎‚’๎‚’๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚… ๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚˜๎‚–๎‚๎‚‹๎‚–๎‚…๎‚”๎‚Œ๎‡€
๎€ˆ๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚–๎‚๎‚‹๎‚–๎‚…๎‚”๎‚Œ ๎‡Œ??-๎‡ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‡Œ๎Š‘๎‡๎‡€ ๎€ƒ๎‚๎‚ ๎‚˜๎‚–๎‚๎‚‹๎‚–๎‚…๎‚”๎‚Œ๎‚— ๎‚›๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚ˆ ๎‚”๎‚–๎‚๎‚“๎‚–
๎‚˜๎‚“ ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚—๎†ฝ ๎‚–๎‚‰๎‚‹๎‚…๎‚–๎‚ˆ๎‚๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚–๎‚๎‚‹๎‚–๎‚…๎‚”๎‚Œ๎‡€ ๎€–๎‚Œ๎‚‰๎‚ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚“๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚†๎‚‰
๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 25
RULE
28
RULE โ€” Do not use trigraphs
๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚‡๎‚“๎‚’๎‚Š๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚˜๎‚–๎‚๎‚‹๎‚–๎‚…๎‚”๎‚Œ๎†ฝ ๎‚…๎‚๎‚ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎†ฝ ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹๎‚— ๎‚…๎‚’๎‚ˆ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚๎‚๎‚˜๎‚‰๎‚–๎‚…๎‚๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜
๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚˜๎‚›๎‚“ ๎‚—๎‚™๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚š๎‚‰ ๎‚•๎‚™๎‚‰๎‚—๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚…๎‚–๎‚๎‚—๎‡€
RECO
29
RECOMMENDATION โ€” Successive question marks should not be used
๎€–๎‚Œ๎‚๎‚— ๎‚–๎‚™๎‚๎‚‰ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚— ๎‚˜๎‚“ ๎‚…๎‚๎‚ ๎‚”๎‚…๎‚–๎‚˜๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚…๎‚๎‚—๎‚“ ๎‚˜๎‚“ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎‡€
Information
๎€–๎‚Œ๎‚‰ -Wtrigraphs ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚—๎‚—๎‚™๎‚‰๎‚— ๎‚…๎‚’ ๎‚…๎‚๎‚‰๎‚–๎‚˜ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚… ๎‚˜๎‚–๎‚๎‚‹๎‚–๎‚…๎‚”๎‚Œ ๎‚๎‚— ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ๎‡€
Information
๎€„๎‚ ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜๎†ฝ ๎‚˜๎‚–๎‚๎‚‹๎‚–๎‚…๎‚”๎‚Œ๎‚— ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚๎‚—๎‚…๎‚†๎‚๎‚‰๎‚ˆ ๎‚๎‚’ ๎„Œ๎„ˆ๎„ˆ๎‡€
4.12.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ“๎‡€๎ˆ‘ ๎€–๎‚–๎‚๎‚‹๎‚–๎‚…๎‚”๎‚Œ๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€’๎€”๎€‡๎ˆ๎ˆ–๎‡‚๎€… ๎€ƒ๎‚š๎‚“๎‚๎‚ˆ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚–๎‚‰๎‚”๎‚‰๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚•๎‚™๎‚‰๎‚—๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚…๎‚–๎‚๎‚—๎‡€
26 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
5
Compilation
๎€…๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚…๎‚’ ๎‚๎‚‘๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚’๎‚˜ ๎‚—๎‚˜๎‚‰๎‚” ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚๎‚’๎‚‹ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎‚๎‚˜ ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚’๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’
๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚›๎‚–๎‚๎‚˜๎‚˜๎‚‰๎‚’ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚›๎‚๎‚๎‚ ๎‚…๎‚‡๎‚˜๎‚™๎‚…๎‚๎‚๎‚ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚‰ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚’๎‚ˆ ๎‚™๎‚—๎‚‰๎‚–๎‡‹๎‚— ๎‚‘๎‚…๎‚‡๎‚Œ๎‚๎‚’๎‚‰๎‡€
๎€๎‚…๎‚—๎‚˜๎‚‰๎‚–๎‚๎‚’๎‚‹ ๎‚๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚™๎‚— ๎‚”๎‚–๎‚๎‚‘๎‚“๎‚–๎‚ˆ๎‚๎‚…๎‚๎‡€ ๎€๎‚“๎‚–๎‚‰๎‚“๎‚š๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚๎‚— ๎‚… ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚“๎‚™๎‚— ๎‚…๎‚๎‚๎‚ ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹
๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚“๎‚– ๎‚ˆ๎‚…๎‚’๎‚‹๎‚‰๎‚–๎‚“๎‚™๎‚— ๎‚™๎‚—๎‚‰๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚๎‚˜ ๎‚…๎‚๎‚—๎‚“ ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚๎‚๎‚ ๎‚”๎‚–๎‚“๎‚š๎‚๎‚ˆ๎‚‰๎‚— ๎‚Œ๎‚…๎‚–๎‚ˆ๎‚‰๎‚’๎‚๎‚’๎‚‹ ๎‚Š๎‚‰๎‚…๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚‡๎‚…๎‚”๎‚…๎‚†๎‚๎‚‰
๎‚“๎‚Š ๎‚—๎‚˜๎‚–๎‚“๎‚’๎‚‹๎‚๎‚ ๎‚๎‚‘๎‚”๎‚–๎‚“๎‚š๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰๎‡€
Warning
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰ ๎‚…๎‚’๎‚ˆ a fortiori ๎‚˜๎‚Œ๎‚๎‚— ๎‚‡๎‚Œ๎‚…๎‚”๎‚˜๎‚‰๎‚–๎†ฝ ๎‚˜๎‚›๎‚“ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚Š๎‚–๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚˜๎‚๎‚ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚๎‚๎‚๎‚™๎‚—๎‚˜๎‚–๎‚…๎‡‚
๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚™๎‚–๎‚”๎‚“๎‚—๎‚‰๎‚—๎†ฟ ๎„Œ๎„ˆ๎„ˆ ๎‡ฌ๎€‰๎‚‡๎‚‡๎€”๎‚‰๎‚Š๎‡ญ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ ๎‡ฌ๎€…๎‚๎‚…๎‚’๎‚‹๎€”๎‚‰๎‚Š๎‡ญ๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚‡๎‚Œ๎‚“๎‚๎‚‡๎‚‰ ๎‚๎‚— ๎‚๎‚…๎‚–๎‚‹๎‚‰๎‚๎‚ ๎‚‘๎‚“๎‚˜๎‚๎‚š๎‚…๎‚˜๎‚‰๎‚ˆ
๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚”๎‚™๎‚๎‚…๎‚–๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚—๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚“๎‚’ ๎‚˜๎‚“๎‚” ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚“๎‚”๎‚‰๎‚’ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰๎‡€ ๎€‹๎‚˜ ๎‚ˆ๎‚“๎‚‰๎‚—๎‚’๎‡‹๎‚˜
๎‚‘๎‚‰๎‚…๎‚’ ๎‚๎‚’ ๎‚…๎‚’๎‚ ๎‚›๎‚…๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰ ๎‚“๎‚’๎‚๎‚ ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚— ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚“๎‚’๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚˜๎‚›๎‚“ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‡‚
๎‚‰๎‚–๎‚—๎‡€ ๎€ƒ๎‚’๎‚ ๎‚…๎‚๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚˜๎‚๎‚š๎‚‰ ๎‚‘๎‚…๎‚ ๎‚†๎‚‰ ๎‚”๎‚–๎‚“๎‚”๎‚“๎‚—๎‚‰๎‚ˆ ๎‚†๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚”๎‚‰๎‚– ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚˜๎‚–๎‚…๎‚’๎‚—๎‚”๎‚“๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚“๎‚™๎‚—
๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰ ๎‚Œ๎‚๎‚‘๎‚—๎‚‰๎‚๎‚Š๎‡€
5.1 Mastery of the compilation phase
๎€…๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‚“๎Šฎ๎‚‰๎‚– ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹ ๎‚๎‚‰๎‚š๎‚‰๎‚๎‚— ๎‚‘๎‚‰๎‚…๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚”๎‚‰๎‚– ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚–๎‚๎‚—๎‚๎‚ ๎‚‡๎‚“๎‚’๎‡‚
๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚—๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜ ๎‚๎‚‰๎‚š๎‚‰๎‚ ๎‚๎‚— ๎‚™๎‚—๎‚™๎‚…๎‚๎‚๎‚ ๎‚–๎‚…๎‚˜๎‚Œ๎‚‰๎‚– ๎‚๎‚“๎‚› ๎‚…๎‚’๎‚ˆ
๎‚–๎‚‰๎‚”๎‚“๎‚–๎‚˜๎‚— ๎‚Š๎‚‰๎‚› ๎‚†๎‚…๎‚ˆ ๎‚”๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚‡๎‚‰๎‚—๎‡€ ๎€‹๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚™๎‚— ๎‚๎‚’๎‚—๎‚™๎Šฒ๎‚‡๎‚๎‚‰๎‚’๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚…๎‚—๎‚‰๎‚ˆ๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚–๎‚‰๎‚•๎‚™๎‚๎‚–๎‚‰๎‚— ๎‚‘๎‚…๎‚๎‚๎‚’๎‚‹
๎‚™๎‚—๎‚‰๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‡€ ๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚–๎‚‘๎‚“๎‚–๎‚‰๎†ฝ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎†ฝ ๎‚—๎‚“๎‚‘๎‚‰ ๎‚ˆ๎‚‰๎‡‚
๎‚Š๎‚…๎‚™๎‚๎‚˜ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚–๎‚— ๎‚‘๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚“๎‚’๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚˜๎‚“ ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚–๎‡€ ๎€‡๎‚š๎‚‰๎‚’ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚‰๎‚‘๎‚๎‚˜๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚๎‚’๎‚‹
๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚˜๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‡€ ๎€‹๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚™๎‚— ๎‚“๎‚Š ๎‚”๎‚–๎‚๎‚‘๎‚…๎‚–๎‚ ๎‚๎‚‘๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚’๎‚‡๎‚‰ ๎‚˜๎‚“ ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚‰๎‚๎‚ ๎‚๎‚’๎‚“๎‚› ๎‚˜๎‚Œ๎‚‰
๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚๎‚’ ๎‚™๎‚—๎‚‰๎†ฝ ๎‚๎‚˜๎‚— ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚…๎‚— ๎‚›๎‚‰๎‚๎‚ ๎‚…๎‚— ๎‚…๎‚๎‚ ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰๎‚ˆ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚๎‚ˆ๎‚‰๎‚…๎‚๎‚๎‚ ๎‚†๎‚…๎‚‡๎‚๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚Ž๎‚™๎‚—๎‚˜๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
RULE
30
RULE โ€” Precisely de๎‚พne compilation options
๎€‘๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚๎‚’๎‚‹ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚‰๎‚๎‚ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚›๎‚Œ๎‚“๎‚๎‚‰ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰๎‚—๎‡€
๎€–๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚๎‚’ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚– ๎‚…๎‚‡๎‚‡๎‚™๎‚–๎‚…๎‚˜๎‚‰๎‚๎‚ ๎‚‰๎‚—๎‚˜๎‚…๎‚†๎‚๎‚๎‚—๎‚Œ๎†ฟ
n
๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚๎‚’ ๎‚™๎‚—๎‚‰ ๎‡ฆ๎‚Š๎‚“๎‚– ๎‚๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚‡๎‚‰ ๎€…๎ˆ˜๎ˆ˜ ๎‚“๎‚– ๎€…๎ˆ˜๎ˆ๎‡ง๎†พ
n
๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚๎‚’ ๎‚™๎‚—๎‚‰๎†พ
n
๎‚˜๎‚Œ๎‚‰ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹ ๎‚๎‚‰๎‚š๎‚‰๎‚ ๎‡ฆ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ -Wextra ๎‚Š๎‚“๎‚– ๎„Œ๎„ˆ๎„ˆ๎‡ง๎†พ
n
๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚—๎‚๎‚‘๎‚†๎‚“๎‚๎‚— ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’๎‚— ๎‡ฆ๎‚Š๎‚“๎‚– ๎‚๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚‡๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚’๎‚‹ NDEBUG ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚๎‚’๎‚‹
๎‚๎‚’ ๎‚–๎‚‰๎‚๎‚‰๎‚…๎‚—๎‚‰ ๎‚‘๎‚“๎‚ˆ๎‚‰๎‡ง๎‡€
๎€๎‚“๎‚–๎‚‰๎‚“๎‚š๎‚‰๎‚–๎†ฝ ๎‚…๎‚’๎‚ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚“๎‚– ๎‚๎‚๎‚’๎‚๎‚‰๎‚– ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚Š๎‚™๎‚๎‚๎‚ ๎‚…๎‚›๎‚…๎‚–๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚‰๎‡‚
๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰๎‚— ๎‚–๎‚‰๎‚‹๎‚…๎‚–๎‚ˆ๎‚๎‚’๎‚‹ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚…๎‚†๎‚๎‚‰ ๎‚“๎‚– ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 27
RECO
31
RECOMMENDATION โ€” Master actions performed by the compiler and the
linker
๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚‘๎‚™๎‚—๎‚˜ ๎‚๎‚’๎‚“๎‚› ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜ ๎‚…๎‚๎‚ ๎‚…๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚˜๎‚‰๎‚‘๎‚‘๎‚๎‚’๎‚‹ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰๎‚ˆ ๎‚‡๎‚“๎‚‘๎‡‚
๎‚”๎‚๎‚๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚๎‚๎‚’๎‚๎‚‰๎‚– ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚๎‚’๎‚‹ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚˜๎‚“ ๎‚ˆ๎‚“ ๎‚›๎‚๎‚˜๎‚Œ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚ž๎‚…๎‡‚
๎‚˜๎‚๎‚“๎‚’๎‡€
Warning
๎€‹๎‚’ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— -fno-strict-overflow๎†ฝ
-fwrapv๎†ฝ -fwrapv-pointer๎†ฝ -fno-delete-null-pointer-checks ๎‚“๎‚–
-fno-strict-aliasing ๎‚๎‚— ๎‚‘๎‚“๎‚—๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚‘๎‚‰ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚๎‚š๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚–๎‚๎‚—๎‚๎‚ ๎‚™๎‚—๎‚…๎‚‹๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎€…
๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰๎‡€
๎€—๎‚—๎‚๎‚’๎‚‹ ๎‚… ๎‚†๎‚™๎‚๎‚๎‚ˆ ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚๎‚๎‚๎‚‰ make๎†ฝ CMake ๎‚“๎‚– Meson ๎‚Š๎‚…๎‚‡๎‚๎‚๎‚๎‚˜๎‚…๎‚˜๎‚‰๎‚— ๎‚‘๎‚…๎‚’๎‚…๎‚‹๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‡‚
๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚๎‚…๎‚˜๎‚˜๎‚‰๎‚– ๎‚‘๎‚…๎‚ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚๎‚๎‚ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚๎‚ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰๎‚— ๎‚˜๎‚“ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‡€
GOOD
PRACTICE
32
GOOD PRACTICE โ€” Make use of build automation software
5.1.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€•๎‚™๎‚†๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ“๎‡€๎ˆ‘๎‡€ ๎€—๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€๎€•๎€…๎ˆ๎ˆ•๎‡‚๎€… ๎€„๎‚‰๎‚›๎‚…๎‚–๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€’๎€”๎€‡๎ˆ๎ˆ’๎‡‚๎€… ๎€—๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚”๎‚–๎‚‰๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚— ๎‚˜๎‚“ ๎‚˜๎‚‰๎‚—๎‚˜ ๎‚Š๎‚“๎‚– ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ ๎‚Š๎‚‰๎‚…๎‚˜๎‚™๎‚–๎‚‰๎‚—๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ๎ˆ“ ๎€…๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎€”๎‚‰๎‚‘๎‚“๎‚š๎‚…๎‚ ๎‚“๎‚Š ๎€…๎‚“๎‚ˆ๎‚‰ ๎‚˜๎‚“ ๎€…๎‚๎‚‰๎‚…๎‚– ๎€„๎‚™๎Šฎ๎‚‰๎‚–๎‚—๎‡€
5.2 Compilation without errors nor warnings
๎€๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚— ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚…๎‚’๎‚ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚’๎‚“๎‚– ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹ ๎‚๎‚— ๎‚…๎‚’ ๎‚‰๎‚œ๎‚‡๎‚‰๎‚๎‚๎‚‰๎‚’๎‚˜ ๎‚›๎‚…๎‚ ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚‡๎‚–๎‚‰๎‚…๎‚—๎‚‰
๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚๎‚—๎‚ ๎‚“๎‚Š ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚“๎‚– ๎‚–๎‚๎‚—๎‚๎‚ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚–๎‚‰๎‚‘๎‚…๎‚๎‚’๎‚๎‚’๎‚‹ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚†๎‚…๎‚—๎‚‰๎‡€ ๎€‘๎‚†๎‚š๎‚๎‚“๎‚™๎‚—๎‚๎‚๎†ฝ ๎‚˜๎‚Œ๎‚‰
๎‚๎‚ˆ๎‚‰๎‚… ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚˜๎‚“ ๎‚๎‚“๎‚›๎‚‰๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹๎‚‰๎‚’๎‚‡๎‚ ๎‚“๎‚Š ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚…๎‚‡๎‚Œ๎‚๎‚‰๎‚š๎‚‰ ๎‚˜๎‚Œ๎‚๎‚— ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚˜๎‚“
๎‚…๎‚‡๎‚˜๎‚™๎‚…๎‚๎‚๎‚ ๎Šฑ๎‚œ ๎‚…๎‚๎‚ ๎‚๎‚—๎‚—๎‚™๎‚‰๎‚— ๎‚–๎‚‰๎‚”๎‚“๎‚–๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‡€ ๎€„๎‚ ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜๎†ฝ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚…๎‚— ๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜ ๎‚…๎‚—
๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚๎‚‘ ๎‚“๎‚Š ๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚…๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹๎‚‰๎‚’๎‚‡๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚๎‚๎‚‰๎‚—๎‚˜๎‡€
RULE
33
RULE โ€” Compile the code without any error nor warning while enabling
strict compilation options
๎€Š๎‚๎‚‹๎‚Œ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹ ๎‚…๎‚’๎‚ˆ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚๎‚‰๎‚š๎‚‰๎‚๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚๎‚’๎‚๎‚‰๎‚– ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“
๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰๎†ฝ ๎‚…๎‚— ๎‚‘๎‚™๎‚‡๎‚Œ ๎‚…๎‚— ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚”๎‚“๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚๎‚—๎‚—๎‚™๎‚‰๎‚— ๎‚–๎‚‰๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚™๎‚—๎‚‰
๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹ ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰๎‡€
๎€ƒ๎‚๎‚ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚…๎‚’๎‚ˆ ๎‚…๎‚๎‚ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚–๎‚‰๎‚”๎‚“๎‚–๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚๎‚’๎‚๎‚‰๎‚– ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚…๎‚๎‚˜
๎‚›๎‚๎‚˜๎‚Œ๎‡€ ๎€‹๎‚˜ ๎‚๎‚— ๎‚๎‚’๎‚‡๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚…๎‚๎‚๎‚ ๎‚š๎‚‰๎‚–๎‚ ๎‚‘๎‚™๎‚‡๎‚Œ ๎‚…๎‚ˆ๎‚š๎‚๎‚—๎‚‰๎‚ˆ๎†ฝ ๎‚๎‚Š ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎„Œ๎„ˆ๎„ˆ ๎‚“๎‚– ๎€…๎„‘๎„†๎„“๎„Œ๎†ฝ ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ -Werror
๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚˜๎‚™๎‚–๎‚’ ๎‚…๎‚’๎‚ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹ ๎‚๎‚’๎‚˜๎‚“ ๎‚… ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎†ฝ ๎‚Œ๎‚‰๎‚’๎‚‡๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚–๎‚™๎‚’๎‚’๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰
28 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎‚–๎‚๎‚—๎‚ ๎‚“๎‚Š ๎‚๎‚‹๎‚’๎‚“๎‚–๎‚๎‚’๎‚‹ ๎‚๎‚˜๎‡€
๎€–๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚๎‚‰๎‚š๎‚…๎‚’๎‚‡๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚‡๎‚‡๎‚™๎‚–๎‚…๎‚‡๎‚ ๎‚“๎‚Š ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚‰๎‚‘๎‚๎‚˜๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚— ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚๎‚“๎‚’
๎‚“๎‚Š ๎‚…๎‚’๎‚…๎‚๎‚๎‚ž๎‚‰๎‚— ๎‚๎‚˜ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚™๎‚‡๎‚˜๎‚—๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚๎‚’ ๎‚˜๎‚™๎‚–๎‚’ ๎‚–๎‚‰๎‚๎‚ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚“๎‚™๎‚— ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚๎‚— ๎‚…๎‚†๎‚๎‚‰ ๎‚˜๎‚“
๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‡€ ๎€•๎‚”๎‚‰๎‚‡๎‚๎‚Š๎‚๎‚๎‚’๎‚‹ ๎‚… ๎‚–๎‚‰๎‚…๎‚—๎‚“๎‚’๎‚…๎‚†๎‚๎‚ ๎‚Œ๎‚๎‚‹๎‚Œ ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚‰๎‚š๎‚‰๎‚ ๎‚๎‚— ๎‚˜๎‚Œ๎‚™๎‚— ๎‚†๎‚‰๎‚’๎‚‰๎Šฑ๎‚‡๎‚๎‚…๎‚๎‡€
RULE
34
RULE โ€” Enable a reasonably high optimization level
๎€ˆ๎‚“๎‚– ๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚‰๎‚š๎‚‰๎‚ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚˜ ๎‚๎‚‰๎‚…๎‚—๎‚˜ -O1๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚๎‚ˆ๎‚‰๎‚…๎‚๎‚๎‚ -O2 ๎‚“๎‚–
-Os๎‡€
Warning
๎€‹๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚—๎‚๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚˜๎‚“ ๎‚‘๎‚…๎‚๎‚‰ ๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚… ๎‚Œ๎‚๎‚‹๎‚Œ ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚‰๎‚š๎‚‰๎‚
๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚—๎‚™๎‚”๎‚”๎‚–๎‚‰๎‚—๎‚— ๎‚ˆ๎‚‰๎‚Š๎‚‰๎‚’๎‚—๎‚๎‚š๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚– ๎‚‘๎‚…๎‚’๎‚™๎‚…๎‚๎‚๎‚ ๎‚…๎‚ˆ๎‚ˆ๎‚‰๎‚ˆ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚–๎‚‘๎‚‰๎‚…๎‚—๎‚™๎‚–๎‚‰๎‚—๎‡€
Information
๎€ˆ๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚๎‚’๎‚๎‚‘๎‚…๎‚ ๎‚‡๎‚“๎‚‘๎‚‘๎‚…๎‚’๎‚ˆ ๎‚๎‚๎‚’๎‚‰ ๎‚Š๎‚“๎‚– ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚๎‚’๎‚‹ ๎‚›๎‚๎‚˜๎‚Œ ๎„Œ๎„ˆ๎„ˆ ๎‚“๎‚– ๎€…๎„‘๎„†๎„“๎„Œ ๎‚๎‚—๎†ฟ
gcc/clang -O1 -Wall
a
-Wextra
b
-Wpedantic
c
-Werror -std=c99/c90
d
file.c
-o file.exe
RECO
35
RECOMMENDATION โ€” Use the strictest compilation options
๎€‹๎‚Š ๎‚… ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚–๎‚“๎‚š๎‚‰๎‚— ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚˜๎‚“๎‚“ ๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜ ๎‚Š๎‚“๎‚– ๎‚… ๎‚‹๎‚๎‚š๎‚‰๎‚’ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚… ๎‚‡๎‚Œ๎‚“๎‚๎‚‡๎‚‰
๎‚๎‚— ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚˜๎‚“ ๎‚ˆ๎‚๎‚—๎‚…๎‚†๎‚๎‚‰ ๎‚๎‚˜๎†ฝ ๎‚… ๎‚Ž๎‚™๎‚—๎‚˜๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚”๎‚–๎‚“๎‚š๎‚๎‚ˆ๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚‰๎‚œ๎‚”๎‚๎‚…๎‚๎‚’ ๎‚๎‚˜๎‡€
Information
๎€ƒ๎‚”๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚œ ๎€„๎‡€๎ˆ‘ ๎‚ˆ๎‚–๎‚…๎‚›๎‚— ๎‚™๎‚” ๎‚… ๎‚’๎‚“๎‚’๎‡‚๎‚‰๎‚œ๎‚Œ๎‚…๎‚™๎‚—๎‚˜๎‚๎‚š๎‚‰ ๎‚๎‚๎‚—๎‚˜ ๎‚“๎‚Š ๎‚‰๎‚œ๎‚˜๎‚–๎‚… ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚Š๎‚“๎‚– ๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ๎†ฝ
๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚‡๎‚…๎‚’ ๎‚—๎‚‰๎‚–๎‚š๎‚‰ ๎‚…๎‚— ๎‚… ๎‚—๎‚˜๎‚…๎‚–๎‚˜๎‚๎‚’๎‚‹ ๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–๎‡€
๎€‹๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚—๎‚™๎‚”๎‚”๎‚–๎‚‰๎‚—๎‚— ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚…๎‚’๎‚ˆ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚–๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚ˆ๎‚“ ๎‚๎‚— ๎‚“๎‚Š ๎‚‡๎‚“๎‚™๎‚–๎‚—๎‚‰ ๎‚˜๎‚“ ๎Šฑ๎‚œ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ
๎‚›๎‚Œ๎‚๎‚๎‚‰ ๎‚๎‚‘๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚š๎‚‰๎‚๎‚ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚๎‚’๎‚‹ ๎‚“๎‚’ ๎‚…๎‚’๎‚ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‰๎‚ˆ๎‚๎‚˜๎‡€ ๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚๎‚Š ๎‚๎‚˜ ๎‚…๎‚”๎‚”๎‚‰๎‚…๎‚–๎‚— ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚… ๎‚Š๎‚…๎‚๎‚—๎‚‰
๎‚”๎‚“๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰๎†ฝ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚‘๎‚‰๎‚˜๎‚Œ๎‚“๎‚ˆ๎‚— ๎‚‰๎‚œ๎‚๎‚—๎‚˜๎‡€ ๎€ˆ๎‚๎‚–๎‚—๎‚˜๎‚๎‚๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚… ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚’๎‚๎‚”๎‚”๎‚‰๎‚˜ ๎‚‘๎‚…๎‚ ๎‚“๎‚‡๎‚‡๎‚…๎‚—๎‚๎‚“๎‚’๎‚…๎‚๎‚๎‚ ๎‚—๎‚™๎Šฒ๎‚‡๎‚‰
๎‚˜๎‚“ ๎‚‘๎‚๎‚—๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚…๎‚’๎‚…๎‚๎‚๎‚—๎‚๎‚—๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚†๎‚‰๎‚’๎‚‰๎Šฑ๎‚‡๎‚๎‚…๎‚ ๎‚๎‚’ ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚ ๎‚˜๎‚“ ๎‚—๎‚๎‚‘๎‚”๎‚๎‚ ๎‚–๎‚‰๎‚›๎‚–๎‚๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚๎‚— ๎‚Œ๎‚™๎‚’๎‚
๎‚๎‚’ ๎‚… ๎‚‘๎‚“๎‚–๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚๎‚๎‚๎‚‹๎‚๎‚†๎‚๎‚‰ ๎‚Š๎‚“๎‚–๎‚‘๎†ฝ ๎‚›๎‚Œ๎‚๎‚๎‚‰ ๎‚“๎‚†๎‚š๎‚๎‚“๎‚™๎‚—๎‚๎‚ ๎‚‘๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚—๎‚™๎‚–๎‚‰ ๎‚๎‚˜ ๎‚๎‚— ๎‚—๎‚‰๎‚‘๎‚…๎‚’๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚‰๎‚•๎‚™๎‚๎‚š๎‚…๎‚๎‚‰๎‚’๎‚˜๎‡€ ๎€–๎‚Œ๎‚‰๎‚’๎†ฝ ๎‚๎‚Š ๎‚๎‚˜
๎‚˜๎‚™๎‚–๎‚’๎‚— ๎‚“๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚… ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹ ๎‚‡๎‚…๎‚’๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚‰๎‚๎‚๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎Šฑ๎‚œ๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚…๎‚๎‚๎‚“๎‚›๎‚—
๎‚Š๎‚“๎‚– ๎‚๎‚˜ ๎‡ฆ๎‚š๎‚๎‚… ๎‚… #pragma ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎‡ง๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹ ๎‚‘๎‚…๎‚ ๎‚†๎‚‰ ๎‚ˆ๎‚๎‚—๎‚…๎‚†๎‚๎‚‰๎‚ˆ ๎‚๎‚“๎‚‡๎‚…๎‚๎‚๎‚๎‡€
a๎‡€ ๎€‡๎‚’๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚…๎‚†๎‚“๎‚™๎‚˜ ๎‚–๎‚๎‚—๎‚๎‚ ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚‰๎‚…๎‚—๎‚ ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ๎‡€ ๎€•๎‚‰๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‚‘๎‚…๎‚’๎‚™๎‚…๎‚๎‚—
๎‚Š๎‚“๎‚– ๎‚… ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚˜๎‚‰ ๎‚๎‚๎‚—๎‚˜๎‚๎‚’๎‚‹๎‡€
b๎‡€ ๎€‡๎‚’๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚—๎‚“๎‚‘๎‚‰ ๎‚‰๎‚œ๎‚˜๎‚–๎‚… ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹ ๎Šท๎‚…๎‚‹๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰๎‚ˆ ๎‚†๎‚ -Wall๎‡€ ๎€•๎‚‰๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‚‘๎‚…๎‚’๎‚™๎‚…๎‚๎‚— ๎‚Š๎‚“๎‚– ๎‚… ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚˜๎‚‰
๎‚๎‚๎‚—๎‚˜๎‚๎‚’๎‚‹๎‡€
c๎‡€ ๎€‡๎‚’๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚ˆ๎‚‰๎‚‘๎‚…๎‚’๎‚ˆ๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎†พ ๎‚ˆ๎‚๎‚—๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚๎‚ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚‰๎‚œ๎‚˜๎‚‰๎‚’๎‚—๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚’๎‚‰๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚“๎‚’๎Šท๎‚๎‚‡๎‚˜
๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‡€
d๎‡€ ๎€•๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‡€ ๎€•๎‚‰๎‚‰ ๎‚…๎‚”๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚œ ๎€„๎‡€๎ˆ๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 29
Warning
๎€—๎‚—๎‚๎‚’๎‚‹ #pragma ๎‚˜๎‚“ ๎‚—๎‚™๎‚”๎‚”๎‚–๎‚‰๎‚—๎‚— ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚•๎‚™๎‚๎‚˜๎‚‰ ๎‚ˆ๎‚…๎‚’๎‚‹๎‚‰๎‚–๎‚“๎‚™๎‚— ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚†๎‚‰
๎‚”๎‚‰๎‚–๎‚Š๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚“๎‚“๎‚ˆ ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚’๎‚“๎‚˜ ๎‚˜๎‚“ ๎‚ˆ๎‚๎‚—๎‚…๎‚†๎‚๎‚‰ ๎‚“๎‚’๎‚‰ ๎‚“๎‚– ๎‚‘๎‚“๎‚–๎‚‰ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚›๎‚Œ๎‚“๎‚๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰
๎‚†๎‚…๎‚—๎‚‰ ๎‚†๎‚ ๎‚‘๎‚๎‚—๎‚˜๎‚…๎‚๎‚‰๎‡€ ๎€๎‚“๎‚–๎‚‰๎‚“๎‚š๎‚‰๎‚–๎†ฝ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š #pragma ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚‘๎‚™๎‚—๎‚˜
๎‚๎‚‰๎‚‰๎‚” ๎‚๎‚’ ๎‚‘๎‚๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚๎‚— ๎‚๎‚— ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‡ ๎‚˜๎‚“ ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‡ฆ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‡‚๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ๎‡ง ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚™๎‚—
๎‚–๎‚๎‚—๎‚๎‚๎‡€
๎€‹๎‚’ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚“๎‚”๎‚˜๎‚— ๎‚Š๎‚“๎‚– ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹ ๎‚—๎‚™๎‚”๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎†ฝ ๎‚… ๎‚‡๎‚๎‚‰๎‚…๎‚– ๎‚Ž๎‚™๎‚—๎‚˜๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚’๎‚‰๎‚‰๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚”๎‚–๎‚“๎‚š๎‚๎‚ˆ๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ
๎‚… ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎†ฟ
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused - variable"
/* code */
/* some variables are not used in the new algorithm anymore but are kept for API compatibility */
/* warnings about such unused variables are thus disabled in the code following the pragma */
#pragma GCC diagnostic pop
5.2.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€†๎‚๎‚–๎‡€ ๎ˆ‘๎‡€๎ˆ๎†ฟ ๎€ƒ๎‚๎‚ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰๎‚— ๎Šฑ๎‚๎‚‰๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚…๎‚’๎‚ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚—๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€†๎‚๎‚–๎‡€ ๎ˆ“๎‡€๎ˆ๎†ฟ ๎€”๎‚™๎‚’๎‡‚๎‚˜๎‚๎‚‘๎‚‰ ๎‚Š๎‚…๎‚๎‚๎‚™๎‚–๎‚‰๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚‘๎‚๎‚’๎‚๎‚‘๎‚๎‚ž๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€๎€•๎€…๎ˆ๎ˆ๎‡‚๎€…๎†ฟ ๎€…๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰ ๎‚‡๎‚๎‚‰๎‚…๎‚’๎‚๎‚ ๎‚…๎‚˜ ๎‚Œ๎‚๎‚‹๎‚Œ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹ ๎‚๎‚‰๎‚š๎‚‰๎‚๎‚—๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ”๎ˆ•๎ˆ’ ๎€—๎‚’๎‚™๎‚—๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ”๎ˆ–๎ˆ ๎€‡๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚Š๎‚…๎‚๎‚—๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ”๎ˆ–๎ˆ ๎€‡๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚˜๎‚–๎‚™๎‚‰๎‡€
5.3 Use of security features provided by compilers
๎€๎‚“๎‚ˆ๎‚‰๎‚–๎‚’ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‚“๎Šฎ๎‚‰๎‚– ๎‚š๎‚…๎‚–๎‚๎‚“๎‚™๎‚— ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‘๎‚…๎‚๎‚‰ ๎‚๎‚˜ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚๎‚‘๎‚”๎‚–๎‚“๎‚š๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚“๎‚†๎‚™๎‚—๎‚˜๎‚’๎‚‰๎‚—๎‚— ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚‰๎‚Š๎‚‰๎‚’๎‡‚
๎‚—๎‚๎‚š๎‚‰๎‚’๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚’๎‚…๎‚ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚…๎‚†๎‚๎‚‰๎‡€ ๎€‹๎‚˜ ๎‚‡๎‚…๎‚’ ๎‚‡๎‚“๎‚‘๎‚‰ ๎‚˜๎‚“ ๎‚”๎‚–๎‚‰๎‚š๎‚‰๎‚’๎‚˜ ๎‚… ๎‚š๎‚™๎‚๎‚’๎‚‰๎‚–๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚—๎‚™๎‚–๎‚Š๎‚…๎‚‡๎‚๎‚’๎‚‹ ๎‚“๎‚– ๎‚˜๎‚“ ๎‚–๎‚‰๎‚ˆ๎‚™๎‚‡๎‚‰
๎‚๎‚˜๎‚— ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚ ๎‚๎‚‘๎‚”๎‚…๎‚‡๎‚˜๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚…๎‚๎‚—๎‚“ ๎‚˜๎‚“ ๎‚Œ๎‚…๎‚–๎‚ˆ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚…๎‚‹๎‚…๎‚๎‚’๎‚—๎‚˜ ๎‚š๎‚™๎‚๎‚’๎‚‰๎‚–๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚‰๎‚œ๎‚”๎‚๎‚“๎‚๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚˜๎‚˜๎‚‰๎‚‘๎‚”๎‚˜๎‚—๎‡€
RULE
36
RULE โ€” Make use of security features offered by compilers
๎€†๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–๎‚— ๎‚‘๎‚™๎‚—๎‚˜๎†ฝ ๎‚…๎‚— ๎‚‘๎‚™๎‚‡๎‚Œ ๎‚…๎‚— ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰๎†ฝ ๎‚˜๎‚…๎‚๎‚‰ ๎‚…๎‚ˆ๎‚š๎‚…๎‚’๎‚˜๎‚…๎‚‹๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜
๎‚…๎‚๎‚๎‚“๎‚› ๎‚Š๎‚“๎‚– ๎‚๎‚‘๎‚”๎‚–๎‚“๎‚š๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚’๎‚…๎‚ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚˜๎‡€
Warning
๎€–๎‚Œ๎‚–๎‚“๎‚™๎‚‹๎‚Œ๎‚“๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚๎‚— ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚›๎‚Œ๎‚‰๎‚’ ๎„Œ๎„ˆ๎„ˆ ๎‚“๎‚– ๎€…๎„‘๎„†๎„“๎„Œ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚‹๎‚๎‚š๎‚‰๎‚’ ๎‚…๎‚— ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎‚—๎†ฝ ๎‚๎‚˜ ๎‚๎‚—
๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚˜๎‚“ ๎‚๎‚‰๎‚‰๎‚” ๎‚๎‚’ ๎‚‘๎‚๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚…๎‚˜๎†ฟ
n
๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚”๎‚”๎‚๎‚ ๎‚˜๎‚“ ๎„Œ๎„ˆ๎„ˆ ๎ˆ๎ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ ๎ˆ๎ˆ’ ๎‚–๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚๎‚๎†พ
n
๎‚—๎‚“๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚‘ ๎‚…๎‚–๎‚‰ ๎‚…๎‚๎‚–๎‚‰๎‚…๎‚ˆ๎‚ ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜๎†ฝ ๎‚—๎‚“๎‚‘๎‚‰๎‚˜๎‚๎‚‘๎‚‰๎‚— ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚…๎‚๎‚๎‚๎†ฝ ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚’
๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚๎‚˜๎‚— ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚๎‚˜ ๎‚๎‚— ๎‚”๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚…๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚Š๎‚ ๎‚˜๎‚Œ๎‚‰๎‚‘ ๎‚…๎‚’๎‚๎‚›๎‚…๎‚๎†พ
n
๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚‡๎‚‡๎‚™๎‚–๎‚…๎‚‡๎‚ ๎‚“๎‚Š ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚‰๎‚‘๎‚๎‚˜๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚‘๎‚…๎‚ ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ ๎‚™๎‚”๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ
๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚‰๎‚š๎‚‰๎‚๎†พ
30 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
n
๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‘๎‚”๎‚…๎‚‡๎‚˜ ๎‚“๎‚’ ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚’๎‚‡๎‚‰๎‚—๎†ฝ ๎‚๎‚Š ๎‚‘๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚‰๎‚ˆ๎†ฝ ๎‚๎‚— ๎‚‹๎‚๎‚š๎‚‰๎‚’ ๎‚Š๎‚“๎‚– ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚™๎‚–๎‚”๎‚“๎‚—๎‚‰๎‚— ๎‚“๎‚’๎‚๎‚
๎‚…๎‚’๎‚ˆ ๎‚‘๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚ ๎‚… ๎‚๎‚“๎‚˜ ๎‚…๎‚‡๎‚‡๎‚“๎‚–๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰๎‚—๎†พ ๎‚๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–๎‡‹๎‚— ๎‚Ž๎‚“๎‚† ๎‚˜๎‚“
๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚’๎‚‡๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚Œ๎‚๎‚— ๎‚’๎‚‰๎‚‰๎‚ˆ๎‚—๎‡€
5.3.1 Warnings oriented towards security bugs
๎€ƒ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚“๎Šฎ๎‚‰๎‚–๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‚Š๎‚“๎‚‡๎‚™๎‚— ๎‚š๎‚‰๎‚–๎‚ ๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚…๎‚๎‚๎‚ ๎‚“๎‚’ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜๎‚๎‚’๎‚‹ ๎‚”๎‚“๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚
๎‚๎‚—๎‚—๎‚™๎‚‰๎‚—๎‡€ ๎€‡๎‚’๎‚…๎‚†๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚‰๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚›๎‚๎‚˜๎‚Œ ๎‚…๎‚’๎‚ ๎‚๎‚—๎‚—๎‚™๎‚‰ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚–๎‚‰๎‚”๎‚“๎‚–๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚™๎‚— ๎‚… ๎Šฑ๎‚–๎‚—๎‚˜ ๎‚—๎‚˜๎‚‰๎‚” ๎‚๎‚’
๎‚–๎‚‰๎‚ˆ๎‚™๎‚‡๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚๎‚—๎‚ ๎‚“๎‚Š ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚š๎‚™๎‚๎‚’๎‚‰๎‚–๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚๎‚‰๎‚— ๎‚†๎‚‰๎‚๎‚’๎‚‹ ๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚†๎‚…๎‚—๎‚‰๎‡€
RULE
37
RULE โ€” Enable warnings that focus on detecting security bugs and deal
with any reported issue
๎€ˆ๎‚“๎‚– ๎‚๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚‡๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ -Wformat=2
a
๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰๎‚ˆ
๎‚…๎‚’๎‚ˆ ๎‚…๎‚’๎‚ ๎‚–๎‚‰๎‚”๎‚“๎‚–๎‚˜๎‚‰๎‚ˆ ๎‚๎‚—๎‚—๎‚™๎‚‰ ๎‚’๎‚‰๎‚‰๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚‘๎‚‰๎‚˜๎‚Œ๎‚“๎‚ˆ๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚ˆ๎‚‰๎‚…๎‚๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ๎‡€
๎„Œ๎„ˆ๎„ˆ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— -Wformat-overflow=2 ๎‚…๎‚’๎‚ˆ -Wformat-truncation=2 ๎‚‘๎‚…๎‚ ๎‚…๎‚๎‚—๎‚“ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
Information
๎€–๎‚Œ๎‚‰ ๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‚’๎‚“๎‚›๎‚…๎‚ˆ๎‚…๎‚๎‚— ๎‚‰๎‚‘๎‚†๎‚‰๎‚ˆ ๎‚—๎‚˜๎‚…๎‚˜๎‚๎‚‡ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚…๎‚’๎‚…๎‚๎‚๎‚ž๎‚‰๎‚–๎‚— ๎‚‡๎‚…๎‚”๎‚…๎‚†๎‚๎‚‰
๎‚“๎‚Š ๎‚–๎‚™๎‚’๎‚’๎‚๎‚’๎‚‹ ๎‚ˆ๎‚‰๎‚‰๎‚”๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚‰ ๎‡ก ๎‚†๎‚™๎‚˜ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚‰๎‚œ๎‚”๎‚‰๎‚’๎‚—๎‚๎‚š๎‚‰ ๎‡ก ๎‚…๎‚’๎‚…๎‚๎‚๎‚—๎‚‰๎‚— ๎‚“๎‚’ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚—
๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚—๎†ฝ ๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚…๎‚๎‚๎‚ ๎‚”๎‚“๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚ ๎‚š๎‚™๎‚๎‚’๎‚‰๎‚–๎‚…๎‚†๎‚๎‚๎‡‚
๎‚๎‚˜๎‚๎‚‰๎‚—๎‡€
๎€‡๎‚š๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚“๎‚™๎‚‹๎‚Œ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚‰๎‚‘๎‚†๎‚‰๎‚ˆ๎‚ˆ๎‚‰๎‚ˆ ๎‚…๎‚’๎‚…๎‚๎‚๎‚ž๎‚‰๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚—๎‚˜๎‚๎‚๎‚ ๎‚–๎‚…๎‚˜๎‚Œ๎‚‰๎‚– ๎‚†๎‚…๎‚—๎‚๎‚‡ ๎‚…๎‚’๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚’๎‚ˆ ๎‚๎‚‰๎‚˜ ๎‚•๎‚™๎‚๎‚˜๎‚‰
๎‚‰๎‚œ๎‚”๎‚‰๎‚–๎‚๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚๎‚“๎‚’๎‚‰ ๎‚˜๎‚“๎‚“๎‚๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚—๎‚˜๎‚…๎‚˜๎‚๎‚‡ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰
๎‚…๎‚’๎‚…๎‚๎‚๎‚—๎‚๎‚—๎†ฝ ๎‚๎‚˜ ๎‚‘๎‚…๎‚ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚๎‚‰๎‚’๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚‰๎‚—๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰๎‚‘๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚‰๎‚—๎‚˜๎‚‰๎‚ˆ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–
๎‚‘๎‚…๎‚ ๎‚–๎‚‰๎‚Š๎‚‰๎‚– ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡ฌ๎€‰๎‚‡๎‚‡๎€”๎‚‰๎‚Š๎‡ญ ๎‚“๎‚Š ๎„Œ๎„ˆ๎„ˆ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ -fanalyzer ๎‚…๎‚— ๎‚›๎‚‰๎‚๎‚ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰
Clang Static Analyzer ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎€…๎„‘๎„†๎„“๎„Œ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡ฌ๎€…๎‚๎‚…๎‚’๎‚‹๎€”๎‚‰๎‚Š๎‡ญ๎‡€
5.3.2 Instrumentation of some particularly unsafe functions
๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚“๎‚– ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹๎‚— ๎‚๎‚— ๎‚… ๎‚๎‚…๎‚–๎‚‹๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹
๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚Š๎‚–๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚˜๎‚๎‚ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚š๎‚™๎‚๎‚’๎‚‰๎‚–๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚๎‚‰๎‚— ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— ๎‚†๎‚™๎Šฎ๎‚‰๎‚– ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‚—๎‡€ ๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚“ ๎‚‡๎‚Œ๎‚“๎‚“๎‚—๎‚๎‚’๎‚‹ ๎‚๎‚‰๎‚—๎‚—
๎‚ˆ๎‚…๎‚’๎‚‹๎‚‰๎‚–๎‚“๎‚™๎‚— ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚—๎‚™๎‚‡๎‚Œ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚…๎‚š๎‚…๎‚๎‚๎‚…๎‚†๎‚๎‚‰ ๎‡ฆcf. ๎‚–๎‚™๎‚๎‚‰๎‚— ๎‚Š๎‚–๎‚“๎‚‘ ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ๎ˆ–๎‡€๎ˆ“๎‡ง๎†ฝ ๎‚—๎‚“๎‚‘๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚—
๎‚…๎‚–๎‚‰ ๎‚‡๎‚…๎‚”๎‚…๎‚†๎‚๎‚‰ ๎‚“๎‚Š ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚‰๎‚’๎‚Œ๎‚…๎‚’๎‚‡๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰๎‚‘ ๎‚›๎‚๎‚˜๎‚Œ ๎‚—๎‚๎‚‘๎‚”๎‚๎‚‰ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚—๎†ฝ ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰ ๎‚“๎‚– ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‡‚
๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚๎‚‘๎‚‰๎†ฝ ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜ ๎‚”๎‚“๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚†๎‚™๎Šฎ๎‚‰๎‚– ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‚—๎‡€
RULE
38
RULE โ€” Enable the use of hardened variants of unsafe functions
๎€ˆ๎‚“๎‚– ๎‚๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚‡๎‚‰๎†ฝ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎„Œ๎„ˆ๎„ˆ ๎‚˜๎‚“ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰ ๎‚… ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚ˆ๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚… ๎€‰๎€๎€—๎‡š๎€Ž๎‚๎‚’๎‚™๎‚œ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘
๎‚–๎‚™๎‚’๎‚’๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‹๎‚๎‚๎‚†๎‚‡๎†ฝ ๎‚˜๎‚Œ๎‚‰ _FORTIFY_SOURCE ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’
๎‚๎‚‰๎‚š๎‚‰๎‚ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‹๎‚–๎‚‰๎‚…๎‚˜๎‚‰๎‚– ๎‚“๎‚– ๎‚‰๎‚•๎‚™๎‚…๎‚ ๎‚˜๎‚“ -O1 ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚‰๎‚ˆ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚— ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚‰๎Šฎ๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‡€
๎€”๎‚™๎‚’๎‡‚๎‚˜๎‚๎‚‘๎‚‰ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚— ๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚…๎‚— ๎‚—๎‚“๎‚“๎‚’ ๎‚…๎‚— ๎‚…๎‚’ ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚› ๎‚๎‚— ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ๎‡€
a๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚— ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‡‚๎‚˜๎‚๎‚‘๎‚‰ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚— ๎‚“๎‚’ ๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜ ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹๎‚—๎†ฝ ๎‚…๎‚— ๎‚›๎‚‰๎‚๎‚ ๎‚…๎‚— ๎‚“๎‚’ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚๎‚๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚…๎‚๎‚‰ ๎‚˜๎‚Œ๎‚‰๎‚‘ ๎‚…๎‚— ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 31
Information
๎€‘๎‚”๎‚˜๎‚๎‚“๎‚’ -Wstringop-overflow=n ๎‚“๎‚Š ๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚Š๎‚“๎‚– ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚๎‚’๎‚‹ ๎‚—๎‚๎‚‘๎‚๎‚๎‚…๎‚– ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚— ๎‚…๎‚˜
๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‡‚๎‚˜๎‚๎‚‘๎‚‰ ๎‚“๎‚’๎‚๎‚๎‡€ ๎€–๎‚Œ๎‚‰๎‚๎‚– ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹๎‚‰๎‚’๎‚‡๎‚ ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚— ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š n๎‡€
5.3.3 Initialization of automatic variables
๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚™๎‚’๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚ˆ ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—
๎ˆ’
๎‚๎‚— ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚‡๎‚“๎‚‘๎‚‘๎‚“๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚…๎‚’๎‚ˆ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚š๎‚™๎‚๎‚’๎‚‰๎‚–๎‚…๎‚†๎‚๎‚๎‡‚
๎‚๎‚˜๎‚๎‚‰๎‚— ๎‡ฆ๎‚—๎‚‰๎‚‰ ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ•๎‡€๎ˆ๎ˆ ๎‚…๎‚— ๎‚›๎‚‰๎‚๎‚๎‡ง๎‡€ ๎€ƒ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‚Œ๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚– ๎‚…๎‚–๎‚‰ ๎‚‡๎‚…๎‚”๎‚…๎‚†๎‚๎‚‰ ๎‚“๎‚Š ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜๎‚๎‚’๎‚‹ ๎‚—๎‚“๎‚‘๎‚‰ ๎‚“๎‚Š
๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚™๎‚—๎‚‰๎‚—๎†ฝ ๎‚…๎‚— ๎‚๎‚“๎‚’๎‚‹ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚”๎‚”๎‚–๎‚“๎‚”๎‚–๎‚๎‚…๎‚˜๎‚‰ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚…๎‚–๎‚‰ ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰๎‚ˆ๎‡€
RULE
39
RULE โ€” Enable compiler warnings related to the use of uninitialized vari-
ables
๎€‹๎‚’ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚–๎†ฝ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— -Wuninitialized
a
๎†ฝ -Winit-self ๎‚…๎‚’๎‚ˆ
-Wmaybe-uninitialized
b
๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰๎‚ˆ ๎‚›๎‚Œ๎‚‰๎‚’ ๎„Œ๎„ˆ๎„ˆ ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎€ƒ๎‚— ๎‚Š๎‚“๎‚– ๎€…๎„‘๎„†๎„“๎„Œ๎†ฝ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— -Wuninitialized
c
๎‚…๎‚’๎‚ˆ -Wconditional-uninitialized ๎‚‘๎‚™๎‚—๎‚˜
๎‚†๎‚‰ ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰๎‚ˆ๎‡€
Warning
๎€‹๎‚˜ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚’๎‚“๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜ ๎‚…๎‚๎‚ ๎‚๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚‡๎‚‰๎‚— ๎‚“๎‚Š ๎‚™๎‚’๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚ˆ
๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚™๎‚—๎‚‰๎‚—๎†ฝ ๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚…๎‚๎‚๎‚ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚”๎‚…๎‚—๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—
๎‚†๎‚ ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰๎‡€
๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚–๎‚‘๎‚“๎‚–๎‚‰๎†ฝ ๎‚—๎‚“๎‚‘๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‚—๎‚™๎‚”๎‚”๎‚“๎‚–๎‚˜ ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚—๎‚™๎‚‡๎‚Œ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎‡€ ๎€‹๎‚’ ๎‚”๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚‡๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚๎‚—
๎‚Š๎‚“๎‚–๎‚‡๎‚‰๎‚ˆ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚’ ๎‚™๎‚—๎‚‰ ๎‚‰๎‚๎‚˜๎‚Œ๎‚‰๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚ž๎‚‰๎‚–๎‚“ ๎‚“๎‚– ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚– ๎‚š๎‚…๎‚๎‚™๎‚‰๎†ฝ ๎‚‡๎‚…๎‚๎‚๎‚‰๎‚ˆ pattern๎‡€ ๎€ƒ๎‚™๎‡‚
๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚“ ๎‚ž๎‚‰๎‚–๎‚“ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚‡๎‚Œ๎‚“๎‚—๎‚‰๎‚’ ๎‚Š๎‚“๎‚– release ๎‚†๎‚™๎‚๎‚๎‚ˆ๎‚— ๎‡ฆcf. ๎‚’๎‚‰๎‚œ๎‚˜ ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ”๎‡€๎ˆ“๎‡ง ๎‚†๎‚‰๎‚‡๎‚…๎‚™๎‚—๎‚‰ ๎‚๎‚˜
๎‚™๎‚—๎‚™๎‚…๎‚๎‚๎‚ ๎‚๎‚๎‚‘๎‚๎‚˜๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚“๎‚๎‚˜๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚†๎‚™๎‚‹๎‡€ ๎€‹๎‚’ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚…๎‚—๎‚˜๎†ฝ ๎‚ˆ๎‚™๎‚–๎‚๎‚’๎‚‹ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜๎†ฝ ๎‚˜๎‚‰๎‚—๎‚˜๎‚๎‚’๎‚‹ ๎‚…๎‚’๎‚ˆ
๎‚ˆ๎‚‰๎‚†๎‚™๎‚‹๎‚‹๎‚๎‚’๎‚‹ ๎‚”๎‚Œ๎‚…๎‚—๎‚‰๎‚—๎†ฝ ๎‚”๎‚…๎‚˜๎‚˜๎‚‰๎‚–๎‚’ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚”๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚…๎‚†๎‚๎‚‰ ๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎‚๎‚˜ ๎‚๎‚— ๎‚‘๎‚“๎‚–๎‚‰ ๎‚๎‚๎‚๎‚‰๎‚๎‚ ๎‚˜๎‚“ ๎‚™๎‚’๎‚‡๎‚“๎‚š๎‚‰๎‚– ๎‚‡๎‚‰๎‚–๎‚˜๎‚…๎‚๎‚’ ๎‚†๎‚™๎‚‹๎‚—๎‡€
๎€•๎‚‰๎‚๎‚‰๎‚‡๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚๎‚‹๎‚Œ๎‚˜ ๎‚”๎‚…๎‚˜๎‚˜๎‚‰๎‚–๎‚’ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚๎†ฟ ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚Š๎‚“๎‚– ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡‚๎‚˜๎‚๎‚”๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎†ฝ ๎‚๎‚˜ ๎‚‘๎‚…๎‚ ๎‚†๎‚‰ ๎‚…
๎‚’๎‚“๎‚’๎‡‚๎‚‡๎‚…๎‚’๎‚“๎‚’๎‚๎‚‡๎‚…๎‚ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚—๎‚“ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚’๎‚ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚˜๎‚Œ๎‚–๎‚“๎‚™๎‚‹๎‚Œ ๎‚…๎‚’ ๎‚™๎‚’๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚ˆ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚
๎‚Š๎‚…๎‚™๎‚๎‚˜๎‚—๎‡€
RULE
40
RULE โ€” Enable forced initialization of automatic variables by the compiler
๎€…๎„‘๎„†๎„“๎„Œ ๎‚—๎‚™๎‚”๎‚”๎‚“๎‚–๎‚˜๎‚— ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚›๎‚“ ๎‚…๎‚Š๎‚“๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚‰๎‚ˆ ๎‚…๎‚”๎‚”๎‚–๎‚“๎‚…๎‚‡๎‚Œ๎‚‰๎‚—๎†ฟ
n
-ftrivial-auto-var-init=pattern ๎‚Š๎‚“๎‚– ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜๎†ฝ ๎‚˜๎‚‰๎‚—๎‚˜๎‚— ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚‰๎‚†๎‚™๎‚‹๎‚‹๎‚๎‚’๎‚‹๎†พ
n
-ftrivial-auto-var-init=zero ๎‚Š๎‚“๎‚– release ๎‚†๎‚™๎‚๎‚๎‚ˆ๎‚—๎†ฝ ๎‚…๎‚’
๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‡๎‚™๎‚–๎‚–๎‚‰๎‚’๎‚˜๎‚๎‚ ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚˜๎‚…๎‚˜๎‚‰๎‚— ๎‚…๎‚”๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’
-enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang๎‡€
๎ˆ’๎‡€ ๎€ƒ๎‚’ ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚๎‚— ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚‰ static ๎‚—๎‚˜๎‚“๎‚–๎‚…๎‚‹๎‚‰ ๎‚‡๎‚๎‚…๎‚—๎‚— ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚–๎‡€ ๎€‹๎‚˜๎‚— ๎‚—๎‚˜๎‚“๎‚–๎‚…๎‚‹๎‚‰ ๎‚๎‚— ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ
๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚‰๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚๎‚ ๎‚—๎‚˜๎‚…๎‚‡๎‚๎‡€
a๎‡€ ๎€ƒ๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰๎‚ˆ ๎‚†๎‚ -Wall
b๎‡€ ๎€ƒ๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰๎‚ˆ ๎‚†๎‚ -Wall
c๎‡€ ๎€ƒ๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰๎‚ˆ ๎‚†๎‚ -Wall
32 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
Warning
๎€‡๎‚š๎‚‰๎‚’ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚…๎‚๎‚ ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚Š๎‚“๎‚–๎‚‡๎‚๎‚†๎‚๎‚ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚™๎‚—๎‚‰
๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚”๎‚–๎‚๎‚“๎‚– ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚๎‚— ๎‚—๎‚˜๎‚๎‚๎‚ ๎‚… ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚˜๎‚Œ๎‚…๎‚˜
๎‚…๎‚†๎‚—๎‚“๎‚๎‚™๎‚˜๎‚‰๎‚๎‚ ๎‚Œ๎‚…๎‚— ๎‚˜๎‚“ ๎‚†๎‚‰ ๎Šฑ๎‚œ๎‚‰๎‚ˆ๎‡€
๎€ƒ๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚๎‚— ๎‚˜๎‚Œ๎‚™๎‚— ๎‚… ๎‚Œ๎‚…๎‚–๎‚ˆ๎‚‰๎‚’๎‚๎‚’๎‚‹ ๎‚“๎‚’๎‚๎‚ ๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚ˆ๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚๎‚๎‚‘๎‚๎‚˜
๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚ ๎‚๎‚‘๎‚”๎‚…๎‚‡๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚†๎‚™๎‚‹๎‚—๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚— ๎‚… ๎‚‡๎‚“๎‚’๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ
๎‚’๎‚‰๎‚š๎‚‰๎‚– ๎‚–๎‚‰๎‚๎‚ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚๎‚— ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚–๎‡€
5.3.4 Integer over๎‚ฟows
๎€•๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‚— ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚™๎‚— ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚–๎‚๎‚ ๎‚ˆ๎‚…๎‚’๎‚‹๎‚‰๎‚–๎‚“๎‚™๎‚—๎‡€
๎€ˆ๎‚“๎‚– ๎‚๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚‡๎‚‰๎†ฝ ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚’ ๎‚Œ๎‚…๎‚–๎‚ˆ๎‚›๎‚…๎‚–๎‚‰ ๎‚…๎‚–๎‚‡๎‚Œ๎‚๎‚˜๎‚‰๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚—๎†ฝ ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚๎‚”๎‚‰ int ๎‚–๎‚‰๎‚…๎‚‡๎‚Œ๎‚๎‚’๎‚‹
๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ INT_MAX ๎‚‡๎‚…๎‚’ wrap ๎‚…๎Šน๎‚‰๎‚– ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎†ฝ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚— ๎‚†๎‚‰๎‚‡๎‚“๎‚‘๎‚‰ INT_MIN๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚‘๎‚…๎‚ ๎‚”๎‚–๎‚“๎‚š๎‚‰ ๎‚˜๎‚“
๎‚†๎‚‰ ๎‚•๎‚™๎‚๎‚˜๎‚‰ ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚…๎‚๎‚๎‚ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚— ๎‚… ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰ ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚– ๎‚Š๎‚“๎‚– ๎‚…
๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚‘๎‚…๎‚ ๎‚†๎‚‰ ๎‚…๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜ ๎‚‡๎‚‰๎‚–๎‚˜๎‚…๎‚๎‚’ ๎‚๎‚๎‚’๎‚ˆ๎‚— ๎‚“๎‚Š ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‚—๎‡€
RECO
41
RECOMMENDATION โ€” Enable compiler options that allow for detecting
signed integer over๎‚ฟows
๎€‹๎‚’ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚–๎†ฝ ๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ ๎‚†๎‚“๎‚˜๎‚Œ ๎‚—๎‚™๎‚”๎‚”๎‚“๎‚–๎‚˜ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ -ftrapv๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‡‚
๎‚”๎‚๎‚๎‚‰๎‚– ๎‚๎‚’๎‚—๎‚˜๎‚–๎‚™๎‚‘๎‚‰๎‚’๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚’ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚… ๎‚›๎‚…๎‚ ๎‚…๎‚— ๎‚˜๎‚“ ๎‚‰๎‚‘๎‚๎‚˜ ๎‚… ๎‚˜๎‚–๎‚…๎‚” ๎‚ˆ๎‚™๎‚–๎‚๎‚’๎‚‹ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘
๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚“๎‚– ๎‚…๎‚’๎‚ ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚› ๎‚“๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚—๎‚™๎‚†๎‚˜๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚– ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’
๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
Warning
๎€‡๎‚š๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚“๎‚™๎‚‹๎‚Œ ๎‚™๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‚— ๎‚…๎‚–๎‚‰ ๎‚…๎‚‡๎‚˜๎‚™๎‚…๎‚๎‚๎‚ ๎‚… ๎‚›๎‚‰๎‚๎‚๎‡‚๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚– ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚…๎‚’๎‚ ๎‚๎‚‰๎‚—๎‚— ๎‚ˆ๎‚…๎‚’๎‚‹๎‚‰๎‚–๎‚“๎‚™๎‚— ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚…๎‚ ๎‚Ž๎‚™๎‚—๎‚˜ ๎‚…๎‚— ๎‚›๎‚‰๎‚๎‚ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚˜๎‚–๎‚“๎‡‚
๎‚ˆ๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚†๎‚™๎‚‹๎‚— ๎‚…๎‚’๎‚ˆ ๎‚š๎‚™๎‚๎‚’๎‚‰๎‚–๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚๎‚‰๎‚— ๎‚๎‚’ ๎‚… ๎‚”๎‚๎‚‰๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚˜๎‚Œ๎‚™๎‚—
๎‚–๎‚‰๎‚‘๎‚…๎‚๎‚’ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚–๎‚๎‚ ๎‚‡๎‚…๎‚–๎‚‰๎‚Š๎‚™๎‚ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚๎‚’๎‚‹ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚”๎‚–๎‚“๎‚’๎‚‰ ๎‚˜๎‚“ ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‚—๎†ฝ ๎‚‰๎‚š๎‚‰๎‚’
๎‚›๎‚๎‚˜๎‚Œ ๎‚™๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ๎‚—๎‡€
Information
๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ ๎‚—๎‚™๎‚”๎‚”๎‚“๎‚–๎‚˜ ๎‚‘๎‚…๎‚’๎‚ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚™๎‚—๎‚‰๎‚Š๎‚™๎‚ ๎‚Š๎‚“๎‚– ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜๎‚๎‚’๎‚‹ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚–
๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‚—๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚…๎‚–๎‚‰ ๎‚”๎‚…๎‚–๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎€—๎€„๎€•๎‚…๎‚’
a
sanitizer๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚…๎‚— ๎‚›๎‚‰๎‚๎‚ ๎‚…๎‚— ๎‚“๎‚Š
๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚—๎‚…๎‚’๎‚๎‚˜๎‚๎‚ž๎‚‰๎‚–๎‚— ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚—๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰๎‡€
5.3.5 Call stack hardenings
๎€‹๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚‘๎‚…๎‚๎‚‰ ๎‚๎‚˜ ๎‚Œ๎‚…๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚‰๎‚œ๎‚”๎‚๎‚“๎‚๎‚˜ ๎‚‡๎‚‰๎‚–๎‚˜๎‚…๎‚๎‚’ ๎‚š๎‚™๎‚๎‚’๎‚‰๎‚–๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚๎‚‰๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚–๎‚‰๎‚… ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰
๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚—๎‚˜๎‚…๎‚‡๎‚ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚…๎‚†๎‚๎‚‰๎‡€ ๎€๎‚“๎‚ˆ๎‚‰๎‚–๎‚’ ๎‚˜๎‚“๎‚“๎‚๎‚‡๎‚Œ๎‚…๎‚๎‚’๎‚— ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚๎‚๎‚ ๎‚‰๎‚’๎‚ˆ๎‚‰๎‚…๎‚š๎‚“๎‚– ๎‚˜๎‚“ ๎‚‰๎‚’๎‚Š๎‚“๎‚–๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚๎‚—
๎‚–๎‚™๎‚๎‚‰ ๎‚†๎‚ ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜๎‡€
a๎‡€ ๎€—๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎€„๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚– ๎€•๎‚…๎‚’๎‚๎‚˜๎‚๎‚ž๎‚‰๎‚–
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 33
๎€๎‚“๎‚’๎‚‰๎‚˜๎‚Œ๎‚‰๎‚๎‚‰๎‚—๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚’๎‚‰๎‚—๎‚˜๎‚‰๎‚ˆ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚…๎‚— ๎‚—๎‚™๎‚”๎‚”๎‚“๎‚–๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎„Œ๎„ˆ๎„ˆ๎†ฝ ๎‚๎‚‘๎‚”๎‚“๎‚—๎‚‰๎‚— ๎‚…๎‚’ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚…๎‚†๎‚๎‚‰ ๎‚—๎‚˜๎‚…๎‚‡๎‚
๎ˆ“
๎‚…๎‚’๎‚ˆ
๎‚๎‚— ๎‚˜๎‚Œ๎‚™๎‚— ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ
๎ˆ”
๎‡€ ๎€๎‚“๎‚–๎‚‰๎‚“๎‚š๎‚‰๎‚–๎†ฝ ๎‚—๎‚™๎‚”๎‚”๎‚“๎‚–๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚๎‚— ๎‚Š๎‚‰๎‚…๎‚˜๎‚™๎‚–๎‚‰ ๎‚Œ๎‚…๎‚— ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚›๎‚…๎‚ ๎€‰๎€๎€— ๎‚๎‚๎‚’๎‚๎‚‰๎‚–๎‚— ๎‡ก
๎‚’๎‚…๎‚‘๎‚‰๎‚๎‚ ๎€„๎€ˆ๎€† ๎‚…๎‚’๎‚ˆ ๎‚‹๎‚“๎‚๎‚ˆ ๎‡ก ๎‚‡๎‚Œ๎‚“๎‚“๎‚—๎‚‰ ๎‚˜๎‚“ ๎‚‘๎‚…๎‚–๎‚ ๎‚…๎‚’ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚…๎‚†๎‚๎‚‰ ๎‚“๎‚– ๎‚… ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚…๎‚— ๎‚–๎‚‰๎‚•๎‚™๎‚๎‚–๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚…๎‚†๎‚๎‚‰ ๎‚—๎‚˜๎‚…๎‚‡๎‚๎†พ
๎‚๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚™๎‚— ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚š๎‚‰๎‚–๎‚ ๎‚‡๎‚…๎‚™๎‚˜๎‚๎‚“๎‚™๎‚— ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰๎‚‘๎‡€
RULE
42
RULE โ€” Do not use executable stack
๎€‹๎‚’ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚–๎†ฝ ๎„Œ๎„ˆ๎„ˆ ๎‚’๎‚‰๎‚—๎‚˜๎‚‰๎‚ˆ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€ ๎€„๎‚‰๎‚—๎‚๎‚ˆ๎‚‰๎‚—๎†ฝ ๎‚Š๎‚“๎‚– ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚๎‚’๎‡‚
๎‚˜๎‚‰๎‚’๎‚ˆ๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎€‰๎€๎€—๎‡š๎€Ž๎‚๎‚’๎‚™๎‚œ ๎‚“๎‚– ๎€ˆ๎‚–๎‚‰๎‚‰๎€„๎€•๎€† ๎‚‰๎‚’๎‚š๎‚๎‚–๎‚“๎‚’๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎†ฟ
n
๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ -z execstack ๎‚“๎‚Š ๎€„๎€ˆ๎€†๎†ฝ ๎‚‹๎‚“๎‚๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚๎‚๎‚ˆ ๎‚๎‚๎‚’๎‚๎‚‰๎‚–๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎†พ
n
๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ -z nognustack ๎‚“๎‚Š ๎‚๎‚๎‚ˆ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎†พ
n
๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ -z noexecstack ๎‚“๎‚Š ๎€„๎€ˆ๎€† ๎‚…๎‚’๎‚ˆ ๎‚‹๎‚“๎‚๎‚ˆ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎€•๎‚˜๎‚…๎‚‡๎‚ ๎‚†๎‚™๎Šฎ๎‚‰๎‚– ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‚— ๎‚‡๎‚‰๎‚–๎‚˜๎‚…๎‚๎‚’๎‚๎‚ ๎‚–๎‚…๎‚’๎‚ ๎‚…๎‚‘๎‚“๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚๎‚ˆ๎‚‰๎‚—๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚“๎‚—๎‚˜ ๎‚‡๎‚“๎‚‘๎‚‘๎‚“๎‚’ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚‡๎‚“๎‚–๎‚–๎‚™๎‚”๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎€’๎‚“๎‚—๎‚๎‚˜๎‚๎‚“๎‚’๎‚๎‚’๎‚‹ ๎‚‹๎‚™๎‚…๎‚–๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚–๎‚…๎‚’๎‚ˆ๎‚“๎‚‘ ๎‚š๎‚…๎‚๎‚™๎‚‰๎†ฝ ๎‚™๎‚—๎‚™๎‚…๎‚๎‚๎‚ ๎‚‡๎‚…๎‚๎‚๎‚‰๎‚ˆ canaries
๎ˆ•
๎†ฝ ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚๎‚˜ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚Š๎‚“๎‚–
๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜ ๎‚—๎‚“๎‚‘๎‚‰ ๎‚๎‚๎‚’๎‚‰๎‚…๎‚– ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚› ๎‚…๎‚˜๎‚˜๎‚‰๎‚‘๎‚”๎‚˜๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚–๎‚ ๎‚˜๎‚“ ๎‚“๎‚š๎‚‰๎‚–๎‚›๎‚–๎‚๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚—
๎‚—๎‚…๎‚š๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚‡๎‚๎‡€ ๎€‹๎‚Š ๎‚’๎‚‰๎‚‰๎‚ˆ ๎‚†๎‚‰๎†ฝ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚‰๎‚ˆ๎‡€
RULE
43
RULE โ€” Enable stack canaries
๎€™๎‚๎‚˜๎‚Œ ๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ๎†ฝ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ -fstack-protector-strong ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‰๎‚’๎‡‚
๎‚…๎‚†๎‚๎‚‰๎‚ˆ๎‡€
๎€•๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ๎ˆ˜๎‡€๎ˆ‘ ๎‚…๎‚๎‚—๎‚“ ๎‚˜๎‚…๎‚‡๎‚๎‚๎‚‰๎‚— ๎‚‘๎‚…๎‚’๎‚™๎‚…๎‚ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚‡๎‚…๎‚’๎‚…๎‚–๎‚ ๎‚‘๎‚‰๎‚‡๎‚Œ๎‚…๎‚’๎‚๎‚—๎‚‘๎‡€
๎€†๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚’ ๎‚Œ๎‚…๎‚–๎‚ˆ๎‚›๎‚…๎‚–๎‚‰ ๎‚…๎‚–๎‚‡๎‚Œ๎‚๎‚˜๎‚‰๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚—๎†ฝ ๎‚”๎‚๎‚…๎‚˜๎‚Š๎‚“๎‚–๎‚‘๎‚— ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚—๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚…๎‚๎‚—๎‚“ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰ ๎‚… ๎‚‹๎‚™๎‚…๎‚–๎‚ˆ
๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚›๎‚Œ๎‚“๎‚—๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚๎‚— ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚Š๎‚“๎‚– ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚˜๎‚Œ๎‚–๎‚‰๎‚…๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚Š๎‚“๎‚– ๎‚–๎‚‰๎‚ˆ๎‚™๎‚‡๎‚๎‚’๎‚‹
๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚… ๎‚”๎‚“๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚๎‚‰๎‚…๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚’๎‚…๎‚–๎‚ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‡€
RECO
44
RECOMMENDATION โ€” Use per-thread canaries
๎€‹๎‚’ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚–๎†ฝ ๎‚Š๎‚“๎‚– ๎‚œ๎ˆ—๎ˆ• ๎‚…๎‚–๎‚‡๎‚Œ๎‚๎‚˜๎‚‰๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚—๎†ฝ ๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ ๎‚—๎‚™๎‚”๎‚”๎‚“๎‚–๎‚˜ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’
-mstack-protector-guard=tls๎†ฝ ๎‚–๎‚‰๎‚๎‚๎‚๎‚’๎‚‹ ๎‚“๎‚’ ๎‚‹๎‚๎‚๎‚†๎‚‡ Thread Local Storage๎‡€
5.3.6 Dynamic loading
๎€”๎‚…๎‚’๎‚ˆ๎‚“๎‚‘๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚… ๎‚”๎‚–๎‚“๎‚†๎‚…๎‚†๎‚๎‚๎‚๎‚—๎‚˜๎‚๎‚‡ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚ ๎‚ˆ๎‚‰๎‚Š๎‚‰๎‚’๎‚—๎‚‰ ๎‚˜๎‚…๎‚‡๎‚˜๎‚๎‚‡๎‚— ๎‚…๎‚๎‚‘๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚‘๎‚…๎‚๎‚‰ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚š๎‚™๎‚๎‚’๎‚‰๎‚–๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚
๎‚‰๎‚œ๎‚”๎‚๎‚“๎‚๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚‰๎‚—๎‚— ๎‚–๎‚‰๎‚๎‚๎‚…๎‚†๎‚๎‚‰๎‡€ ๎€‹๎‚’ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚–๎†ฝ ๎‚‡๎‚“๎‚‘๎‚‘๎‚“๎‚’ ๎‚˜๎‚“๎‚“๎‚๎‚‡๎‚Œ๎‚…๎‚๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚…๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜
๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚๎‚“๎‚…๎‚ˆ๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎‚… ๎‚–๎‚…๎‚’๎‚ˆ๎‚“๎‚‘ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚—๎†ฝ ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚‘๎‚…๎‚๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚“๎‚—๎‚˜ ๎‚“๎‚Š ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚๎‚…๎‚๎‚“๎‚™๎‚˜
๎‚–๎‚…๎‚’๎‚ˆ๎‚“๎‚‘๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡ฆ๎€ƒ๎€•๎€Ž๎€”๎‡ง ๎‚…๎‚— ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‡€
๎ˆ“๎‡€ ๎€๎‚“๎‚–๎‚‰ ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚‰๎‚๎‚๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š closures๎†ฝ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎„Œ๎„ˆ๎„ˆ ๎‚†๎‚ ๎‚‘๎‚‰๎‚…๎‚’๎‚— ๎‚“๎‚Š ๎‚—๎‚™๎‚‡๎‚Œ ๎‚’๎‚‰๎‚—๎‚˜๎‚‰๎‚ˆ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚˜๎‚–๎‚…๎‚‘๎‚”๎‚“๎‚๎‚๎‚’๎‚‰๎‚— ๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚’
๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚‡๎‚๎†ฝ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚— ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‡€
๎ˆ”๎‡€ ๎€‹๎‚˜ ๎‚๎‚— ๎‚… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚‰๎‚œ๎‚˜๎‚‰๎‚’๎‚—๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚๎‚›๎‚…๎‚๎†ฝ ๎‚“๎Šฎ๎‚‰๎‚–๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚… ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎†ฝ ๎‚๎‚‰๎‚˜๎†ฝ ๎‚…๎‚— ๎‚… ๎‚–๎‚‰๎‚‘๎‚๎‚’๎‚ˆ๎‚‰๎‚–๎†ฝ ๎‚“๎‚’๎‚๎‚ ๎€… ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚’๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎€…๎ˆ˜๎ˆ ๎‚“๎‚– ๎€…๎ˆ˜๎ˆ˜ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‚—
๎‚๎‚— ๎‚…๎‚๎‚๎‚“๎‚›๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜ ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰๎‡€
๎ˆ•๎‡€ ๎€ƒ๎‚๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚˜๎‚๎‚š๎‚‰๎‚๎‚ ๎‚๎‚’๎‚“๎‚›๎‚’ ๎‚…๎‚— cookies
34 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
RULE
45
RULE โ€” Produce position independent executables
๎€™๎‚๎‚˜๎‚Œ ๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ๎†ฝ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ -fPIE ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰๎‚ˆ๎†ฝ ๎‚…๎‚— ๎‚›๎‚‰๎‚๎‚ ๎‚…๎‚— ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’
-pie ๎‚“๎‚Š ๎€„๎€ˆ๎€†๎†ฝ ๎‚‹๎‚“๎‚๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚๎‚๎‚ˆ ๎‚๎‚๎‚’๎‚๎‚‰๎‚–๎‚—๎‡€
๎€–๎‚“ ๎‚…๎‚๎‚๎‚“๎‚› ๎‚Š๎‚“๎‚– ๎‚–๎‚‰๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚—๎‚Œ๎‚…๎‚–๎‚‰๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚…๎‚†๎‚๎‚‰๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡ ๎‚๎‚“๎‚…๎‚ˆ๎‚‰๎‚– ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚‘๎‚“๎‚ˆ๎‡‚
๎‚๎‚Š๎‚ ๎‚—๎‚“๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€ ๎€‹๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚‘๎‚…๎‚”๎‚”๎‚๎‚’๎‚‹๎‚— ๎‚‡๎‚“๎‚’๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚˜๎‚๎‚ ๎‚–๎‚‰๎‚‘๎‚…๎‚๎‚’ ๎‚›๎‚–๎‚๎‚˜๎‚…๎‚†๎‚๎‚‰
๎‚ˆ๎‚™๎‚–๎‚๎‚’๎‚‹ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚‘๎‚…๎‚ ๎‚”๎‚–๎‚“๎‚š๎‚‰ ๎‚™๎‚—๎‚‰๎‚Š๎‚™๎‚ ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚…๎‚˜๎‚˜๎‚…๎‚‡๎‚๎‚‰๎‚– ๎‚˜๎‚–๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚‰๎‚œ๎‚”๎‚๎‚“๎‚๎‚˜ ๎‚… ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚š๎‚™๎‚๎‡‚
๎‚’๎‚‰๎‚–๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚๎‡€ ๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚…๎‚˜ ๎‚๎‚๎‚’๎‚๎‡‚๎‚˜๎‚๎‚‘๎‚‰ ๎‚˜๎‚“ ๎‚‘๎‚…๎‚–๎‚ ๎‚—๎‚…๎‚๎‚ˆ ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚“ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡ ๎‚๎‚“๎‚…๎‚ˆ๎‚‰๎‚–
๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚๎‚๎‚๎‚’๎‚‹ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚‘๎‚…๎‚”๎‚”๎‚๎‚’๎‚‹๎‚— ๎‚–๎‚‰๎‚…๎‚ˆ๎‡‚๎‚“๎‚’๎‚๎‚ ๎‚…๎‚— ๎‚—๎‚“๎‚“๎‚’ ๎‚…๎‚— ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚๎‚— ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚–๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚— relro
๎‚“๎‚– partial relro ๎‚‘๎‚“๎‚ˆ๎‚‰๎‡€
RULE
46
RULE โ€” Use relro mode of linkers
๎€ˆ๎‚“๎‚– ๎‚๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚‡๎‚‰๎†ฝ ๎‚›๎‚๎‚˜๎‚Œ ๎€„๎€ˆ๎€† ๎‚…๎‚’๎‚ˆ ๎‚‹๎‚“๎‚๎‚ˆ ๎‚๎‚๎‚’๎‚๎‚‰๎‚–๎‚—๎†ฝ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ -z relro ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€ ๎‚๎‚๎‚ˆ ๎‚‰๎‚’๎‚Š๎‚“๎‚–๎‚‡๎‚‰๎‚—
relro ๎‚‘๎‚“๎‚ˆ๎‚‰ ๎‚†๎‚ ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜ ๎‚˜๎‚Œ๎‚™๎‚— ๎‚’๎‚“ ๎‚‰๎‚œ๎‚˜๎‚–๎‚… ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚–๎‚‰๎‚•๎‚™๎‚๎‚–๎‚‰๎‚ˆ๎‡€
๎€๎‚‰๎‚š๎‚‰๎‚–๎‚˜๎‚Œ๎‚‰๎‚๎‚‰๎‚—๎‚—๎†ฝ ๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚๎‚‘๎‚†๎‚“๎‚๎‚— ๎‚–๎‚‰๎‚—๎‚“๎‚๎‚™๎‚˜๎‚๎‚“๎‚’ ๎‚™๎‚—๎‚™๎‚…๎‚๎‚๎‚ ๎‚Œ๎‚…๎‚”๎‚”๎‚‰๎‚’๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚™๎‚–๎‚—๎‚‰ ๎‚“๎‚Š ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‡‚
๎‚˜๎‚๎‚“๎‚’ ๎‡ฆlazy binding๎‡ง๎†ฝ ๎‚… ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚—๎‚Œ๎‚…๎‚–๎‚‰๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‚‰๎‚— ๎‚“๎‚– ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚–๎‚‰๎‚‘๎‚…๎‚๎‚’ ๎‚›๎‚–๎‚๎‚˜๎‚…๎‚†๎‚๎‚‰
๎‚–๎‚‰๎‚‹๎‚…๎‚–๎‚ˆ๎‚๎‚‰๎‚—๎‚— ๎‚“๎‚Š relro ๎‚‘๎‚“๎‚ˆ๎‚‰๎‡€ ๎€‹๎‚— ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚Š๎‚“๎‚–๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡ ๎‚๎‚“๎‚…๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚–๎‚‰๎‚—๎‚“๎‚๎‚š๎‚‰ ๎‚…๎‚๎‚ ๎‚—๎‚๎‚‘๎‚†๎‚“๎‚๎‚—
๎‚›๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚๎‚— ๎‚—๎‚˜๎‚…๎‚–๎‚˜๎‚‰๎‚ˆ
๎ˆ–
๎‚—๎‚“ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚˜ ๎‚‡๎‚…๎‚’ ๎‚‹๎‚“ ๎‚“๎‚’ ๎‚˜๎‚“ ๎‚‘๎‚…๎‚๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚๎‚๎‚๎‚’๎‚‹ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚‘๎‚…๎‚”๎‚”๎‚๎‚’๎‚‹๎‚— ๎‚–๎‚‰๎‚…๎‚ˆ๎‡‚
๎‚“๎‚’๎‚๎‚๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚๎‚— ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚–๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚— full relro ๎‚“๎‚– BIND_NOW ๎‚‘๎‚“๎‚ˆ๎‚‰๎‡€
RECO
47
RECOMMENDATION โ€” Do not use lazy binding
๎€ˆ๎‚“๎‚– ๎‚๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚‡๎‚‰๎†ฝ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ -z now ๎‚“๎‚Š ๎€„๎€ˆ๎€†๎†ฝ ๎‚‹๎‚“๎‚๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚๎‚๎‚ˆ ๎‚‡๎‚…๎‚™๎‚—๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚๎‚๎‚’๎‚๎‚‰๎‚–๎‚— ๎‚˜๎‚“ ๎‚‘๎‚…๎‚–๎‚ ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‡‚
๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‚‰๎‚— ๎‚—๎‚“ ๎‚…๎‚— ๎‚˜๎‚“ ๎‚˜๎‚‰๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡ ๎‚๎‚“๎‚…๎‚ˆ๎‚‰๎‚– ๎‚๎‚˜ ๎‚’๎‚‰๎‚‰๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚–๎‚‰๎‚—๎‚“๎‚๎‚š๎‚‰ ๎‚…๎‚๎‚
๎‚—๎‚๎‚‘๎‚†๎‚“๎‚๎‚— ๎‚…๎‚˜ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚—๎‚˜๎‚…๎‚–๎‚˜๎‚™๎‚”๎‡€
Information
๎€™๎‚Œ๎‚‰๎‚’ relro ๎‚‘๎‚“๎‚ˆ๎‚‰ ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ lazy binding ๎‚๎‚— ๎‚ˆ๎‚๎‚—๎‚…๎‚†๎‚๎‚‰๎‚ˆ๎†ฝ ๎‚—๎‚“๎‚‘๎‚‰ ๎‚๎‚๎‚’๎‚๎‚‰๎‚–๎‚— ๎‚–๎‚‰๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—
๎‚๎‚’๎‚—๎‚๎‚ˆ๎‚‰ ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚๎‚’๎‚…๎‚–๎‚๎‚‰๎‚— ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚”๎‚–๎‚‰๎‚š๎‚‰๎‚’๎‚˜ ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‚— ๎‚“๎‚Š ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚“๎‚’๎‚‰ ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’
๎‚Š๎‚–๎‚“๎‚‘ ๎‚“๎‚š๎‚‰๎‚–๎‚›๎‚–๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎€–๎‚Œ๎‚๎‚— ๎‚๎‚— ๎‚’๎‚“๎‚˜๎‚…๎‚†๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎€„๎€ˆ๎€†๎†ฝ ๎‚‹๎‚“๎‚๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚๎‚๎‚ˆ๎‡€
5.3.7 Reproducible builds
๎€ƒ๎‚‘๎‚“๎‚’๎‚‹ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚˜๎‚Œ๎‚๎‚’๎‚‹๎‚—๎†ฝ ๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚๎‚†๎‚๎‚‰ ๎‚†๎‚™๎‚๎‚๎‚ˆ๎‚— ๎‚…๎‚๎‚๎‚“๎‚› ๎‚™๎‚—๎‚‰๎‚–๎‚— ๎‚“๎‚Š ๎‚… ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚– ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚˜๎‚“ ๎‚š๎‚‰๎‚–๎‚๎‚Š๎‚ ๎‚๎‚’๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‡‚
๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚… ๎‚†๎‚๎‚’๎‚…๎‚–๎‚ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚…๎‚–๎‚‰ ๎‚”๎‚–๎‚“๎‚š๎‚๎‚ˆ๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚๎‚— ๎‚๎‚’๎‚ˆ๎‚‰๎‚‰๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚’๎‚…๎‚๎‚˜๎‚‰๎‚–๎‚‰๎‚ˆ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚˜ ๎‚“๎‚Š ๎‚… ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰ ๎‚“๎‚Š
๎‚๎‚˜๎‚— ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰๎‚—๎†ฝ ๎‚†๎‚ ๎‚–๎‚‰๎‚†๎‚™๎‚๎‚๎‚ˆ๎‚๎‚’๎‚‹ ๎‚๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚–๎‚‰๎‚•๎‚™๎‚๎‚–๎‚‰๎‚— ๎‚… ๎‚Š๎‚™๎‚๎‚๎‚ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚๎‚—๎‚˜๎‚๎‚‡ ๎‚†๎‚™๎‚๎‚๎‚ˆ ๎‚”๎‚–๎‚“๎‡‚
๎‚‡๎‚‰๎‚—๎‚—๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚๎‚— ๎‚’๎‚“๎‚’๎‚˜๎‚–๎‚๎‚š๎‚๎‚…๎‚ ๎‚…๎‚’๎‚ˆ ๎‚‹๎‚“๎‚‰๎‚— ๎‚†๎‚‰๎‚๎‚“๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‡๎‚“๎‚”๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚˜๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚๎‚— ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚๎‚’๎‚˜๎‚“
๎‚‡๎‚“๎‚’๎‚—๎‚๎‚ˆ๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚— ๎‚—๎‚“๎‚“๎‚’ ๎‚…๎‚— ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚๎‚’ ๎‚… ๎‚”๎‚–๎‚“๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚‡๎‚…๎‚’ ๎‚‹๎‚–๎‚‰๎‚…๎‚˜๎‚๎‚ ๎‚Š๎‚…๎‚‡๎‚๎‚๎‚๎‚˜๎‚…๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…
๎‚Š๎‚‰๎‚…๎‚˜๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚…๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚˜ ๎‚†๎‚‰๎‚‡๎‚“๎‚‘๎‚‰๎‚— ๎‚…๎‚’ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‡€
๎ˆ–๎‡€ ๎€†๎‚๎‚—๎‚…๎‚†๎‚๎‚๎‚’๎‚‹ lazy binding ๎‚‘๎‚๎‚‹๎‚Œ๎‚˜ ๎‚˜๎‚Œ๎‚™๎‚— ๎‚—๎‚๎‚“๎‚› ๎‚ˆ๎‚“๎‚›๎‚’ ๎‚… ๎‚๎‚…๎‚–๎‚‹๎‚‰ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰๎‡‹๎‚— ๎‚—๎‚˜๎‚…๎‚–๎‚˜๎‚™๎‚”๎‡€ ๎€‹๎‚˜ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚…๎‚๎‚—๎‚“ ๎‚†๎‚‰ ๎‚’๎‚“๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚๎‚— ๎‚†๎‚‰๎‚‡๎‚“๎‚‘๎‚‰๎‚— ๎‚“๎‚Š ๎‚๎‚๎‚˜๎‚˜๎‚๎‚‰
๎‚๎‚‘๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚’๎‚‡๎‚‰ ๎‚๎‚’ ๎‚”๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚‡๎‚‰ ๎‚Š๎‚“๎‚– ๎‚… daemon๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 35
GOOD
PRACTICE
48
GOOD PRACTICE โ€” Ensure reproducible builds
๎€ˆ๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— -Wdate-time ๎‚Š๎‚–๎‚“๎‚‘ ๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ ๎‚“๎‚–
-frandom-seed= ๎‚Š๎‚–๎‚“๎‚‘ ๎„Œ๎„ˆ๎„ˆ ๎‚‘๎‚…๎‚ ๎‚”๎‚–๎‚“๎‚š๎‚‰ ๎‚™๎‚—๎‚‰๎‚Š๎‚™๎‚ ๎‚˜๎‚“ ๎‚๎‚๎‚‘๎‚๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚’๎‚“๎‚’๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚–๎‡‚
๎‚‘๎‚๎‚’๎‚๎‚—๎‚‘ ๎‚…๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‡‚๎‚˜๎‚๎‚‘๎‚‰๎‡€
5.4 Debug and release modes
๎€„๎‚™๎‚๎‚๎‚ˆ ๎‚‘๎‚“๎‚ˆ๎‚‰๎‚— debug ๎‚…๎‚’๎‚ˆ release ๎‚…๎‚–๎‚‰ ๎‚™๎‚—๎‚™๎‚…๎‚๎‚๎‚ ๎‚†๎‚“๎‚˜๎‚Œ ๎‚…๎‚š๎‚…๎‚๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚…๎‚’๎‚ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚…๎‚–๎‚‰ ๎‚š๎‚‰๎‚–๎‚ ๎‚™๎‚—๎‚‰๎‚Š๎‚™๎‚
๎‚Š๎‚“๎‚– ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚†๎‚‰๎‚‡๎‚…๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚‹๎‚’๎‚๎Šฑ๎‚‡๎‚…๎‚’๎‚˜ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚‡๎‚…๎‚’ ๎‚๎‚’๎‚ˆ๎‚™๎‚‡๎‚‰ ๎‚“๎‚’ ๎‚†๎‚™๎‚๎‚๎‚ˆ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚˜๎‚—๎‡€
Debug and release modes
๎€‹๎‚’ debug ๎‚‘๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚๎‚— ๎‚‘๎‚…๎‚๎‚’๎‚๎‚ ๎‚ˆ๎‚‰๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚ˆ๎‚‰๎‚†๎‚™๎‚‹๎‚‹๎‚๎‚’๎‚‹ ๎‚›๎‚Œ๎‚๎‚๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚๎‚’๎‚‹๎†ฝ ๎‚‘๎‚“๎‚—๎‚˜ ๎‚“๎‚”๎‚˜๎‚๎‡‚
๎‚‘๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚๎‚—๎‚…๎‚†๎‚๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚‰๎‚†๎‚™๎‚‹๎‚‹๎‚๎‚’๎‚‹ ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚“๎‚– ๎‚…๎‚๎‚ ๎‚—๎‚๎‚‘๎‚†๎‚“๎‚๎‚— ๎‚๎‚— ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ๎†ฝ ๎‚‘๎‚…๎‚๎‡‚
๎‚๎‚’๎‚‹ ๎‚๎‚˜ ๎‚‰๎‚…๎‚—๎‚๎‚‰๎‚– ๎‚๎‚’ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚– ๎‚˜๎‚“ ๎‚—๎‚‰๎‚˜ ๎‚†๎‚–๎‚‰๎‚…๎‚๎‚”๎‚“๎‚๎‚’๎‚˜๎‚—๎‡€ ๎€…๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚Š๎‚…๎‚—๎‚˜๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚™๎‚—๎‚‰๎‚— ๎‚๎‚‰๎‚—๎‚— ๎‚‘๎‚‰๎‚‘๎‡‚
๎‚“๎‚–๎‚๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚›๎‚๎‚๎‚ ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚๎‚…๎‚–๎‚‹๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚—๎‚๎‚“๎‚›๎‚‰๎‚– ๎‚˜๎‚“ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚‰๎‡€ ๎€‹๎‚’ release
๎‚‘๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚’๎‚…๎‚ ๎‚‘๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚™๎‚๎‚˜๎‚…๎‚†๎‚๎‚‰ ๎‚Š๎‚“๎‚– ๎‚‡๎‚๎‚๎‚‰๎‚’๎‚˜ ๎‚ˆ๎‚‰๎‚๎‚๎‚š๎‚‰๎‚–๎‚ ๎‚“๎‚– ๎‚ˆ๎‚‰๎‚”๎‚๎‚“๎‚๎‚๎‚’๎‚‹
๎‚˜๎‚“ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚…๎‚ˆ๎‚ˆ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚˜๎‚“
๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— ๎‚—๎‚๎‚‘๎‚†๎‚“๎‚๎‚—๎†ฝ ๎‚๎‚— ๎‚–๎‚‰๎‚‘๎‚“๎‚š๎‚‰๎‚ˆ๎‡€ ๎€…๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚…๎‚๎‚‰๎‚— ๎‚‘๎‚“๎‚–๎‚‰ ๎‚˜๎‚๎‚‘๎‚‰ ๎‚˜๎‚Œ๎‚‰๎‚’
๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚’๎‚—๎‚™๎‚‘๎‚‰๎‚— ๎‚‘๎‚“๎‚–๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚Š๎‚“๎‚– ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ ๎‚‘๎‚…๎‚‡๎‚Œ๎‚๎‚’๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰
๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚›๎‚๎‚๎‚ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚†๎‚ ๎‚†๎‚‰ ๎‚Š๎‚…๎‚—๎‚˜๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚‡๎‚˜๎‡€
Debug ๎‚‘๎‚“๎‚ˆ๎‚‰ ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚˜๎‚“ ๎‚†๎‚‰๎‚˜๎‚˜๎‚‰๎‚– ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚Œ๎‚“๎‚› ๎‚… ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚›๎‚“๎‚–๎‚๎‚— ๎‚…๎‚’๎‚ˆ ๎Šฑ๎‚œ ๎‚–๎‚‰๎‚”๎‚“๎‚–๎‚˜๎‚‰๎‚ˆ
๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚›๎‚Œ๎‚‰๎‚–๎‚‰๎‚…๎‚— release ๎‚‘๎‚“๎‚ˆ๎‚‰ ๎‚๎‚— ๎‚–๎‚‰๎‚•๎‚™๎‚๎‚–๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚ˆ๎‚‰๎‚๎‚๎‚š๎‚‰๎‚–๎‚ ๎‚Š๎‚“๎‚– ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚’๎‚‡๎‚‰ ๎‚“๎‚– ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚—๎‚๎‚ž๎‚‰ ๎‚–๎‚‰๎‚…๎‚—๎‚“๎‚’๎‚—๎‡€ ๎€ˆ๎‚“๎‚–
๎‚๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚‡๎‚‰๎†ฝ ๎‚๎‚’ debug ๎‚‘๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚—๎‚“๎‚‘๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‚‘๎‚…๎‚๎‚‰ ๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚ˆ
๎‚˜๎‚“ 0๎†ฝ ๎‚›๎‚Œ๎‚๎‚๎‚‰ ๎‚๎‚’ release ๎‚‘๎‚“๎‚ˆ๎‚‰ ๎‚˜๎‚Œ๎‚๎‚— ๎‚“๎‚’๎‚๎‚ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚— ๎‚˜๎‚“ ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎†ฝ ๎‚…๎‚— ๎‚”๎‚‰๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‡€
Information
๎€‹๎‚Š NDEBUG ๎‚๎‚— ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚›๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‡‚
๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– assert.h ๎‚๎‚— ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚ˆ๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ assert ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚๎‚— ๎‚–๎‚‰๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚†๎‚‰
๎‚ˆ๎‚๎‚—๎‚…๎‚†๎‚๎‚‰๎‚ˆ๎‡€
RULE
49
RULE โ€” All production-ready code must be compiled in release mode
๎€…๎‚“๎‚‘๎‚”๎‚๎‚๎‚๎‚’๎‚‹ ๎‚๎‚’ release ๎‚‘๎‚“๎‚ˆ๎‚‰ ๎‚๎‚— ๎‚‘๎‚…๎‚’๎‚ˆ๎‚…๎‚˜๎‚“๎‚–๎‚ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚”๎‚™๎‚˜๎‚˜๎‚๎‚’๎‚‹ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚๎‚’๎‚˜๎‚“ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
๎€–๎‚Œ๎‚๎‚— ๎‚‡๎‚…๎‚’ ๎‚—๎‚‰๎‚‰๎‚‘ ๎‚–๎‚‰๎‚ˆ๎‚™๎‚’๎‚ˆ๎‚…๎‚’๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ ๎‚–๎‚™๎‚๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚…๎‚’๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚Š๎‚–๎‚“๎‚‘ ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ”๎‡€๎ˆ ๎‚…๎‚’๎‚ˆ ๎ˆ”๎‡€๎ˆ‘ ๎‚†๎‚™๎‚˜ ๎‚๎‚˜ ๎‚๎‚— ๎‚ˆ๎‚‰๎‚Š๎‡‚
๎‚๎‚’๎‚๎‚˜๎‚‰๎‚๎‚ ๎‚… ๎‚–๎‚…๎‚˜๎‚Œ๎‚‰๎‚– ๎‚‡๎‚“๎‚‘๎‚‘๎‚“๎‚’ ๎‚‘๎‚๎‚—๎‚˜๎‚…๎‚๎‚‰ ๎‚๎‚’ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚‰๎‚’๎‚‹๎‚๎‚’๎‚‰๎‚‰๎‚–๎‚๎‚’๎‚‹๎‡€ ๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚“ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚–๎‚— ๎‚–๎‚‰๎‡‚
๎‚‹๎‚…๎‚–๎‚ˆ๎‚๎‚’๎‚‹ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚‘๎‚…๎‚’๎‚…๎‚‹๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ debug ๎‚‘๎‚“๎‚ˆ๎‚‰ ๎‚‘๎‚…๎‚ ๎‚‰๎‚š๎‚‰๎‚’ ๎‚—๎‚“๎‚‘๎‚‰๎‚˜๎‚๎‚‘๎‚‰๎‚— ๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚…๎‚—๎‚‰
๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚…๎‚˜๎‚˜๎‚…๎‚‡๎‚ ๎‚—๎‚™๎‚–๎‚Š๎‚…๎‚‡๎‚‰๎‡€ ๎€‹๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚™๎‚— ๎‚š๎‚‰๎‚–๎‚ ๎‚๎‚‘๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚’๎‚˜ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚‘๎‚…๎‚๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚‘๎‚“๎‚ˆ๎‚‰๎‚— ๎‚๎‚’
๎‚Š๎‚™๎‚๎‚ ๎‚๎‚’๎‚“๎‚›๎‚๎‚‰๎‚ˆ๎‚‹๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚™๎‚—๎‚‰๎‡€
36 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
RECO
50
RECOMMENDATION โ€” Pay special attention to debug and release modes
when building a project
๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š debug ๎‚…๎‚’๎‚ˆ release ๎‚‘๎‚“๎‚ˆ๎‚‰๎‚— ๎‚…๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‡‚๎‚˜๎‚๎‚‘๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚“๎‚’๎‚‰ ๎‚›๎‚Œ๎‚๎‚๎‚‰ ๎‚‘๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚—๎‚™๎‚–๎‚‰
๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚๎‚ ๎‚๎‚’๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰๎‚— ๎‚–๎‚‰๎‚‹๎‚…๎‚–๎‚ˆ๎‚๎‚’๎‚‹ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚‘๎‚…๎‚’๎‚…๎‚‹๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚–๎‚‰ ๎‚›๎‚‰๎‚๎‚
๎‚๎‚’๎‚“๎‚›๎‚’๎‡€ ๎€‡๎‚š๎‚‰๎‚–๎‚ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚˜๎‚›๎‚“ ๎‚‘๎‚“๎‚ˆ๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚‰๎‚œ๎‚Œ๎‚…๎‚™๎‚—๎‡‚
๎‚˜๎‚๎‚š๎‚‰๎‚๎‚๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 37
6
Declaration, de๎‚พnition and initialisation
De๎‚พnition versus use of variables
๎€†๎‚‰๎Šฑ๎‚’๎‚๎‚’๎‚‹ ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚‘๎‚‰๎‚…๎‚’๎‚— ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚๎‚’๎‚‹ ๎‚… ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚˜๎‚“ ๎‚๎‚˜ ๎‡ฆi.e. ๎‚›๎‚–๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‡‹๎‚— ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚
๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚—๎‡ง๎†ฝ ๎‚›๎‚Œ๎‚๎‚๎‚‰ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚‘๎‚‰๎‚…๎‚’๎‚— ๎‚–๎‚‰๎‚…๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‡ฆi.e. ๎‚–๎‚‰๎‚…๎‚ˆ๎‚๎‚’๎‚‹
๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚—๎‚˜๎‚“๎‚–๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚—๎‡ง๎‡€
6.1 Multiple variable declarations
๎€–๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚™๎‚‘๎‚™๎‚๎‚˜๎‚…๎‚’๎‚‰๎‚“๎‚™๎‚— ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚˜๎‚๎‚”๎‚‰๎‡€ ๎€‹๎‚’ ๎‚‘๎‚™๎‚๎‡‚
๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚๎‚— ๎‚—๎‚‰๎‚”๎‚…๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚‡๎‚“๎‚‘๎‚‘๎‚…๎‡€ ๎€–๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—
๎‚…๎‚–๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰ ๎‚… ๎‚‹๎‚๎‚š๎‚‰๎‚’ ๎‚˜๎‚๎‚”๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚‹๎‚–๎‚“๎‚™๎‚” ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚“๎‚– ๎‚˜๎‚“ ๎‚‹๎‚–๎‚“๎‚™๎‚” ๎‚˜๎‚“๎‚‹๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚–๎‚‰๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎‡€
๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚“๎‚’๎‚๎‚ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚—๎‚๎‚‘๎‚”๎‚๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‡ฆ๎‚’๎‚“ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚—
๎‚“๎‚– ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎‡ง ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚˜๎‚๎‚”๎‚‰๎‡€
RECO
51
RECOMMENDATION โ€” Only multiple declarations of simple variables of the
same type are authorised
๎€‹๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚…๎‚๎‚—๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎†ฝ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‡๎‚“๎‚™๎‚”๎‚๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰
๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚–๎‚‰ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ๎‡€ ๎€‹๎‚’๎‚ˆ๎‚‰๎‚‰๎‚ˆ๎†ฝ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚—๎‚๎‚’๎‚‹๎‚๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚“๎‚’๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚…๎‚—๎‚˜ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚๎‚— ๎‚…๎‚‡๎‚˜๎‚™๎‚…๎‚๎‚๎‚ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰๎‚ˆ๎‡€
RULE
52
RULE โ€” Do not make multiple variable declarations associated with an
initialisation
๎€‹๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‡ฆi.e. ๎‚‡๎‚“๎‚’๎‚—๎‚‰๎‚‡๎‚™๎‚˜๎‚๎‚š๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡ง ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰
๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚–๎‚‰ ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚†๎‚‰๎‚๎‚“๎‚›๎†ฝ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚“๎‚’๎‚๎‚ ๎‚˜๎‚Œ๎‚‰
๎‚๎‚…๎‚—๎‚˜ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚๎‚— ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰๎‚ˆ๎‡€
uint32_t abs , ord = 0; /* caution , the variable abs is not set to zero here ! */
uint32_t a, *b; /* to be prohibited: mix of simple variable and pointer
declaration */
struct blob_t g, h[35]; /* to be prohibited : mix of simple variable , pointer and
array declaration */
38 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
Good example
uint32_t a,
uint32_t *b; /* separation of the simple variable and the pointer */
struct blob_t g;
struct blob_t h[35]; /* as above for separation of the array and simple variable
*/
uint32_t abs , ord; /* joint declaration of two functionally -related variables */
abs = 0; /* assignment of the two variables */
ord = 0;
6.1.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ๎ˆ“๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚“๎‚’๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚”๎‚‰๎‚– ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
6.2 Free declaration of variables
๎€•๎‚๎‚’๎‚‡๎‚‰ ๎€…๎ˆ˜๎ˆ˜๎†ฝ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚…๎‚’๎‚๎‚›๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚Š๎‚‰๎‚…๎‚˜๎‚™๎‚–๎‚‰ ๎‚—๎‚‰๎‚‰๎‚‘๎‚— ๎‚”๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚‡๎‚…๎‚๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚๎‚˜๎‚—
๎‚…๎‚†๎‚™๎‚—๎‚‰ ๎‚‡๎‚…๎‚’ ๎‚‘๎‚…๎‚๎‚‰ ๎‚–๎‚‰๎‚…๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚๎‚‹๎‚’๎‚๎Šฑ๎‚‡๎‚…๎‚’๎‚˜๎‚๎‚ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚…๎‚ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚–๎‚‰๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’
๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎‡€
RECO
53
RECOMMENDATION โ€” Group variable declarations at the beginning of the
block in which they are used
๎€ˆ๎‚“๎‚– ๎‚–๎‚‰๎‚…๎‚—๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚–๎‚‰๎‚…๎‚ˆ๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚–๎‚‰๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚˜๎‚“
๎‚†๎‚‰ ๎‚‹๎‚–๎‚“๎‚™๎‚”๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚‰๎‚‹๎‚๎‚’๎‚’๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚๎‚‰๎†ฝ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚– ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚๎‚“๎‚‡๎‚ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜
๎‚…๎‚‡๎‚‡๎‚“๎‚–๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚—๎‚‡๎‚“๎‚”๎‚‰๎‡€
Information
๎€–๎‚Œ๎‚๎‚— ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‚๎‚ ๎‚–๎‚‰๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚ ๎‚†๎‚™๎‚˜ ๎‚Œ๎‚…๎‚— ๎‚…๎‚’ ๎‚๎‚‘๎‚”๎‚…๎‚‡๎‚˜ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰
๎‚–๎‚‰๎‚…๎‚ˆ๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚๎†ฝ ๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚…๎‚’๎‚ˆ๎‡š๎‚“๎‚– ๎‚‘๎‚…๎‚๎‚’๎‚˜๎‚…๎‚๎‚’๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚’๎‚‡๎‚‰๎‚–๎‚’๎‚— ๎‚…๎‚๎‚ ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚“๎‚Š
๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜๎‡€
๎€–๎‚Œ๎‚‰ -Wdeclaration-after-statement ๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚’ ๎‚Œ๎‚‰๎‚๎‚”
๎‚›๎‚๎‚˜๎‚Œ ๎‚‰๎‚’๎‚Š๎‚“๎‚–๎‚‡๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚๎‚— ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
๎€ƒ ๎‚š๎‚‰๎‚–๎‚ ๎‚‡๎‚“๎‚‘๎‚‘๎‚“๎‚’ ๎‚”๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚‡๎‚‰ ๎‚Š๎‚“๎‚– ๎‚๎‚“๎‚“๎‚” ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚๎‚— ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰๎‚‘ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚๎‚“๎‚“๎‚”๎‡€ ๎€–๎‚Œ๎‚๎‚—
๎‡Œ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎Šท๎‚๎‡ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚…๎‚‡๎‚‡๎‚‰๎‚”๎‚˜๎‚‰๎‚ˆ๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚… ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚…๎‚ ๎‚‡๎‚…๎‚–๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚˜๎‚…๎‚๎‚‰๎‚’ ๎‚˜๎‚“ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ
๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚‘๎‚…๎‚—๎‚ ๎‚“๎‚’๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚“๎‚ˆ๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚“๎‚“๎‚”๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‡Œ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎Šท๎‚๎‡ ๎‚…๎‚’๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚๎‚’ ๎‚… ๎‚‹๎‚–๎‚“๎‚™๎‚”๎‚‰๎‚ˆ
๎‚…๎‚’๎‚ˆ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ ๎‚‘๎‚…๎‚’๎‚’๎‚‰๎‚–๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚”๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚‡๎‚‰ ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚๎‚˜ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ ๎‚˜๎‚“ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎‚Š๎‚ ๎‚…๎‚๎‚
๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ๎†ฝ ๎‚˜๎‚Œ๎‚™๎‚— ๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚…๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚๎‚—๎‚ ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚—๎‚Œ๎‚…๎‚ˆ๎‚“๎‚›๎‚๎‚’๎‚‹๎‡€
#include <stdint .h>
uint8_t glob_var; /* global variable */
uint8_t fonc(void)
{
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 39
uint8_t var1; /* local variable */
if (glob_var >=0)
{
/* ... */
}
else
var1 = glob_var;
uint8_t var2; /* other local variable declared in the middle of a block */
/* ... */
}
uint8_t glob_var2; /* other global variable declared between two functions */
void main( void)
{
uint8_t x = fonc ();
/* ... */
}
Good example
๎€–๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚… ๎‚‹๎‚–๎‚“๎‚™๎‚”๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ ๎‚›๎‚…๎‚ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚‰๎‚‹๎‚๎‚’๎‚’๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎‚†๎‚๎‚“๎‚‡๎‚๎‚—๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰๎‚‘ ๎‚‰๎‚…๎‚—๎‚๎‚‰๎‚– ๎‚˜๎‚“ ๎‚–๎‚‰๎‚…๎‚ˆ๎‡€
#include <stdint .h>
uint8_t glob_var; /* global variable declared together */
uint8_t glob_var2;
uint8_t fonc(void)
{
uint8_t var1; /* local variables declared together at the beginning of the
function */
uint8_t var2;
if (glob_var >= 0)
{
/* ... */
}
else
{
var1 = glob_var;
}
/* ... */
}
void main( void)
{
uint8_t x = fonc ();
/* ... */
}
6.3 Declaration of constants
๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚๎‚‡๎‚…๎‚ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‡ฆ๎‚“๎‚– ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‚— ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚“๎‚– literals๎‡ง ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰
๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚‘๎‚…๎‚๎‚’๎‚˜๎‚…๎‚๎‚’๎‡€ ๎€‹๎‚Š ๎‚… ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚๎‚— ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰๎‚ˆ๎†ฝ ๎‚“๎‚’๎‚‰ ๎‚Œ๎‚…๎‚— ๎‚˜๎‚“ ๎‚–๎‚‰๎‚‘๎‚‰๎‚‘๎‚†๎‚‰๎‚– ๎‚˜๎‚“ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰ ๎‚…๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚๎‚’
๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
RULE
54
RULE โ€” Do not use hard-coded values
๎€–๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚—๎‡€
๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚–๎‚™๎‚๎‚‰ ๎‚๎‚— ๎‚…๎‚๎‚—๎‚“ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚…๎‚๎‚ ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚“๎‚Š ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚…๎‚”๎‚”๎‚‰๎‚…๎‚–๎‚๎‚’๎‚‹ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚˜๎‚๎‚‘๎‚‰๎‚— ๎‚๎‚’
๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€ ๎€–๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰๎†ฝ ๎‚๎‚Š ๎‚… ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‚“๎‚– ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹ ๎‚๎‚— ๎‚–๎‚‰๎‚”๎‚‰๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚˜๎‚๎‚‘๎‚‰๎‚—๎†ฝ ๎‚๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚…๎‚๎‚—๎‚“ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ
๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚… ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ const ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚“๎‚–๎†ฝ ๎‚Š๎‚…๎‚๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚…๎‚˜๎†ฝ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚… ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‡€
40 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎€…๎‚‰๎‚’๎‚˜๎‚–๎‚…๎‚๎‚๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚๎‚— ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚ˆ ๎‚…๎‚‡๎‚–๎‚“๎‚—๎‚—
๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚’๎‚˜๎‚๎‚–๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
GOOD
PRACTICE
55
GOOD PRACTICE โ€” Centralise the declaration of constants at the beginning
of the ๎‚พle
๎€–๎‚“ ๎‚‘๎‚…๎‚๎‚‰ ๎‚๎‚˜ ๎‚‰๎‚…๎‚—๎‚๎‚‰๎‚– ๎‚˜๎‚“ ๎‚–๎‚‰๎‚…๎‚ˆ๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚˜๎‚“๎‚‹๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚‰๎‚‹๎‚๎‚’๎‚’๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎Šฑ๎‚๎‚‰๎‡€
๎€–๎‚“ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎‚Š๎‚ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚—๎†ฝ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚–๎‚™๎‚๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ๎‡€
RULE
56
RULE โ€” Declare constants in upper case
๎€…๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚•๎‚™๎‚๎‚–๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚๎‚’๎‚‹ ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ #define๎‡€
RULE
57
RULE โ€” Constants that do not require type checking are declared with the
#define preprocessing directive
RULE
58
RULE โ€” Constants requiring explicit type checking must be declared with
the keyword const
RULE
59
RULE โ€” Constant values must be associated with a suf๎‚พx depending on
the type
๎€–๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚‘๎‚๎‚—๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚”๎‚–๎‚‰๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚… ๎‚—๎‚™๎Šฒ๎‚œ ๎‚†๎‚…๎‚—๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚˜๎‚๎‚”๎‚‰๎†ฟ
n
๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚™๎Šฒ๎‚œ U ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚…๎‚๎‚ ๎‚™๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚—๎†พ
n
๎‚˜๎‚“ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰ ๎‚… long ๎‡ฆ๎‚“๎‚– long long ๎‚Š๎‚“๎‚– ๎€…๎ˆ˜๎ˆ˜๎‡ง ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚™๎Šฒ๎‚œ L ๎‡ฆ๎‚“๎‚– LL ๎‚–๎‚‰๎‡‚
๎‚—๎‚”๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚๎‚๎‡ง ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’๎‚—๎‚˜๎‚‰๎‚…๎‚ˆ ๎‚“๎‚Š l ๎‡ฆ๎‚“๎‚– ll ๎‚–๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚๎‚๎‡ง ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚…๎‚’๎‚
๎‚…๎‚‘๎‚†๎‚๎‚‹๎‚™๎‚๎‚˜๎‚ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– 1๎†พ
n
๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚†๎‚ ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜ ๎‚‡๎‚“๎‚’๎‚—๎‚๎‚ˆ๎‚‰๎‚–๎‚‰๎‚ˆ ๎‚…๎‚— double๎†พ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚™๎Šฒ๎‚œ f ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎Šท๎‚“๎‚…๎‚˜
๎‚˜๎‚๎‚”๎‚‰ ๎‡ฆ๎‚“๎‚– d ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚“๎‚™๎‚†๎‚๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚–๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚๎‚๎‡ง๎‡€
Warning
๎€„๎‚ ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜๎†ฝ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚๎‚ˆ๎‚‰๎‚–๎‚‰๎‚ˆ int ๎‚˜๎‚๎‚”๎‚‰ ๎‚…๎‚’๎‚ˆ ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚๎‚ˆ๎‚‰๎‚–๎‚‰๎‚ˆ
double ๎‚˜๎‚๎‚”๎‚‰๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 41
RULE
60
RULE โ€” The size of the type associated with a constant expression must
be suf๎‚พcient to contain it
๎€‹๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰๎‚ˆ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚“๎‚– ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‰๎‚œ๎‚‡๎‚‰๎‚‰๎‚ˆ ๎‚˜๎‚Œ๎‚‰
๎‚˜๎‚๎‚”๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰๎‚‘๎‡€
๎€–๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚…๎‚’๎‚ ๎‚‡๎‚“๎‚’๎‚Š๎‚™๎‚—๎‚๎‚“๎‚’๎†ฝ ๎‚“๎‚‡๎‚˜๎‚…๎‚ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚…๎‚–๎‚‰ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ๎‡€ ๎€•๎‚“๎‚‘๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰๎‚— ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚˜๎‚“๎‚๎‚‰๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚—
๎€—๎€๎€‹๎€š ๎Šฑ๎‚๎‚‰ ๎‚‘๎‚“๎‚ˆ๎‚‰๎‚—๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚›๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚’๎‡€
RECO
61
RECOMMENDATION โ€” Prohibit octal constants
๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚“๎‚‡๎‚˜๎‚…๎‚ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚“๎‚– ๎‚‰๎‚—๎‚‡๎‚…๎‚”๎‚‰ ๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰๎‚—๎‡€
Bad example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚‡๎‚‰๎‚’๎‚˜๎‚–๎‚…๎‚๎‚๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚—๎‡€ ๎€…๎‚‰๎‚–๎‚˜๎‚…๎‚๎‚’
๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰๎‚‰๎‚’ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ๎‡€ ๎€–๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚— ๎‚…๎‚๎‚—๎‚“ ๎‚…๎‚’ ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‡ ๎‚’๎‚…๎‚‘๎‚๎‚’๎‚‹ ๎‚Š๎‚“๎‚–
๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚—๎‡€
#define octal_const 075 /* octal base numerical constant and name in lower case */
const int64_t b = 0l; /* l and not L, and constant naming problem */
uint8_t buffer [0x82]; /* constant not declared and type check needed for this
constant */
int16_t i;
for(i = 0; i < 0x82; i++) { /* hard -coded value */
...
}
printf (" Message \012"); /* octal base escape sequence \012 = \n */
Good example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚–๎‚™๎‚๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‡‚
๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚—๎‡€
const uint32_t INIT_VALUE = 0 x1294U; /* declared constant and with the necessary
type check */
#define BUFFER_SIZE 0 x82U /* declared constant with type check not necessary */
const int64_t B = 0L; /* correction of the suffix and specific naming of the
constant */
uint8_t buffer[BUFFER_SIZE];
uint16_t i;
for(i = 0; i < BUFFER_SIZE; i++) {
...
}
6.3.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ๎‡€๎ˆ— ๎€ƒ ๎‚‡๎‚…๎‚—๎‚˜ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚‘๎‚“๎‚š๎‚‰ ๎‚… ๎‚‡๎‚“๎‚’๎‚—๎‚˜ ๎‚“๎‚– ๎‚š๎‚“๎‚๎‚…๎‚˜๎‚๎‚๎‚‰ ๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚ˆ
๎‚˜๎‚“ ๎‚†๎‚ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡€
42 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ–๎‡€๎ˆ๎‡€ ๎€‘๎‚‡๎‚˜๎‚…๎‚ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ–๎‡€๎ˆ‘๎‡€ ๎€ƒ u ๎‚“๎‚– U ๎‚—๎‚™๎Šฒ๎‚œ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚๎‚ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ
๎‚๎‚’ ๎‚…๎‚’ ๎‚™๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚˜๎‚๎‚”๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ–๎‡€๎ˆ’๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚๎‚“๎‚›๎‚‰๎‚–๎‚‡๎‚…๎‚—๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– l ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚… ๎‚๎‚๎‚˜๎‚‰๎‚–๎‚…๎‚ ๎‚—๎‚™๎Šฒ๎‚œ๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ–๎‡€๎ˆ“๎‡€ ๎€ƒ ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹ ๎‚๎‚๎‚˜๎‚‰๎‚–๎‚…๎‚ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚™๎‚’๎‚๎‚‰๎‚—๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‡‹๎‚— ๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚—
๎‡๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚“ ๎‚… ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‡‚๎‚•๎‚™๎‚…๎‚๎‚๎‚Š๎‚‰๎‚ˆ ๎‚‡๎‚Œ๎‚…๎‚–๎‡๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ‘๎‡€๎ˆ“ ๎€‡๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚™๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚›๎‚–๎‚…๎‚”๎‡‚
๎‚…๎‚–๎‚“๎‚™๎‚’๎‚ˆ๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€†๎‚๎‚–๎‡€ ๎ˆ“๎‡€๎ˆ” ๎€‹๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚“๎‚š๎‚‰๎‚–๎‚๎‚…๎‚”๎‚”๎‚๎‚’๎‚‹ ๎‚š๎‚๎‚—๎‚๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚˜๎‚๎‚”๎‚“๎‡‚
๎‚‹๎‚–๎‚…๎‚”๎‚Œ๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚™๎‚’๎‚…๎‚‘๎‚†๎‚๎‚‹๎‚™๎‚“๎‚™๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ๎ˆ•๎‡‚๎€… ๎€—๎‚—๎‚‰ L๎†ฝ ๎‚’๎‚“๎‚˜ l๎†ฝ ๎‚˜๎‚“ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰ ๎‚… ๎‚๎‚“๎‚’๎‚‹ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€‡๎€…๎€Ž๎ˆ๎ˆ๎‡‚๎€… ๎€…๎‚“๎‚’๎‚—๎‚˜๎‡‚๎‚•๎‚™๎‚…๎‚๎‚๎‚Š๎‚ ๎‚๎‚‘๎‚‘๎‚™๎‚˜๎‚…๎‚†๎‚๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€•๎€–๎€”๎ˆ๎ˆ”๎‡‚๎€… ๎€—๎‚—๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚˜๎‚“ ๎‚‡๎‚“๎‚’๎‚—๎‚˜ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚–๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹ ๎‚๎‚๎‚˜๎‚‰๎‚–๎‚…๎‚๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€‡๎€…๎€Ž๎ˆ๎ˆ—๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰๎‚‹๎‚๎‚’ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎ˆ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚Š๎‚๎‚๎‚’๎‚‹ ๎‚… ๎‚ˆ๎‚‰๎‚‡๎‚๎‚‘๎‚…๎‚ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‡๎€š๎€’๎ˆ“๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‘๎‚“๎‚ˆ๎‚๎‚Š๎‚ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€•๎€–๎€”๎ˆ’๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚…๎‚˜๎‚˜๎‚‰๎‚‘๎‚”๎‚˜ ๎‚˜๎‚“ ๎‚‘๎‚“๎‚ˆ๎‚๎‚Š๎‚ ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹ ๎‚๎‚๎‚˜๎‚‰๎‚–๎‚…๎‚๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‡๎€š๎€’๎ˆ๎ˆ”๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚…๎‚—๎‚˜ ๎‚…๎‚›๎‚…๎‚ ๎‚… ๎‚‡๎‚“๎‚’๎‚—๎‚˜ ๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ๎ˆ‘๎‡‚๎€… ๎€—๎‚—๎‚‰ ๎‚š๎‚๎‚—๎‚™๎‚…๎‚๎‚๎‚ ๎‚ˆ๎‚๎‚—๎‚˜๎‚๎‚’๎‚‡๎‚˜ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ๎ˆ•๎‡‚๎€… ๎€—๎‚—๎‚‰ ๎‚‘๎‚‰๎‚…๎‚’๎‚๎‚’๎‚‹๎‚Š๎‚™๎‚ ๎‚—๎‚๎‚‘๎‚†๎‚“๎‚๎‚๎‚‡ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚˜๎‚“ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜ ๎‚๎‚๎‚˜๎‚‰๎‚–๎‚…๎‚ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚—๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ”๎ˆ“๎ˆ– ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎€Š๎‚…๎‚–๎‚ˆ๎‡‚๎‚‡๎‚“๎‚ˆ๎‚‰๎‚ˆ๎†ฝ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚ ๎‚–๎‚‰๎‚๎‚‰๎‚š๎‚…๎‚’๎‚˜ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚—๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ–๎ˆ๎ˆ“ ๎€‹๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚– ๎‚‡๎‚…๎‚—๎‚˜๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€๎‚“๎‚ˆ๎‚๎‚Š๎‚๎‚๎‚’๎‚‹ ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹ ๎‚๎‚๎‚˜๎‚‰๎‚–๎‚…๎‚๎‚— ๎‡ฌ๎‚—๎‚˜๎‚–๎‚‘๎‚“๎‚ˆ๎‡ญ๎‡€
6.4 Limited use of global variables
๎€™๎‚Œ๎‚‰๎‚’ ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎‚Š๎‚ ๎‚‰๎‚š๎‚‰๎‚–๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‘๎‚“๎‚ˆ๎‚๎Šฑ๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎‡€
๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚–๎‚‘๎‚“๎‚–๎‚‰๎†ฝ ๎‚๎‚Š ๎‚… ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚’๎‚…๎‚‘๎‚‰๎‚ˆ ๎‚…๎‚‡๎‚‡๎‚“๎‚–๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚‡๎‚๎‚‰๎‚…๎‚– ๎‚’๎‚…๎‚‘๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚–๎‚‰๎‚…๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰
๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚๎‚— ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚๎‚‘๎‚‘๎‚‰๎‚ˆ๎‚๎‚…๎‚˜๎‚‰๎‚๎‚ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎‚Š๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ˆ๎‚‰ ๎‚‰๎Šฎ๎‚‰๎‚‡๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’
๎‚“๎‚’ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‡ ๎‚’๎‚…๎‚‘๎‚๎‚’๎‚‹ ๎‚—๎‚‡๎‚Œ๎‚‰๎‚‘๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚๎‚‰๎‚…๎‚– ๎‚…๎‚’๎‚ˆ ๎‚–๎‚‰๎‚‘๎‚…๎‚๎‚’๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚Œ๎‚“๎‚๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚“๎‚– ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚๎‚’๎‚‹ ๎‚˜๎‚‰๎‚…๎‚‘ ๎‡ฆ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚™๎‚”๎‚”๎‚‰๎‚– ๎‚‡๎‚…๎‚—๎‚‰๎†ฝ ๎‚”๎‚–๎‚‰๎Šฑ๎‚œ g_๎†ฝ etc.๎‡ง๎‡€
๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚‡๎‚…๎‚’ ๎‚•๎‚™๎‚๎‚‡๎‚๎‚๎‚ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘๎‚— ๎‚“๎‚Š ๎‚‡๎‚“๎‚’๎‚‡๎‚™๎‚–๎‚–๎‚‰๎‚’๎‚‡๎‚ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚…
๎‚‘๎‚™๎‚๎‚˜๎‚๎‡‚๎‚˜๎‚…๎‚—๎‚๎‚๎‚’๎‚‹ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€ˆ๎‚“๎‚– ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚‘๎‚™๎‚—๎‚˜ ๎‚—๎‚˜๎‚™๎‚ˆ๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚๎‚๎‚˜๎‚
๎‚“๎‚Š ๎‚๎‚๎‚‘๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‡๎‚“๎‚”๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚๎‡€
RULE
62
RULE โ€” Limit global variables to what is strictly necessary
๎€Ž๎‚๎‚‘๎‚๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚‹๎‚๎‚š๎‚‰ ๎‚”๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰ ๎‚˜๎‚“ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚–
๎‚˜๎‚“ ๎‚”๎‚–๎‚“๎‚”๎‚…๎‚‹๎‚…๎‚˜๎‚‰ ๎‚… ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚–๎‚“๎‚™๎‚‹๎‚Œ ๎‚…๎‚’ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
Bad example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚™๎‚—๎‚‰๎‚— ๎‚… ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‡€ ๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚๎‚˜๎‚— ๎‚™๎‚—๎‚‰ ๎‚‡๎‚“๎‚™๎‚๎‚ˆ ๎‚‰๎‚…๎‚—๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚…๎‚š๎‚“๎‚๎‚ˆ๎‚‰๎‚ˆ๎‡€
static uint32_t g_state;
void foo( void ) {
...
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 43
g_state = 1;
}
void bar( void ) {
...
g_state = 2;
}
int main(int argc , char * argv []) {
foo();
bar();
...
}
Good example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚… ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ state ๎‚๎‚— ๎‚”๎‚–๎‚“๎‚”๎‚…๎‡‚
๎‚‹๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚“ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚†๎‚ ๎‚”๎‚…๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚๎‚˜ ๎‚…๎‚— ๎‚… ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎†ฟ
void foo( uint32_t* state) {
...
(* state) = 1;
}
void bar( uint32_t* state) {
...
(* state) = 2;
}
int main(int argc , char * argv []) {
uint32_t state = 0;
foo(& state);
bar(& state);
...
}
6.4.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ๎ˆ‘๎‡‚๎€… ๎€—๎‚—๎‚‰ ๎‚š๎‚๎‚—๎‚™๎‚…๎‚๎‚๎‚ ๎‚ˆ๎‚๎‚—๎‚˜๎‚๎‚’๎‚‡๎‚˜ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€†๎€…๎€Ž๎ˆ’๎ˆ๎‡‚๎€… ๎€†๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚…๎‚”๎‚”๎‚–๎‚“๎‚”๎‚–๎‚๎‚…๎‚˜๎‚‰ ๎‚—๎‚˜๎‚“๎‚–๎‚…๎‚‹๎‚‰ ๎‚ˆ๎‚™๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ๎ˆ˜๎‡‚๎€… ๎€๎‚๎‚’๎‚๎‚‘๎‚๎‚ž๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‡๎‚“๎‚”๎‚‰ ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€†๎‚๎‚–๎‡€ ๎ˆ“๎‡€๎ˆ” ๎€‹๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚“๎‚š๎‚‰๎‚–๎‚๎‚…๎‚”๎‚”๎‚๎‚’๎‚‹ ๎‚š๎‚๎‚—๎‚๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚˜๎‚๎‚”๎‚“๎‡‚
๎‚‹๎‚–๎‚…๎‚”๎‚Œ๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚™๎‚’๎‚…๎‚‘๎‚†๎‚๎‚‹๎‚™๎‚“๎‚™๎‚—๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ—๎‡€๎ˆ˜ ๎€ƒ๎‚’ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎‚†๎‚๎‚“๎‚‡๎‚ ๎‚—๎‚‡๎‚“๎‚”๎‚‰ ๎‚๎‚Š ๎‚๎‚˜๎‚— ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚– ๎‚“๎‚’๎‚๎‚ ๎‚…๎‚”๎‚”๎‚‰๎‚…๎‚–๎‚— ๎‚๎‚’ ๎‚…
๎‚—๎‚๎‚’๎‚‹๎‚๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
6.5 Use of the static keyword
๎€™๎‚Œ๎‚‰๎‚’ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ๎†ฝ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚“๎‚’๎‚๎‚ ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚… ๎‚—๎‚๎‚’๎‚‹๎‚๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ static ๎‚—๎‚˜๎‚“๎‚–๎‚…๎‚‹๎‚‰๎‡‚
๎‚‡๎‚๎‚…๎‚—๎‚— ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚– ๎‚๎‚— ๎‚“๎Šน๎‚‰๎‚’ ๎‚Š๎‚“๎‚–๎‚‹๎‚“๎‚˜๎‚˜๎‚‰๎‚’๎‡€ ๎€…๎‚“๎‚’๎Šท๎‚๎‚‡๎‚˜๎‚— ๎‚‘๎‚…๎‚ ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚…๎‚–๎‚๎‚—๎‚‰ ๎‚…๎‚˜ ๎‚๎‚๎‚’๎‚๎‡‚๎‚˜๎‚๎‚‘๎‚‰๎‡€ ๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š
๎‚˜๎‚Œ๎‚‰ static ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚– ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚–๎‚‰๎‚š๎‚๎‚‰๎‚› ๎‚‘๎‚“๎‚–๎‚‰ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚†๎‚‰๎‚‡๎‚…๎‚™๎‚—๎‚‰ ๎‚๎‚˜ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚…๎‚๎‚๎‚“๎‚› ๎‚˜๎‚“ ๎‚•๎‚™๎‚๎‚‡๎‚๎‚๎‚ ๎‚’๎‚“๎‚˜๎‚๎‚‡๎‚‰
๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‡Œ๎‚”๎‚–๎‚๎‚š๎‚…๎‚˜๎‚‰๎‡š๎‚๎‚“๎‚‡๎‚…๎‚๎‡๎‡€ ๎€–๎‚Œ๎‚‰ static ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ ๎‚˜๎‚‰๎‚๎‚๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‡š๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’
๎‚๎‚— ๎‚๎‚’๎‚ˆ๎‚‰๎‚‰๎‚ˆ ๎‚… ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‡š๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚†๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚˜๎‚— ๎‚š๎‚๎‚—๎‚๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚๎‚๎‚‘๎‚๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚๎‚’ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ
๎‚๎‚˜ ๎‚๎‚— ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ๎‡€
44 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎€–๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚— ๎‚Š๎‚“๎‚– ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ ๎‚˜๎‚“ ๎‚… ๎Šฑ๎‚๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚“๎‚™๎‚˜๎‚—๎‚๎‚ˆ๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎Šฑ๎‚๎‚‰๎‡€ ๎€‰๎‚๎‚“๎‚†๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚“๎‚Š
๎‚˜๎‚Œ๎‚๎‚— ๎‚˜๎‚๎‚”๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚…๎‚— static๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚๎‚๎‚‘๎‚๎‚˜๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‡๎‚“๎‚”๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚“๎‚’๎‚๎‚
๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎Šฑ๎‚๎‚‰๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚๎‚๎‚‘๎‚๎‚˜๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚”๎‚“๎‚—๎‚™๎‚–๎‚‰ ๎‚“๎‚Š ๎‚—๎‚…๎‚๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎‡€
๎€–๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚… ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‡€
RULE
63
RULE โ€” Systematically use the static speci๎‚พer for declarations
๎€–๎‚Œ๎‚‰ static ๎‚—๎‚˜๎‚“๎‚–๎‚…๎‚‹๎‚‰๎‡‚๎‚‡๎‚๎‚…๎‚—๎‚— ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚– ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚…๎‚๎‚ ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—
๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚“๎‚™๎‚˜๎‚—๎‚๎‚ˆ๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚๎‚’ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ๎‡€
6.5.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ๎ˆ”๎‡‚๎€… ๎€†๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰ ๎Šฑ๎‚๎‚‰๎‡‚๎‚—๎‚‡๎‚“๎‚”๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚— ๎‚“๎‚– ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚’๎‚‰๎‚‰๎‚ˆ ๎‚‰๎‚œ๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚ ๎‚๎‚๎‚’๎‚๎‚…๎‚‹๎‚‰ ๎‚…๎‚—
๎‚—๎‚˜๎‚…๎‚˜๎‚๎‚‡๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€๎€•๎€…๎ˆ“๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚š๎‚๎‚“๎‚๎‚…๎‚˜๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚–๎‚…๎‚๎‚’๎‚˜๎‚—๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ—๎‡€๎ˆ– ๎€ˆ๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚‰๎‚œ๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚ ๎‚๎‚๎‚’๎‚๎‚…๎‚‹๎‚‰ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚…๎‚–๎‚‰
๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚“๎‚’๎‚๎‚ ๎‚“๎‚’๎‚‰ ๎‚˜๎‚–๎‚…๎‚’๎‚—๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚™๎‚’๎‚๎‚˜๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ—๎‡€๎ˆ— ๎€–๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚๎‚‡ ๎‚—๎‚˜๎‚“๎‚–๎‚…๎‚‹๎‚‰ ๎‚‡๎‚๎‚…๎‚—๎‚— ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚– ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚…๎‚๎‚ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚—
๎‚…๎‚’๎‚ˆ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚ ๎‚๎‚๎‚’๎‚๎‚…๎‚‹๎‚‰๎‡€
6.6 Use of the volatile keyword
๎€–๎‚Œ๎‚‰ volatile ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚•๎‚™๎‚…๎‚๎‚๎‚Š๎‚ ๎‚‰๎‚๎‚˜๎‚Œ๎‚‰๎‚– ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚… ๎‚Œ๎‚…๎‚–๎‚ˆ๎‚›๎‚…๎‚–๎‚‰
๎‚…๎‚–๎‚‰๎‚… ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚— ๎‚…๎‚’ ๎‚๎‚’๎‚”๎‚™๎‚˜๎‡š๎‚“๎‚™๎‚˜๎‚”๎‚™๎‚˜ ๎‚”๎‚“๎‚–๎‚˜ ๎‚๎‚’ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚๎†ฝ ๎‚“๎‚– ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚–๎‚‰๎‚…๎‚ˆ ๎‚“๎‚– ๎‚›๎‚–๎‚๎‚˜๎‚˜๎‚‰๎‚’ ๎‚˜๎‚“ ๎‚†๎‚ ๎‚…๎‚’ ๎‚…๎‚—๎‚๎‚’๎‡‚
๎‚‡๎‚Œ๎‚–๎‚“๎‚’๎‚“๎‚™๎‚— ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚–๎‚™๎‚”๎‚˜ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€ƒ๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚‰๎‚— ๎‚˜๎‚“ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚๎‚’๎‚ˆ๎‚‰๎‚‰๎‚ˆ ๎‚†๎‚‰ ๎‚”๎‚–๎‚“๎‚˜๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–
๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
RULE
64
RULE โ€” Only variables that can be modi๎‚พed outside the implementation
should be declared volatile
๎€‘๎‚’๎‚๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚๎‚’๎‚”๎‚™๎‚˜๎‡š๎‚“๎‚™๎‚˜๎‚”๎‚™๎‚˜ ๎‚”๎‚“๎‚–๎‚˜๎‚— ๎‚“๎‚– ๎‚…๎‚—๎‚๎‚’๎‚‡๎‚Œ๎‚–๎‚“๎‚’๎‚“๎‚™๎‚— ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚–๎‚™๎‚”๎‚˜ ๎‚Š๎‚™๎‚’๎‚‡๎‡‚
๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚…๎‚— volatile ๎‚˜๎‚“ ๎‚”๎‚–๎‚‰๎‚š๎‚‰๎‚’๎‚˜ ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚– ๎‚–๎‚‰๎‚“๎‚–๎‚‹๎‚…๎‚’๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚’
๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
๎€๎‚“๎‚–๎‚‰๎‚“๎‚š๎‚‰๎‚–๎†ฝ ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎†ฝ ๎‚“๎‚’๎‚๎‚ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚— ๎‚๎‚˜๎‚—๎‚‰๎‚๎‚Š ๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚…๎‚— volatile ๎‚‡๎‚…๎‚’
๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚… volatile ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‡€
RULE
65
RULE โ€” Only volatile-quali๎‚พed pointers can access volatile
variables
6.6.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ๎ˆ–๎‡‚๎€… ๎€„๎‚‰๎‚›๎‚…๎‚–๎‚‰ ๎‚“๎‚Š ๎‚‘๎‚๎‚—๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚ˆ ๎‚š๎‚“๎‚๎‚…๎‚˜๎‚๎‚๎‚‰๎‡‚๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 45
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ‘๎ˆ‘๎‡‚๎€… ๎€—๎‚—๎‚‰ ๎‚š๎‚“๎‚๎‚…๎‚˜๎‚๎‚๎‚‰ ๎‚Š๎‚“๎‚– ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‡๎‚…๎‚’๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚…๎‚‡๎‚Œ๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‡๎€š๎€’๎ˆ’๎ˆ‘๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚… ๎‚š๎‚“๎‚๎‚…๎‚˜๎‚๎‚๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚Œ๎‚–๎‚“๎‚™๎‚‹๎‚Œ ๎‚… ๎‚’๎‚“๎‚’๎‚š๎‚“๎‚๎‚…๎‚˜๎‚๎‚๎‚‰ ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎‡€๎ˆ‘ ๎€–๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚’๎‚“ ๎‚ˆ๎‚‰๎‚…๎‚ˆ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ๎‡€๎ˆ— ๎€ƒ ๎‚‡๎‚…๎‚—๎‚˜ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚‘๎‚“๎‚š๎‚‰ ๎‚…๎‚’๎‚ ๎‚‡๎‚“๎‚’๎‚—๎‚˜ ๎‚“๎‚– ๎‚š๎‚“๎‚๎‚…๎‚˜๎‚๎‚๎‚‰ ๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰
๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚†๎‚ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ–๎ˆ๎ˆ“ ๎€‹๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚– ๎‚‡๎‚…๎‚—๎‚˜๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ”๎ˆ•๎ˆ ๎€†๎‚‰๎‚…๎‚ˆ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€
6.7 Implicit type declaration is prohibited
๎€…๎ˆ˜๎ˆ ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚๎‚’ ๎‚˜๎‚‰๎‚–๎‚‘๎‚— ๎‚“๎‚Š ๎‚“๎‚‘๎‚๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚’ ๎‚‡๎‚‰๎‚–๎‚˜๎‚…๎‚๎‚’ ๎‚‡๎‚๎‚–๎‚‡๎‚™๎‚‘๎‡‚
๎‚—๎‚˜๎‚…๎‚’๎‚‡๎‚‰๎‚—๎†ฝ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚…
typedef๎‡€
Information
๎€‹๎‚’ ๎‚”๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚‡๎‚‰๎†ฝ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‚๎‚—๎‚—๎‚™๎‚‰ ๎‚… ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹ ๎‡ฆ-Wimplicit-int๎‡ง ๎‚†๎‚™๎‚˜ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚…๎‚—๎‚—๎‚™๎‚‘๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜
๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚— int๎‡€
RULE
66
RULE โ€” No type omission is accepted when declaring a variable
๎€ƒ๎‚๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚†๎‚‰๎‚‰๎‚’ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚™๎‚—๎‚‰๎‡€
๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚–๎‚‘๎‚“๎‚–๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ K&R
๎ˆ—
๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚๎‚’๎‚˜๎‚…๎‚œ๎†ฝ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฟ
int foo(a,p)
int a;
char *p;
{ ...
}
๎‚๎‚— ๎‚…๎‚๎‚—๎‚“ ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ๎‡€ ๎€ˆ๎‚๎‚–๎‚—๎‚˜๎‚๎‚๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚“๎‚†๎‚—๎‚“๎‚๎‚‰๎‚˜๎‚‰ ๎‚…๎‚’๎‚ˆ๎†ฝ ๎‚—๎‚‰๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚๎†ฝ ๎‚๎‚˜ ๎‚–๎‚‰๎‚ˆ๎‚™๎‚‡๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚…๎‚ˆ๎‚…๎‚†๎‚๎‚๎‡‚
๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰๎†ฝ ๎‚”๎‚“๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚๎‚๎‚๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚— ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚…๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚๎‚‰๎‚š๎‚‰๎‚๎‡€
Bad example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‡ฆ๎€…๎ˆ˜๎ˆ๎‡ง ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚— ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚˜๎‚๎‚”๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
...
const ACONST = 42; /* prohibited : type of constant not explicitly defined (
implicite int) */
unsigned e; /* prohibited : type of e not explicitly defined ( implicit unsigned int )
*/
signed f; /* prohibited : type of constant not explicitly defined
(implicit signed int) */
...
int foo(char a, const b) /* prohibited : type of b not explicitly defined ( implicit
int) */
{
...
}
๎ˆ—๎‡€ ๎€๎‚‰๎‚–๎‚’๎‚๎‚‹๎‚Œ๎‚…๎‚’ ๎Šจ ๎€”๎‚๎‚˜๎‚‡๎‚Œ๎‚๎‚‰๎‡‹๎‚— ๎€… ๎‚—๎‚๎‚’๎‚˜๎‚…๎‚œ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎€ƒ๎€๎€•๎€‹ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‚—
46 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
bar( char c, const int d) /* prohibited : type of return of the function not
explicitly defined ( implicit int) */
{
..
}
Good example
๎€ƒ๎‚๎‚ ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚› ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‡€
...
const int ACONST = 42;
unsigned int e;
signed int f;
...
int foo( unsigned char a, const int b)
{
...
}
int bar( unsigned char c, const int d)
{
...
}
6.7.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€†๎€…๎€Ž๎ˆ’๎ˆ๎‡‚๎€… ๎€†๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚– ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰๎‚‘๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ—๎‡€๎ˆ ๎€–๎‚๎‚”๎‚‰๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ๎‡€
6.8 Compound literals
Compound literals ๎‚›๎‚‰๎‚–๎‚‰ ๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚†๎‚ ๎€…๎ˆ˜๎ˆ˜ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚๎‚๎‚“๎‚› ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚–๎‚‰๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚™๎‚’๎‚’๎‚…๎‚‘๎‚‰๎‚ˆ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚— ๎‚Š๎‚–๎‚“๎‚‘ ๎‚… ๎‚๎‚๎‚—๎‚˜
๎‚“๎‚Š ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚—๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚“๎Šน๎‚‰๎‚’ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚– ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚†๎‚‰๎‚‰๎‚’
๎‚”๎‚…๎‚—๎‚—๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚—๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚๎‚๎‚Š๎‚‰๎‚˜๎‚๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚… compound literal ๎‚๎‚— ๎‚‰๎‚๎‚˜๎‚Œ๎‚‰๎‚– ๎‚—๎‚˜๎‚…๎‚˜๎‚๎‚‡ ๎‚“๎‚– ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡
๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚’ ๎‚›๎‚Œ๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚๎‚˜ ๎‚๎‚— ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎Šฑ๎‚๎‚‰ ๎‚“๎‚– ๎‚†๎‚๎‚“๎‚‡๎‚ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚‰๎‚š๎‚‰๎‚๎‡€
๎€ƒ๎‚˜๎‚˜๎‚‰๎‚‘๎‚”๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚“๎‚™๎‚˜๎‚—๎‚๎‚ˆ๎‚‰ ๎‚“๎‚Š ๎‚๎‚˜๎‚— ๎‚—๎‚‡๎‚“๎‚”๎‚‰ ๎‚›๎‚๎‚๎‚ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚๎‚’ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€
๎€‹๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚˜๎‚“ ๎‚Š๎‚™๎‚๎‚๎‚ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‡๎‚“๎‚”๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚๎‚— ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
RECO
67
RECOMMENDATION โ€” Limit the use of compound literals
๎€†๎‚™๎‚‰ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚๎‚—๎‚ ๎‚“๎‚Š ๎‚‘๎‚๎‚—๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚๎‚’๎‚‹ compound literals๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚™๎‚—๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚๎‚๎‚‘๎‚๎‚˜๎‚‰๎‚ˆ๎†ฝ ๎‚ˆ๎‚“๎‚‡๎‚™๎‡‚
๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚…๎‚ ๎‚…๎‚˜๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚”๎‚…๎‚๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚—๎‚‡๎‚“๎‚”๎‚‰๎‡€
Bad example
#include <stdio .h>
#include <stdint .h>
#define MAX 10
struct point {
uint8_t x,y;
};
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 47
int main( void )
{
uint8_t i;
struct point *tab[MAX];
for (i = 0; i < MAX; i++){
tab[i] = &( struct point){i ,2*i};
}
for (i = 0; i < MAX; i++){
printf ("%d\n", tab[i]->x); /* undefined behaviour because the compound literal
defined in the previous loop does not exist anymore */
}
...
}
Good example
#include <stdio .h>
#include <stdint .h>
struct point {
uint8_t x,y;
};
#define MAX 10
int main( void )
{
uint8_t i;
struct point tab[MAX];
for (i = 0; i < MAX; i++){
tab[i] = ( struct point){i, 2*i};
}
for (i = 0; i < MAX; i++){
printf ("%d\n", tab[i].x);
}
...
}
6.8.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ‘๎ˆ๎‡‚๎€… ๎€—๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚“๎‚–๎‚…๎‚‹๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚‘๎‚”๎‚“๎‚™๎‚’๎‚ˆ ๎‚๎‚๎‚˜๎‚‰๎‚–๎‚…๎‚๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€†๎€…๎€Ž๎ˆ’๎ˆ๎‡‚๎€… ๎€†๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚…๎‚”๎‚”๎‚–๎‚“๎‚”๎‚–๎‚๎‚…๎‚˜๎‚‰ ๎‚—๎‚˜๎‚“๎‚–๎‚…๎‚‹๎‚‰ ๎‚ˆ๎‚™๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€‡๎‚—๎‚‡๎‚…๎‚”๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚…๎‚’ ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‡ฌ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚‡๎‚…๎‚”๎‚‰๎‡ญ๎‡€
6.9 Enumerations
๎€–๎‚Œ๎‚‰ ๎‚’๎‚“๎‚’๎‡‚๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚๎‚’ ๎‚…๎‚’ ๎‚‰๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎ˆ ๎‚Œ๎‚๎‚‹๎‚Œ๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚š๎‚๎‚“๎‚™๎‚—
๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‡€ ๎€‹๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚–๎‚—๎‚˜ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚๎‚˜ ๎‚๎‚— ๎ˆ๎‡€ ๎€‹๎‚Š ๎‚…๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚–๎‚‰
๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎†ฝ ๎‚’๎‚“ ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘ ๎‚…๎‚–๎‚๎‚—๎‚‰๎‚—๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚‡๎‚‰๎‚–๎‚˜๎‚…๎‚๎‚’ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎†ฝ ๎‚…๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚๎‚— ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰๎‡€
๎€‹๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚†๎‚‰๎‚˜๎‚˜๎‚‰๎‚– ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚‘๎‚๎‚œ๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚—๎‡€ ๎€‹๎‚Š ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚“๎‚Š
๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚‰๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚๎‚‰๎‚…๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€ ๎€‹๎‚Š ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰
๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚…๎‚๎‚ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚˜๎‚“ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚’๎‚“๎‚’๎‚‰ ๎‚“๎‚Š
๎‚˜๎‚Œ๎‚‰ ๎‚‹๎‚๎‚š๎‚‰๎‚’ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚–๎‚‰๎‚”๎‚‰๎‚…๎‚˜๎‚‰๎‚ˆ๎‡€
48 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
RULE
68
RULE โ€” Do not mix explicit and implicit constants in an enumeration
๎€‡๎‚๎‚˜๎‚Œ๎‚‰๎‚– ๎‚…๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚๎‚’ ๎‚…๎‚’ ๎‚‰๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚—๎‚๎‚’๎‚‹๎‚๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰๎†ฝ
๎‚“๎‚– ๎‚’๎‚“๎‚’๎‚‰ ๎‚…๎‚˜ ๎‚…๎‚๎‚๎‡€
๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚…๎‚’ ๎‚‰๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚–๎‚‰ ๎‚…๎‚๎‚—๎‚“ ๎‚—๎‚™๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚™๎‚๎‚‰๎‚— ๎‚“๎‚Š ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ•๎‡€๎ˆ’๎†ฝ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š
๎‚™๎‚”๎‚”๎‚‰๎‚– ๎‚‡๎‚…๎‚—๎‚‰ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚—๎‡€
๎€ƒ ๎‚™๎‚—๎‚‰ ๎‚—๎‚“๎‚‘๎‚‰๎‚˜๎‚๎‚‘๎‚‰๎‚— ๎‚“๎‚†๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ ๎‚…๎‚–๎‚“๎‚™๎‚’๎‚ˆ ๎‚‰๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚…๎‚’๎‚“๎‚’๎‚๎‚‘๎‚“๎‚™๎‚— ๎‚‰๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—
๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚—๎‡€ ๎€ˆ๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฟ
enum {
ZERO ,
ONE
};
๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’๎‚—๎‚˜๎‚‰๎‚…๎‚ˆ ๎‚“๎‚Š๎†ฟ
const int ZERO =0;
const int ONE =1;
๎€‡๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚๎‚— ๎‚”๎‚™๎‚–๎‚”๎‚“๎‚—๎‚‰๎†พ ๎‚๎‚˜ ๎‚๎‚— ๎‚… ๎‚‘๎‚๎‚—๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‡๎‚…๎‚’ ๎‚‘๎‚…๎‚๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚Œ๎‚…๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“
๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‡€
RULE
69
RULE โ€” Do not use anonymous enumerations
Bad example
enum une_enum {
enum1 =1,
enum2 ,
enum3 ,
enum4 =3 /* enum4 et enum3 ont la mรชme valeur */
};
Good example
๎€ƒ๎‚๎‚ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚… ๎‚™๎‚’๎‚๎‚•๎‚™๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚–๎‚‰ ๎‚๎‚’ ๎‚™๎‚”๎‚”๎‚‰๎‚– ๎‚‡๎‚…๎‚—๎‚‰๎‡€
enum une_enum {
ENUM1 =0,
ENUM2 =1,
ENUM3 =2,
ENUM4 =3
};
6.9.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ—๎‡€๎ˆ๎ˆ‘ ๎€™๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚…๎‚’ ๎‚‰๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚๎‚๎‚—๎‚˜๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚๎‡‚๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚‰๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’
๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚™๎‚’๎‚๎‚•๎‚™๎‚‰๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‹๎€๎€–๎ˆ๎ˆ˜๎‡‚๎€… ๎€‡๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚‰๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚‘๎‚…๎‚”๎‚— ๎‚˜๎‚“ ๎‚™๎‚’๎‚๎‚•๎‚™๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚—๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 49
6.10 Initialising variables before use
๎€‹๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚— ๎‚… ๎‚–๎‚๎‚—๎‚ ๎‚“๎‚Š ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚๎‚˜ ๎‚Œ๎‚…๎‚— ๎‚’๎‚“๎‚˜
๎‚†๎‚‰๎‚‰๎‚’ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰๎‚ˆ๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ๎‡€
Information
๎€‰๎‚๎‚“๎‚†๎‚…๎‚ ๎‚…๎‚’๎‚ˆ ๎‚—๎‚˜๎‚…๎‚˜๎‚๎‚‡ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰๎‚ˆ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ๎†ฝ ๎‚†๎‚™๎‚˜
๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‡€ ๎€†๎‚™๎‚‰ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚๎‚…๎‚‡๎‚ ๎‚“๎‚Š ๎‚๎‚’๎‚“๎‚›๎‚๎‚‰๎‚ˆ๎‚‹๎‚‰
๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚—๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰ ๎‚…๎‚๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎‡€
๎€‘๎‚’๎‚‰ ๎‚‰๎‚…๎‚—๎‚ ๎‚›๎‚…๎‚ ๎‚˜๎‚“ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚๎‚— ๎‚๎‚— ๎‚˜๎‚“ ๎‚ˆ๎‚“ ๎‚๎‚˜ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚๎‚’๎‚‹ ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚๎‚Š ๎‚๎‚˜ ๎‚๎‚— ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ
๎‚…๎‚๎‚“๎‚’๎‚‰๎†ฝ ๎‚“๎‚– ๎‚๎‚‘๎‚‘๎‚‰๎‚ˆ๎‚๎‚…๎‚˜๎‚‰๎‚๎‚ ๎‚…๎Šน๎‚‰๎‚– ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚“๎‚– ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
RECO
70
RECOMMENDATION โ€” Variables should be initialised at or immediately
after declaration
๎€ƒ๎‚๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰๎‚ˆ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ๎†ฝ ๎‚“๎‚– ๎‚๎‚‘๎‚‘๎‚‰๎‚ˆ๎‚๎‡‚
๎‚…๎‚˜๎‚‰๎‚๎‚ ๎‚…๎Šน๎‚‰๎‚–๎‚›๎‚…๎‚–๎‚ˆ๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
Information
๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚‡๎‚…๎‚’ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜ ๎‚‡๎‚‰๎‚–๎‚˜๎‚…๎‚๎‚’ ๎‚‘๎‚๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€ ๎„Œ๎„ˆ๎„ˆ ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚”๎‚–๎‚“๎‚š๎‚๎‚ˆ๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰
-Wuninitialized ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€•๎‚™๎‚†๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ”๎‡€๎ˆ’๎‡€๎ˆ’ ๎‚‹๎‚๎‚š๎‚‰๎‚— ๎‚‘๎‚“๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚˜๎‚…๎‚๎‚๎‚— ๎‚…๎‚’๎‚ˆ ๎‚…๎‚๎‚—๎‚“ ๎‚Œ๎‚๎‚‹๎‚Œ๎‚๎‚๎‚‹๎‚Œ๎‚˜๎‚— ๎‚˜๎‚Œ๎‚‰
๎‚๎‚๎‚‘๎‚๎‚˜๎‚— ๎‚“๎‚Š ๎‚—๎‚™๎‚‡๎‚Œ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€ ๎€‹๎‚’ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹ ๎‚–๎‚…๎‚๎‚—๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚๎‚— ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚—
๎‚’๎‚“๎‚˜ ๎‚—๎‚™๎Šฒ๎‚‡๎‚๎‚‰๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚‹๎‚™๎‚…๎‚–๎‚…๎‚’๎‚˜๎‚‰๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚”๎‚–๎‚“๎‚”๎‚‰๎‚–๎‚๎‚ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚ˆ๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚ˆ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚…๎‚–๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
/* declarations in the body of a function */
uint32_t a;
uint32_t b;
uint32_t c;
a = b + c; /* variables are used but without initialisation */
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚†๎‚‰๎‚๎‚’๎‚‹ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‡ฆ๎‚…๎‚—
๎‚—๎‚“๎‚“๎‚’ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚Œ๎‚‰๎‚–๎‚‰๎‡ง๎‡€
/* declarations in the body of the function */
uint32_t a = 0;
uint32_t b = 0;
uint32_t c = 0;
a = b + c;
6.10.1 References
50 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ˜๎‡€๎ˆ๎†ฟ ๎€–๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚—๎‚˜๎‚“๎‚–๎‚…๎‚‹๎‚‰ ๎‚ˆ๎‚™๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚…๎‚ˆ
๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚๎‚˜ ๎‚Œ๎‚…๎‚— ๎‚†๎‚‰๎‚‰๎‚’ ๎‚—๎‚‰๎‚˜๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‡๎‚œ๎‚”๎ˆ’๎ˆ’๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚…๎‚ˆ ๎‚™๎‚’๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚ˆ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ“๎ˆ”๎ˆ– ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚™๎‚’๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ–๎ˆ”๎ˆ— ๎€”๎‚‰๎‚๎‚๎‚…๎‚’๎‚‡๎‚‰ ๎‚“๎‚’ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ๎†ฝ ๎‚™๎‚’๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ๎†ฝ ๎‚“๎‚– ๎€‹๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‡‚๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚–๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ˜๎ˆ๎ˆ— ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚™๎‚’๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚ˆ ๎€”๎‚‰๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€”๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚๎‚’๎‚‹ ๎‚™๎‚’๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚ˆ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‡ฌ๎‚™๎‚’๎‚๎‚’๎‚๎‚˜๎‚–๎‚‰๎‚Š๎‡ญ๎‡€
6.11 Initialisation of structured variables
๎€–๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚“๎Šฎ๎‚‰๎‚–๎‚— ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚๎‚๎‚˜๎‚๎‚‰๎‚— ๎‚Š๎‚“๎‚– ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚๎‚’๎‚‹ ๎‚…๎‚–๎‚–๎‚…๎‚๎‚—๎†ฝ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ
๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎‡€ ๎€ƒ๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚…๎‚–๎‚‰ ๎‚‘๎‚…๎‚’๎‚ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚๎‚๎‚˜๎‚๎‚‰๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚’๎‚Š๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚…๎‚’ ๎‚…๎‚๎‚—๎‚“ ๎‚†๎‚‰ ๎‚‘๎‚๎‚—๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡‚
๎‚”๎‚–๎‚‰๎‚˜๎‚‰๎‚ˆ๎‡€
RULE
71
RULE โ€” Use only one initialisation syntax for structured variables
๎€ˆ๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎†ฝ ๎‚“๎‚’๎‚๎‚ ๎‚“๎‚’๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚๎‚’๎‚˜๎‚…๎‚œ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰
๎‚‡๎‚Œ๎‚“๎‚—๎‚‰๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
Bad example
int tab[10] = { 0, [4] = 3, 5, 6, [1] = 1, 2 };
struct type_t o = { .a = 10, 0, "bob" };
Good example
int tab[10] = { 0, 1, 2, 3, 5, 6, 0, 0, 0, 0 };
struct type_t o = { 10, 0, "bob" };
struct type_t p = { .a = 10, .b = 0, .c ="bob" };
๎€ƒ๎‚’ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚“๎Šน๎‚‰๎‚’ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚‡๎‚‡๎‚‰๎‚”๎‚˜๎‚‰๎‚ˆ ๎‚๎‚—๎†ฟ
int tab[N] = {0};
une_structure st = {0};
๎€–๎‚Œ๎‚๎‚— ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ all ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎‡š๎Šฑ๎‚‰๎‚๎‚ˆ๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚…๎‚–๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚ž๎‚‰๎‚–๎‚“๎‡€
Warning
๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚‘๎‚…๎‚’๎‚˜๎‚๎‚‡๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚’๎‚“๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚‘๎‚๎‚—๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚“๎‚“๎‚ˆ๎†ฟ
int tab[N] = {1}; /* does not mean that all the elements are 1, but that all are at
zero and only the first element is 1 */
๎€–๎‚Œ๎‚๎‚— ๎‚๎‚— ๎‚†๎‚‰๎‚‡๎‚…๎‚™๎‚—๎‚‰๎†ฝ ๎‚๎‚’ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚๎‚’๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚˜๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‡ฆi.e. ๎‚๎‚Š
๎‚’๎‚“๎‚˜ ๎‚…๎‚๎‚ ๎Šฑ๎‚‰๎‚๎‚ˆ๎‚—๎‡š๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚…๎‚–๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰๎‚ˆ๎‡ง๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚’๎‚๎‚๎‚—๎‚˜๎‚‰๎‚ˆ ๎Šฑ๎‚‰๎‚๎‚ˆ๎‚—๎‡š๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚…๎‚–๎‚‰
๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎ˆ ๎‚†๎‚ ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜๎‡€ ๎€’๎‚๎‚‰๎‚…๎‚—๎‚‰ ๎‚’๎‚“๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚๎‚— ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚‰๎‚œ๎‚˜๎‚‰๎‚’๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰
๎‚”๎‚…๎‚ˆ๎‚ˆ๎‚๎‚’๎‚‹ ๎‚—๎‚”๎‚…๎‚‡๎‚‰๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 51
๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎€…๎ˆ˜๎ˆ˜ ๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚๎‚’๎‚‹ ๎‡ฆ๎‚…๎‡ง ๎‚‹๎‚๎‚š๎‚‰๎‚’ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡ฆ๎‚—๎‡ง ๎‚“๎‚Š ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ
๎‚…๎‚ˆ๎‚ˆ๎‚— ๎‚๎‚‰๎‚˜ ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚’๎‚Š๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚– ๎‚‰๎‚š๎‚‰๎‚’ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰
๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚”๎‚“๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚๎‚๎‚ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚—๎‡€
RULE
72
RULE โ€” Structured variables must not be initialised without specifying the
initialisation value and each ๎‚พeld/element of the structured variable must
be initialised
๎€๎‚“๎‚’๎‡‚๎‚—๎‚‡๎‚…๎‚๎‚…๎‚– ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰๎‚ˆ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚๎†ฟ ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰๎‚ˆ
๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚‡๎‚๎‚‰๎‚…๎‚– ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚—๎‚™๎‚”๎‚‰๎‚–๎Šท๎‚™๎‚“๎‚™๎‚— ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚—๎‡€ ๎€ƒ๎‚๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚˜๎‚๎‚š๎‚‰๎‚๎‚๎†ฝ
๎‚˜๎‚Œ๎‚‰ {0} ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚– ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€ˆ๎‚๎‚’๎‚…๎‚๎‚๎‚๎†ฝ ๎‚…๎‚–๎‚–๎‚…๎‚๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚—๎‚๎‚ž๎‚‰
๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚—๎‚‰๎‚˜ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰๎‚‘๎‡€
Bad example
๎€–๎‚Œ๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚‰ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฟ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚’๎‡‚๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚๎‚’๎‚๎‡‚
๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚—๎‚™๎‚”๎‚‰๎‚–๎Šท๎‚™๎‚“๎‚™๎‚— ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’
๎‚š๎‚…๎‚๎‚™๎‚‰๎‚—๎‡€
int32_t y[5] = {1, 2, 3}; /* the initialisation is misleading here - in reality ,
the last two elements are initialised at zero */
int32_t z[2] = {1, 2, 3}; /* as above - in reality the value 3 is ignored */
int16_t vv [5] = { [0] = -2, [1] = -9, [3] = -8, [2] = 18 }; /* source of error , the
indexes 2 and 3 are not in increasing order and 4 is forgotten */
struct person {
unsigned char name [20];
uint16_t roll;
float marks;
int grades [10];
};
struct person p1 = {" ", 0}; /* obscure */
struct person p2 = {"toto" ,67 ,78.3,{0},12}; /* everything is correctly initialised ,
including
all the elements of the array grades which are set to 0, but 12 is ignored */
Good example
๎€‹๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚› ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚‰๎‚’๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚—๎‚— ๎‚…๎‚๎‚ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‡‚
๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚—๎‚™๎‚”๎‚‰๎‚–๎Šท๎‚™๎‚“๎‚™๎‚— ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚—๎‡€
int32_t y[5] = { 1, 2, 3, 4, 5 }; /* full initialisation */
int32_t z[2] = { 1, 2 }; /* no superfluous elements */
int32_t w[3] = { 0 }; /* accepted notation to initialise all elements with the
value 0 */
int16_t vv [5] = { [0] = -2, [1] = -9, [2] = 18, [3] = -8, [4] = 33 }; /* ok */
struct person {
unsigned char name [20];
uint16_t roll;
float marks;
int grades [10];
};
struct person p1 = { .name = "titi ", .roll = 12, . marks = 10.0f, . note = {0}};
/* all elements are explicitly initialised */
52 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
struct person p2 = { .name="toto", . roll =67, . marks =78.3 , .grades ={0}}; /* another
example of recognised initialisation , this time without superfluous elements
*/
6.11.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ˜๎‡€๎ˆ‘ ๎€–๎‚Œ๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚– ๎‚Š๎‚“๎‚– ๎‚…๎‚’ ๎‚…๎‚‹๎‚‹๎‚–๎‚‰๎‚‹๎‚…๎‚˜๎‚‰ ๎‚“๎‚– ๎‚™๎‚’๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚‰๎‚’๎‚‡๎‚๎‚“๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚†๎‚–๎‚…๎‚‡๎‚‰๎‚—๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ˜๎‡€๎ˆ’ ๎€ƒ๎‚–๎‚–๎‚…๎‚๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚…๎‚๎‚๎‚ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ˜๎‡€๎ˆ“ ๎€ƒ๎‚’ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚ˆ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚“๎‚’๎‚‡๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ˜๎‡€๎ˆ” ๎€™๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚—๎‚๎‚‹๎‚’๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š
๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€ƒ๎€”๎€”๎ˆ๎ˆ‘๎‡‚๎€… ๎€‡๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚Š๎‚ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚†๎‚“๎‚™๎‚’๎‚ˆ๎‚—๎†ฝ ๎‚‰๎‚š๎‚‰๎‚’ ๎‚๎‚Š ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‡‚
๎‚๎‚ž๎‚‰๎‚–๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ•๎ˆ” ๎€‹๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚“๎‚– ๎‚๎‚’๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚˜๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
6.12 Mandatory use of declarations
๎€™๎‚Œ๎‚‰๎‚’ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚†๎‚™๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚…๎Šน๎‚‰๎‚–๎‚›๎‚…๎‚–๎‚ˆ๎‚—๎†ฝ ๎‚๎‚˜ ๎‚‘๎‚…๎‚ ๎‚‘๎‚‰๎‚…๎‚’ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚‘๎‚…๎‚ˆ๎‚‰
๎‚… ๎‚‘๎‚๎‚—๎‚˜๎‚…๎‚๎‚‰ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚›๎‚–๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚“๎‚’๎‚‰ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚›๎‚…๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’๎‚—๎‚˜๎‚‰๎‚…๎‚ˆ ๎‚“๎‚Š ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚“๎‚– ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚˜๎‚— ๎‚™๎‚—๎‚‰
๎‚›๎‚…๎‚— ๎‚–๎‚‰๎‚‘๎‚“๎‚š๎‚‰๎‚ˆ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‡€
Information
๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— -Wunused-variable ๎‚…๎‚’๎‚ˆ
-Wunused-parameter ๎‚‘๎‚…๎‚๎‚‰ ๎‚๎‚˜ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚Œ๎‚๎‚— ๎‚๎‚๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚”๎‚…๎‚˜๎‚˜๎‚‰๎‚–๎‚’๎‚—๎‡€
RECO
73
RECOMMENDATION โ€” Every declaration must be used
๎€ƒ๎‚๎‚ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎†ฝ ๎‚›๎‚Œ๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚…๎‚–๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎†ฝ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚๎‚…๎‚†๎‚‰๎‚๎‚—๎†ฝ
๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚– ๎‚…๎‚’๎‚๎‚˜๎‚Œ๎‚๎‚’๎‚‹๎‡€
Warning
๎€™๎‚Œ๎‚‰๎‚’ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚๎‚’๎‚‹ ๎‚… ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎†ฝ ๎‚’๎‚“๎‚˜ ๎‚…๎‚๎‚ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚๎‚๎‚ ๎‚™๎‚—๎‚‰๎‚ˆ ๎†ฟ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—
๎‚…๎‚’๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚‰๎‚œ๎‚”๎‚“๎‚–๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚‘๎‚…๎‚ ๎‚“๎‚†๎‚š๎‚๎‚“๎‚™๎‚—๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚๎‚˜๎‚—๎‚‰๎‚๎‚Š๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚†๎‚™๎‚˜ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚๎‚‰๎‚˜๎‚‰๎‚ˆ๎‡€
uint32_t init_list(list_t** pp_list ) {
list_t * p_list = NULL ;
list_element_t * p_element = NULL;
uint32_t ui32_list_len = 0;
if (NULL == pp_list) {
return 0;
}
(* pp_list) = (list_t *)malloc( sizeof (list_t));
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 53
if (NULL == (* pp_list)) {
return 0;
}
(* pp_list)->p_head = NULL;
(* pp_list)->p_tail = NULL;
return 1;
}
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚…๎‚๎‚ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
uint32_t init_list(list_t** pp_list ) {
if (NULL == pp_list) {
return 0;
}
(* pp_list) = (list_t *)malloc( sizeof (list_t));
if (NULL == (* pp_list)) {
return 0;
}
(* pp_list)->p_head = NULL;
(* pp_list)->p_tail = NULL;
return 1;
}
6.12.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎‡€๎ˆ‘ ๎€–๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚’๎‚“ ๎‚ˆ๎‚‰๎‚…๎‚ˆ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎‡€๎ˆ’ ๎€ƒ ๎‚”๎‚–๎‚“๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚™๎‚’๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚๎‚”๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎‡€๎ˆ“ ๎€ƒ ๎‚”๎‚–๎‚“๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚™๎‚’๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚…๎‚‹ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎‡€๎ˆ” ๎€ƒ ๎‚”๎‚–๎‚“๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚™๎‚’๎‚™๎‚—๎‚‰๎‚ˆ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎‡€๎ˆ• ๎€ƒ ๎‚”๎‚–๎‚“๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚™๎‚’๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚…๎‚†๎‚‰๎‚ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎‡€๎ˆ– ๎€–๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚’๎‚“ ๎‚™๎‚’๎‚™๎‚—๎‚‰๎‚ˆ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚๎‚’ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€๎€•๎€…๎ˆ๎ˆ–๎‡‚๎€… ๎€†๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚–๎‚‰๎‚‘๎‚“๎‚š๎‚‰ ๎‚ˆ๎‚‰๎‚…๎‚ˆ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€๎€•๎€…๎ˆ๎ˆ’๎‡‚๎€… ๎€†๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚–๎‚‰๎‚‘๎‚“๎‚š๎‚‰ ๎‚™๎‚’๎‚™๎‚—๎‚‰๎‚ˆ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€๎€•๎€…๎ˆ๎ˆ‘๎‡‚๎€… ๎€†๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚–๎‚‰๎‚‘๎‚“๎‚š๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚Œ๎‚…๎‚— ๎‚’๎‚“ ๎‚‰๎Šฎ๎‚‰๎‚‡๎‚˜ ๎‚“๎‚– ๎‚๎‚— ๎‚’๎‚‰๎‚š๎‚‰๎‚– ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚‰๎‚ˆ๎‡€
6.13 Naming of variables for sensitive data
๎€‹๎‚˜ ๎‚๎‚— ๎‚๎‚‘๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚š๎‚‰ ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰ ๎‚—๎‚‰๎‚”๎‚…๎‚–๎‚…๎‚˜๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚—๎‚˜๎‚“๎‚–๎‚‰ ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚’๎‚“๎‚’๎‡‚๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚…๎‡€ ๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰
๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚›๎‚‰๎‚๎‚๎‡‚๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚’๎‚…๎‚‘๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚–๎‚๎‚—๎‚๎‚— ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚˜๎‚“ ๎‚—๎‚™๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚š๎‚‰๎‚๎‚
๎‚—๎‚˜๎‚“๎‚–๎‚‰ ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚’๎‚“๎‚’๎‡‚๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚…๎‡€
RULE
74
RULE โ€” Use separate variables for sensitive data and non-sensitive data
54 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎€•๎‚‰๎‚”๎‚…๎‚–๎‚…๎‚˜๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚…๎‚๎‚—๎‚“ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚™๎‚’๎‚‰๎‚’๎‚‡๎‚–๎‚๎‚”๎‚˜๎‚‰๎‚ˆ ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚…๎‚’๎‚ˆ ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚”๎‚–๎‚“๎‚˜๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ
๎‚๎‚’ ๎‚‡๎‚“๎‚’๎Šฑ๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚๎‚๎‚˜๎‚ ๎‚…๎‚’๎‚ˆ๎‡š๎‚“๎‚– ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚–๎‚๎‚˜๎‚๎‡€
RULE
75
RULE โ€” Use different variables for sensitive data that are protected in
con๎‚พdentiality and/or integrity than the ones used for unprotected sensitive
data
๎€–๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚–๎‚™๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚”๎‚–๎‚๎‚’๎‚‡๎‚๎‚”๎‚๎‚‰ ๎‚“๎‚Š ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚๎‚’๎‚‹ ๎‚’๎‚“๎‚’๎‡‚๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰๎†ฝ ๎‚‰๎‚’๎‚‡๎‚–๎‚๎‚”๎‚˜๎‚‰๎‚ˆ
๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚™๎‚’๎‚‰๎‚’๎‚‡๎‚–๎‚๎‚”๎‚˜๎‚‰๎‚ˆ ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‡€
๎€‹๎‚˜ ๎‚‹๎‚“๎‚‰๎‚— ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚—๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚Œ๎‚…๎‚–๎‚ˆ๎‡‚๎‚‡๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎‚…๎‚’๎‚ ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚…๎‚’๎‚ ๎‚๎‚๎‚’๎‚ˆ ๎‡ฆ๎‚”๎‚…๎‚—๎‚—๎‚›๎‚“๎‚–๎‚ˆ๎†ฝ ๎‚๎‚“๎‚‹๎‚๎‚’๎†ฝ
๎‚‰๎‚’๎‚‡๎‚–๎‚๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚‰๎‚๎†ฝ etc.๎‡ง ๎‚๎‚— ๎‚Š๎‚“๎‚–๎‚†๎‚๎‚ˆ๎‚ˆ๎‚‰๎‚’๎‡€
RULE
76
RULE โ€” Never hard-code sensitive data
Bad example
๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚†๎‚‰๎‚๎‚“๎‚› ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚… ๎‚’๎‚…๎‚‘๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‡€
#define KEY_SIZE 32U
#define BUFFER_SIZE 512U
size_t key_len = 0;
size_t clear_data1_len = 0;
size_t encrypted_data2_len = 0;
uint8_t key[KEY_SIZE];
uint8_t data1 [ BUFFER_SIZE ];
uint8_t data2 [ BUFFER_SIZE ];
uint32_t error_code = 0;
error_code = cipher_data(clear_data , clear_data_len , key , key_len ,
encrypted_data , encrypted_data_len );
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚… ๎‚’๎‚…๎‚‘๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚—๎‚“ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰
๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚‰๎‚’๎‚‡๎‚–๎‚๎‚”๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚– ๎‚™๎‚’๎‚‰๎‚’๎‚‡๎‚–๎‚๎‚”๎‚˜๎‚‰๎‚ˆ ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚…๎‡€
#define KEY_SIZE 32U
#define BUFFER_SIZE 512U
/* conventions :
suffix s for sensitive data variablesโ€˜โ€™
clear prefix for unencrypted dataโ€˜โ€™
encrypted prefix for encrypted data */
size_t encrypted_key_len_s = 0;
size_t clear_data_len_s = 0;
size_t encrypted_data_len_s = 0;
uint8_t encrypted_key_s[KEY_SIZE ];
uint8_t clear_data_s[BUFFER_SIZE ];
uint8_t encrypted_data_s[BUFFER_SIZE ];
uint32_t encrypted_error_code_s = 0;
encryptederrorCode = cipher_data(clear_data_s , clear_data_len_s ,
encrypted_key_s , encrypted_key_len_s , encrypted_data_s , encrypted_data_len_s);
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 55
6.13.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€†๎‚๎‚–๎‡€ ๎ˆ“๎‡€๎ˆ” ๎€‹๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚“๎‚š๎‚‰๎‚–๎‚๎‚…๎‚”๎‚”๎‚๎‚’๎‚‹ ๎‚š๎‚๎‚—๎‚๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚˜๎‚๎‚”๎‚“๎‡‚
๎‚‹๎‚–๎‚…๎‚”๎‚Œ๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚™๎‚’๎‚…๎‚‘๎‚†๎‚๎‚‹๎‚™๎‚“๎‚™๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ๎ˆ‘๎‡‚๎€… ๎€—๎‚—๎‚‰ ๎‚š๎‚๎‚—๎‚™๎‚…๎‚๎‚๎‚ ๎‚ˆ๎‚๎‚—๎‚˜๎‚๎‚’๎‚‡๎‚˜ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€๎€•๎€…๎ˆ“๎ˆ๎‡‚๎€… ๎€๎‚‰๎‚š๎‚‰๎‚– ๎‚Œ๎‚…๎‚–๎‚ˆ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ‘๎ˆ”๎ˆ˜ ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎€Š๎‚…๎‚–๎‚ˆ๎‡‚๎€…๎‚“๎‚ˆ๎‚‰๎‚ˆ ๎€’๎‚…๎‚—๎‚—๎‚›๎‚“๎‚–๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ–๎ˆ˜๎ˆ— ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎€Š๎‚…๎‚–๎‚ˆ๎‡‚๎€…๎‚“๎‚ˆ๎‚‰๎‚ˆ ๎€…๎‚–๎‚‰๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚๎‚—๎‡€
56 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
7
Types and type conversions
7.1 Explicit size for integers
Warning
๎€–๎‚Œ๎‚‰ ๎€… ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰ ๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚—๎‚๎‚ž๎‚‰ ๎‚Š๎‚“๎‚– ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚–๎‡€ ๎€‹๎‚’ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚–๎†ฝ
๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ int ๎‚˜๎‚๎‚”๎‚‰๎†ฝ ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚‡๎‚Œ๎‚๎‚˜๎‚‰๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎†ฝ ๎‚๎‚˜ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚“๎‚’ ๎ˆ๎ˆ•๎†ฝ ๎ˆ’๎ˆ‘ ๎‚“๎‚– ๎ˆ•๎ˆ“ ๎‚†๎‚๎‚˜๎‚—๎‡€
๎€–๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰๎†ฝ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
int
๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚— ๎‚–๎‚๎‚—๎‚๎‚ ๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎‚๎‚˜ ๎‚๎‚— ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚—๎‚™๎‚–๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚—๎‚๎‚ž๎‚‰ ๎‚…๎‚’๎‚ˆ
๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚…๎‚’๎‚ ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚› ๎‚“๎‚– ๎‚™๎‚’๎‚‰๎‚œ๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— ๎‚… ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚›๎‚–๎‚…๎‚” ๎‡ฆ๎‚Š๎‚“๎‚– ๎‚™๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ
๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚–๎‚—๎‡ง๎‡€
๎€‹๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚†๎‚‰๎‚—๎‚˜ ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚๎‚— ๎‚˜๎‚๎‚”๎‚‰ ๎‚™๎‚’๎‚๎‚‰๎‚—๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚๎‚— ๎‚‡๎‚‰๎‚–๎‚˜๎‚…๎‚๎‚’ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚š๎‚…๎‚๎‚™๎‚‰
๎‚–๎‚…๎‚’๎‚‹๎‚‰ ๎‚๎‚— ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚๎‚˜๎‚— ๎‚–๎‚…๎‚’๎‚‹๎‚‰ ๎‡ฆ๎‚‰๎‡€๎‚‹๎‡€ ๎‚๎‚’ ๎‚๎‚“๎‚“๎‚” ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚–๎‚—๎‡ง๎‡€
๎€–๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰ ๎‚๎‚˜๎‚— ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚…๎‚–๎‚‹๎‚‰๎‚˜ ๎‚‘๎‚…๎‚‡๎‚Œ๎‚๎‚’๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚๎†ฝ ๎‚๎‚๎‚๎‚‰ ๎‚˜๎‚Œ๎‚“๎‚—๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰
๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ stdint.h๎†ฝ ๎‚…๎‚š๎‚…๎‚๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚๎‚’ ๎€…๎ˆ˜๎ˆ˜๎‡€ ๎€‹๎‚˜๎‚— ๎‚™๎‚—๎‚‰ ๎‚๎‚— ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚”๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚–๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚๎‚‡ int ๎‚˜๎‚๎‚”๎‚‰๎‡€ ๎€‹๎‚’ ๎€…๎ˆ˜๎ˆ๎†ฝ
๎‚‰๎‚•๎‚™๎‚๎‚š๎‚…๎‚๎‚‰๎‚’๎‚˜ ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚๎‚— ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚†๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚๎‚—
๎‚–๎‚‰๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚“๎‚’ ๎‚†๎‚“๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚—๎‚๎‚ž๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚—๎‚๎‚‹๎‚’๎‡€
RECO
77
RECOMMENDATION โ€” Only integer types with an explicit size and sign
should be used
๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚–๎‚‘๎‚“๎‚–๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚๎‚…๎‚๎‚’ char ๎‚˜๎‚๎‚”๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚๎‚‡ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚…๎‚— ๎‚๎‚˜๎‚— ๎‚—๎‚๎‚‹๎‚’ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‡‚
๎Šฑ๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚๎‚— ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‡‚๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚˜๎‚๎‚”๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–
๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚๎‚’๎‚‹๎‡€
RULE
78
RULE โ€” Only signed char and unsigned char types must be
used to handle numeric values
Bad example
#define MAXUINT16 65535U
int value;
char c = 35; /* sign is not specified */
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 57
if (value >= MAXUINT16)
{
/* depending on the architecture */
}
Good example
#include <stdint .h> /* if C99 */
#define MAXUINT16 65535U
unsigned char c = 35U; /* sign is explicit , for numeric value manipulation */
...
uint32_t value ; /* if C99 */
typedef unsigned char uint8_t; /* type definition in C90 */
if (value >= MAXUINT16)
{
/* ... */
}
7.1.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ๎‡€๎ˆ ๎€‘๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚๎‚’๎‚…๎‚”๎‚”๎‚–๎‚“๎‚”๎‚–๎‚๎‚…๎‚˜๎‚‰ ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ๎‡€๎ˆ’ ๎€–๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚’๎‚…๎‚–๎‡‚
๎‚–๎‚“๎‚›๎‚‰๎‚– ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚– ๎‚“๎‚Š ๎‚… ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚…๎‚˜๎‚‰๎‚‹๎‚“๎‚–๎‚๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ๎‡€๎ˆ“ ๎€„๎‚“๎‚˜๎‚Œ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ๎‚— ๎‚“๎‚Š ๎‚…๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚๎‚’ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚™๎‚…๎‚ ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰
๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚…๎‚˜๎‚‰๎‚‹๎‚“๎‚–๎‚๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ—๎‡€๎ˆ ๎€–๎‚๎‚”๎‚‰๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€†๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎ˆ“๎‡€๎ˆ• typedef ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ๎‚’๎‚‰๎‚—๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚”๎‚๎‚…๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎‚†๎‚…๎‚—๎‚๎‚‡ ๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚๎‚‡๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‹๎€๎€–๎ˆ๎ˆ๎‡‚๎€… ๎€—๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚‘๎‚“๎‚ˆ๎‚‰๎‚ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚๎‚“๎‚™๎‚– ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‡ฆ๎‚—๎‡ง๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‹๎€๎€–๎ˆ๎ˆ–๎‡‚๎€… ๎€—๎‚—๎‚‰ ๎‚“๎‚’๎‚๎‚ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚“๎‚– ๎‚™๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚‡๎‚Œ๎‚…๎‚– ๎‚˜๎‚๎‚”๎‚‰ ๎‚Š๎‚“๎‚– ๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚๎‚‡ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‹๎€๎€–๎ˆ’๎ˆ”๎‡‚๎€… ๎€—๎‚—๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡ ๎€•๎€–๎€”๎ˆ๎ˆ๎‡‚๎€… ๎€”๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‚— ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚…๎‚’ ๎‚…๎‚”๎‚”๎‚–๎‚“๎‚”๎‚–๎‚๎‚…๎‚˜๎‚‰ ๎‚˜๎‚๎‚”๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ—๎ˆ‘ ๎€‹๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚‡๎‚…๎‚๎‚‡๎‚™๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
7.2 Type alias
๎€–๎‚Œ๎‚‰ typedef ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚˜๎‚๎‚”๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚Œ๎‚…๎‚— ๎‚๎‚˜๎‚—๎‚‰๎‚๎‚Š ๎‚†๎‚‰๎‚‰๎‚’ ๎‚–๎‚‰๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚™๎‚—๎‚๎‚’๎‚‹ typedef๎‡€
๎€‹๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚› ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚‡๎‚˜๎‚™๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‡€ ๎€–๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚— ๎‚… ๎‚—๎‚๎‚‹๎‚’๎‚๎Šฑ๎‚‡๎‚…๎‚’๎‚˜ ๎‚–๎‚๎‚—๎‚ ๎‚“๎‚Š
๎‚‡๎‚“๎‚’๎‚Š๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚™๎‚”๎‚๎‚๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚๎‚”๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚“๎‚– ๎‚†๎‚ ๎‚‰๎‚œ๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚
๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‚‰๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚“๎‚’๎‚‡๎‚‰๎‡€ ๎€‹๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚๎‚— ๎‚ˆ๎‚“๎‚’๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚…๎‚๎‚๎‚ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰
๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚Š๎‚“๎‚– strong type checking๎†ฝ ๎‚๎‚˜ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚‰๎‚œ๎‚”๎‚๎‚…๎‚๎‚’๎‚‰๎‚ˆ๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚…๎‚๎‚—๎‚“ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚๎‚๎‚‰๎‚ˆ
๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚…๎‚’๎‚ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚Š๎‚™๎‚—๎‚๎‚“๎‚’๎‡€
RECO
79
RECOMMENDATION โ€” Do not rede๎‚พne type aliases
58 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
Bad example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‡ฆ๎€…๎ˆ˜๎ˆ๎‡ง ๎‚—๎‚Œ๎‚“๎‚›๎‚— ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚…๎‚๎‚๎‚…๎‚—๎‚‰๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚˜๎‚๎‚”๎‚‰๎‡€
typedef unsigned short uint16_t; /* definition of type uint16_t */
typedef unsigned short uint16_type; /* type uint16_type is an alias of type
uint16_t */
typedef uint16_t unsigned_short ; /* type unsigned_short is a redefinition of type
uint16_t */
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‡ฆ๎‚๎‚’ ๎€…๎ˆ˜๎ˆ๎†ฝ i.e. ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š stdint.h๎‡ง๎†ฝ ๎‚“๎‚’๎‚๎‚ ๎‚“๎‚’๎‚‰
๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚— ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰๎‡€
typedef unsigned short uint16_t; /* definition of type uint16_t */
7.2.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€’๎€”๎€‡๎ˆ๎ˆ’๎‡‚๎€… ๎€’๎‚–๎‚‰๎‚Š๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰๎‚ˆ๎‚‰๎‚Š๎‚— ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚— ๎‚Š๎‚“๎‚– ๎‚‰๎‚’๎‚‡๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎‚’๎‚“๎‚’๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰๎‚—๎‡€
7.3 Type conversions
๎€… ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚Š๎‚–๎‚“๎‚‘ ๎‚“๎‚’๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚˜๎‚“ ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚–๎‡€ ๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚–๎‚“๎‚‘๎‚“๎‡‚
๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚‡๎‚…๎‚’ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‡ฆ๎‚๎‚“๎‚—๎‚— ๎‚“๎‚Š ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚‘๎‚๎‚—๎‚‡๎‚…๎‚๎‚‡๎‚™๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡ง๎‡€ ๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ
๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚Š๎‚…๎‚‡๎‚๎‚๎‚๎‚˜๎‚…๎‚˜๎‚‰ ๎‚”๎‚–๎‚“๎‚“๎‚Š๎‚–๎‚‰๎‚…๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€ ๎€‹๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰
๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚˜๎‚“ ๎‚…๎‚ˆ๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚…๎‚’๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚˜๎‚“ ๎‚‘๎‚๎‚œ ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚™๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ
๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡ฆ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚—๎†ฟ ๎ŠŠ๎†ฝ ๎‡‚๎†ฝ ๎‡ธ๎†ฝ ๎‡š๎†ฝ ๎Š†๎†ฝ ๎Š‘๎†ฝ ๎Š•๎†ฝ ๎Š”๎†ฝ ๎Š•๎Š๎†ฝ ๎Š”๎Š๎†ฝ ๎Š”๎Š”๎†ฝ ๎Š•๎Š•๎†ฝ etc.๎‡ง๎‡€
๎€๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰๎†ฝ ๎‚›๎‚Œ๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚๎‚’ ๎€…๎ˆ˜๎ˆ ๎‚“๎‚– ๎€…๎ˆ˜๎ˆ˜๎‡€
๎€‘๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚’๎‚‰ ๎‚Œ๎‚…๎‚’๎‚ˆ๎†ฝ integer promotion ๎‚๎‚— ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚›๎‚Œ๎‚“๎‚—๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚— ๎‚—๎‚‘๎‚…๎‚๎‚๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚˜๎‚Œ๎‚‰
int ๎‚˜๎‚๎‚”๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚—๎‚™๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡ฆ๎‚†๎‚๎‚’๎‚…๎‚–๎‚ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚—๎†ฝ ๎‚™๎‚’๎‚…๎‚–๎‚
๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚—๎†ฝ ๎‚—๎‚Œ๎‚๎Šน๎‚—๎†ฝ etc.๎‡ง๎‡€ ๎€–๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚…๎‚’๎‚ˆ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚˜๎‚‰๎‚ˆ
๎‚˜๎‚“ int ๎‚“๎‚– unsigned int๎‡€
๎€‘๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚Œ๎‚…๎‚’๎‚ˆ๎†ฝ type balancing ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚™๎‚…๎‚ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚˜๎‚“ ๎‚… ๎‚‡๎‚“๎‚‘๎‚‘๎‚“๎‚’ ๎‚˜๎‚๎‚”๎‚‰ ๎‚›๎‚Œ๎‚‰๎‚’
๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ๎‚— ๎‚…๎‚–๎‚‰ ๎‚“๎‚Š ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚˜๎‚๎‚”๎‚‰๎‚—๎‡€ ๎€ˆ๎‚๎‚’๎‚…๎‚๎‚๎‚๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚…๎‚—๎‚˜ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜
๎‚“๎‚Š ๎‚… ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚๎‚’ ๎‚… ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚˜๎‚๎‚”๎‚‰๎‡€
Information
๎€†๎‚‰๎‚˜๎‚…๎‚๎‚๎‚— ๎‚“๎‚Š ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚”๎‚–๎‚“๎‚‘๎‚“๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚Š๎‚“๎‚™๎‚’๎‚ˆ ๎‚๎‚’ ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ•๎‡€๎ˆ‘๎‡€๎ˆ๎‡€๎ˆ๎‡€ ๎‚…๎‚’๎‚ˆ ๎ˆ•๎‡€๎ˆ’๎‡€๎ˆ๎‡€๎ˆ๎‡€ ๎‚“๎‚Š ๎‚—๎‚˜๎‚…๎‚’๎‡‚
๎‚ˆ๎‚…๎‚–๎‚ˆ๎‚— ๎‡ฌ๎€ƒ๎‚’๎‚—๎‚๎€…๎ˆ˜๎ˆ๎‡ญ ๎‚…๎‚’๎‚ˆ ๎‡ฌ๎€ƒ๎‚’๎‚—๎‚๎€…๎ˆ˜๎ˆ˜๎‡ญ ๎‚–๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚๎‚๎‡€ ๎€ˆ๎‚“๎‚– ๎‚˜๎‚๎‚”๎‚‰ ๎‚†๎‚…๎‚๎‚…๎‚’๎‚‡๎‚๎‚’๎‚‹๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚๎‚‰๎‚š๎‚…๎‚’๎‚˜ ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—
๎‚…๎‚–๎‚‰ ๎ˆ•๎‡€๎ˆ‘๎‡€๎ˆ๎‡€๎ˆ” ๎‚…๎‚’๎‚ˆ ๎ˆ•๎‡€๎ˆ’๎‡€๎ˆ๎‡€๎ˆ— ๎‚“๎‚Š ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‚— ๎‡ฌ๎€ƒ๎‚’๎‚—๎‚๎€…๎ˆ˜๎ˆ๎‡ญ ๎‚…๎‚’๎‚ˆ ๎‡ฌ๎€ƒ๎‚’๎‚—๎‚๎€…๎ˆ˜๎ˆ˜๎‡ญ ๎‚–๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚๎‚๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 59
RULE
80
RULE โ€” Detailed and precise understanding of the conversion rules
๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚’๎‚‰๎‚‰๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚๎‚’๎‚“๎‚› ๎‚…๎‚’๎‚ˆ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚…๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚–๎‚™๎‚๎‚‰๎‚— ๎‚Š๎‚“๎‚–
๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰๎‚—๎‡€
๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚‘๎‚™๎‚—๎‚˜ ๎‚‘๎‚…๎‚๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚…๎‚’๎‚ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚—๎‡€ ๎€–๎‚Œ๎‚‰
๎‚‡๎‚๎‚…๎‚—๎‚—๎‚๎‚‡ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚— ๎‚“๎Šน๎‚‰๎‚’ ๎‚… ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚๎‚— ๎‚…๎‚’ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚™๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ
๎‚˜๎‚๎‚”๎‚‰๎‚—๎‡€
RULE
81
RULE โ€” Explicit conversions between signed and unsigned types
๎€’๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚—๎‡€ ๎€—๎‚—๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚–๎‚๎‚ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’
๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚™๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚˜๎‚๎‚”๎‚‰๎‚—๎‡€
Bad example
signed int v1 = -1;
unsigned int v2 = 1;
if (v1 < v2)
{
/* v1 converted to unsigned int and value -1 becomes UINT_MAX , therefore the if
condition is always false */
}
Good example
signed int v1 = -1;
unsigned int v2 = 1;
if (v1 < (signed int)v2)
{
/* v2 is explicitly converted to a signed integer - the condition is true */
}
๎€ƒ๎‚‹๎‚…๎‚๎‚’ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚–๎‚‰๎‚…๎‚—๎‚“๎‚’๎‚—๎†ฝ ๎‚’๎‚“ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚…๎‚’ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰ ๎‚…๎‚’๎‚ˆ
๎‚… ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚– ๎‚Š๎‚–๎‚“๎‚‘ ๎‚…๎‚’ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰ ๎‚˜๎‚“ ๎‚… ๎‚—๎‚‘๎‚…๎‚๎‚๎‚‰๎‚– ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚๎‚๎‚’๎‚‰๎‚—๎†ฝ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‡€
uint32_t u32;
int32_t s32;
uint16_t u16;
double dbl;
uint8_t idx;
s32 = 42;
u32 = s32; /* implicit conversion */
u16 = u32 + 2 * s32; /* implicit conversion to a smaller type */
dbl = u32 / u16; /* the result is 0 ( integer division ) */
s32 = dbl; /* implicit conversion float -> integer */
/* the following loop is infinite: idx being unsigned , idx >= 0 is always true
since an unsigned value cannot be negative */
for(idx = 27; idx >= 0; idx --) {
...
}
60 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
Tolerated example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚—๎‚Œ๎‚“๎‚›๎‚— ๎‚… ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚’ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‡€ ๎€–๎‚Œ๎‚๎‚—
๎‚๎‚— ๎‚‡๎‚…๎‚๎‚๎‚‰๎‚ˆ ๎‚… ๎‚˜๎‚“๎‚๎‚‰๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚†๎‚‰๎‚‡๎‚…๎‚™๎‚—๎‚‰ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚‡๎‚๎‚‰๎‚…๎‚’๎‚‰๎‚– ๎‚—๎‚“๎‚๎‚™๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— ๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚๎‚’๎‚‹
๎‚˜๎‚Œ๎‚‰ ๎‚๎‚“๎‚“๎‚” ๎‚๎‚’๎‚ˆ๎‚‰๎‚œ ๎‚‰๎‚œ๎‚๎‚—๎‚˜๎‡€
uint32_t u32;
int32_t s32;
uint16_t u16;
double dbl;
uint8_t idx;
s32 = 42;
u32 = (uint32_t)s32;
u16 = (uint16_t)(( int32_t )u32 + 2 * s32);
dbl = ( double)u16 / (double )u32;
/* the signed integer cast is used to avoid an infinite loop */
for(idx = 27; ( int8_t)idx >= 0; idx --) {
...
}
Good example
int32_t s32;
uint16_t u16;
double dbl;
uint8_t idx;
s32 = 42;
u32 = (uint32_t)s32;
u16 = (uint16_t)(( int32_t )u32 + 2 * s32);
dbl = ( double)u16 / (double )u32;
/* change of the loop */
idx=27;
while (idx >0)
{
...
}
Information
-Wconversion ๎‚…๎‚’๎‚ˆ -Wsign-conversion ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚”๎‚–๎‚“๎‚š๎‚๎‚ˆ๎‚‰๎‚ˆ ๎‚†๎‚ ๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ ๎‚‡๎‚…๎‚’
๎‚Œ๎‚‰๎‚๎‚” ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚—๎‡€
7.3.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ๎‡€๎ˆ ๎€‘๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚๎‚’๎‚…๎‚”๎‚”๎‚–๎‚“๎‚”๎‚–๎‚๎‚…๎‚˜๎‚‰ ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ๎‡€๎ˆ’ ๎€–๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚’๎‚…๎‚–๎‡‚
๎‚–๎‚“๎‚›๎‚‰๎‚– ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚– ๎‚“๎‚Š ๎‚… ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚…๎‚˜๎‚‰๎‚‹๎‚“๎‚–๎‚๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ๎‡€๎ˆ“ ๎€„๎‚“๎‚˜๎‚Œ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ๎‚— ๎‚“๎‚Š ๎‚…๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚๎‚’ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚™๎‚…๎‚ ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰
๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚…๎‚˜๎‚‰๎‚‹๎‚“๎‚–๎‚๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ๎‡€๎ˆ” ๎€–๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚…๎‚—๎‚˜ ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚๎‚’๎‚…๎‚”๎‚”๎‚–๎‚“๎‚”๎‚–๎‚๎‚…๎‚˜๎‚‰ ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚
๎‚˜๎‚๎‚”๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ๎‡€๎ˆ• ๎€–๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚‡๎‚“๎‚‘๎‚”๎‚“๎‚—๎‚๎‚˜๎‚‰ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ
๎‚›๎‚๎‚ˆ๎‚‰๎‚– ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 61
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ๎‡€๎ˆ– ๎€‹๎‚Š ๎‚… ๎‚‡๎‚“๎‚‘๎‚”๎‚“๎‚—๎‚๎‚˜๎‚‰ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚“๎‚’๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚๎‚’ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚˜๎‚Œ๎‚‰
๎‚™๎‚—๎‚™๎‚…๎‚ ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚›๎‚๎‚ˆ๎‚‰๎‚– ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚
๎‚˜๎‚๎‚”๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ๎‡€๎ˆ— ๎€–๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚‡๎‚“๎‚‘๎‚”๎‚“๎‚—๎‚๎‚˜๎‚‰ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚…๎‚—๎‚˜ ๎‚˜๎‚“ ๎‚… ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚
๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚…๎‚˜๎‚‰๎‚‹๎‚“๎‚–๎‚ ๎‚“๎‚– ๎‚… ๎‚›๎‚๎‚ˆ๎‚‰๎‚– ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‹๎€๎€–๎ˆ๎ˆ‘๎‡‚๎€… ๎€—๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚–๎‚™๎‚๎‚‰๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‹๎€๎€–๎ˆ’๎ˆ๎‡‚๎€… ๎€‡๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚™๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚›๎‚–๎‚…๎‚”๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‹๎€๎€–๎ˆ’๎ˆ๎‡‚๎€… ๎€‡๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚๎‚’ ๎‚๎‚“๎‚—๎‚˜ ๎‚“๎‚– ๎‚‘๎‚๎‚—๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚”๎‚–๎‚‰๎‚˜๎‚‰๎‚ˆ ๎‚ˆ๎‚…๎‚˜๎‚…๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‹๎€๎€–๎ˆ’๎ˆ‘๎‡‚๎€… ๎€‡๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚’ ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚–๎‚— ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚๎‚’ ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‹๎€๎€–๎ˆ๎ˆ—๎‡‚๎€… ๎€‡๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚๎‚’ ๎‚… ๎‚๎‚…๎‚–๎‚‹๎‚‰๎‚– ๎‚—๎‚๎‚ž๎‚‰ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚’๎‚‹ ๎‚“๎‚– ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚๎‚’๎‚‹ ๎‚˜๎‚“
๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚—๎‚๎‚ž๎‚‰๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‡๎€š๎€’๎ˆ๎ˆ“๎‡‚๎€… ๎€„๎‚‰๎‚›๎‚…๎‚–๎‚‰ ๎‚“๎‚Š ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚”๎‚–๎‚“๎‚‘๎‚“๎‚˜๎‚๎‚“๎‚’ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚๎‚’๎‚‹ ๎‚†๎‚๎‚˜๎‚›๎‚๎‚—๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚’ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚–
๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚—๎‚‘๎‚…๎‚๎‚๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚๎‚’๎‚˜๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ๎ˆ˜๎ˆ ๎€‹๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚› ๎‚“๎‚– ๎‚›๎‚–๎‚…๎‚”๎‚…๎‚–๎‚“๎‚™๎‚’๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ๎ˆ˜๎ˆ‘ ๎€‹๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚‡๎‚“๎‚‰๎‚–๎‚‡๎‚๎‚“๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ๎ˆ˜๎ˆ– ๎€๎‚™๎‚‘๎‚‰๎‚–๎‚๎‚‡ ๎€–๎‚–๎‚™๎‚’๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎€‡๎‚–๎‚–๎‚“๎‚–๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ—๎ˆ ๎€‹๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚๎‚‡๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰๎‚—๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ–๎ˆ๎ˆ“ ๎€‹๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎€–๎‚๎‚”๎‚‰ ๎€…๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚– ๎€…๎‚…๎‚—๎‚˜๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€…๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‚— ๎‚˜๎‚“ ๎‚›๎‚๎‚ˆ๎‚‰๎‚– ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚… ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚ ๎‚Š๎‚“๎‚– ๎€‡๎€‘๎€ˆ ๎‡ฌ๎‚—๎‚๎‚‹๎‚’๎‚‡๎‚“๎‚’๎‚š๎‡ญ๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€‘๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚–๎‚— ๎‡ฌ๎‚๎‚’๎‚˜๎‚“๎Šท๎‚“๎‚›๎‡ญ๎‡€
7.4 Type conversion of pointers to structured variables of
different types
๎€–๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚“๎‚– ๎‚˜๎‚“ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚š๎‚๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚‡๎‚…๎‚’ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚๎‚’ ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‚— ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚…๎‚–๎‚‹๎‚‰๎‚˜
๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚— ๎‚๎‚…๎‚–๎‚‹๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚–๎‚‰๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“๎‡€ ๎€‹๎‚’๎‚ˆ๎‚‰๎‚‰๎‚ˆ๎†ฝ ๎‚… ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚“๎‚’๎‚‰ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚˜๎‚“
๎‚… ๎‚๎‚…๎‚–๎‚‹๎‚‰๎‚– ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎†ฝ ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚‹๎‚๎‚š๎‚‰๎‚— ๎‚™๎‚’๎‚ˆ๎‚‰๎‚—๎‚๎‚–๎‚…๎‚†๎‚๎‚‰ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚˜๎‚“ ๎‚…๎‚–๎‚‰๎‚…๎‚— ๎‚“๎‚Š ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚“๎‚™๎‚˜๎‚—๎‚๎‚ˆ๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚
๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‡€
๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚Š๎‚–๎‚“๎‚‘๎‡š๎‚˜๎‚“ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚”๎‚–๎‚“๎‚“๎‚Š๎‚–๎‚‰๎‚…๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚‡๎‚“๎‚‘๎‡‚
๎‚”๎‚๎‚‰๎‚œ๎‡€
RECO
82
RECOMMENDATION โ€” Do not use pointer type conversion on types struc-
tured differently
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚›๎‚๎‚๎‚ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚๎‚’ ๎‚…๎‚’ ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‡€
#define TAB_SIZE 16U
typedef struct {
int32_t magic;
} s_a;
typedef struct {
62 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
int32_t magic;
int16_t s;
uint8_t x[TAB_SIZE ];
} s_b;
void foo(s_a* structa) {
s_b* p = (s_b *)structa; /* type conversion to be excluded */
p->magic = 0 xBAADCAFE;
p->s = 0xDEAD; /* overflow outside of the structure s_a */
p->x[0] = 4; /* and risk of overwritten data (buffer overflow) */
}
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚’๎‚“ ๎‚๎‚“๎‚’๎‚‹๎‚‰๎‚– ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ๎‡€
#define TAB_SIZE 16U
typedef struct {
int32_t magic;
} s_a;
typedef struct {
s_a h;
int16_t s;
uint8_t x[TAB_SIZE ];
} s_b;
void foo(s_b* structb) {
structb ->h.magic = 0xCAFEBABE ;
structb ->s = 0 xBEEF;
structb ->x [0] = 4;
}
7.4.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ๎‡€๎ˆ‘ ๎€…๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚๎‚’๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚˜๎‚‰
๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’๎‚ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ๎‡€๎ˆ’ ๎€ƒ ๎‚‡๎‚…๎‚—๎‚˜ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚“ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚๎‚”๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–
๎‚˜๎‚“ ๎‚… ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚๎‚”๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ๎‡€๎ˆ— ๎€ƒ ๎‚‡๎‚…๎‚—๎‚˜ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚‘๎‚“๎‚š๎‚‰ ๎‚… ๎‚‡๎‚“๎‚’๎‚—๎‚˜ ๎‚“๎‚– ๎‚š๎‚“๎‚๎‚…๎‚˜๎‚๎‚๎‚‰ ๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚ˆ
๎‚˜๎‚“ ๎‚†๎‚ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‡๎€š๎€’๎ˆ’๎ˆ•๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚…๎‚—๎‚˜ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚๎‚’๎‚˜๎‚“ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‚๎‚ ๎‚…๎‚๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰๎‚—๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ–๎ˆ๎ˆ“ ๎€‹๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚– ๎‚‡๎‚…๎‚—๎‚˜๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€…๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚˜๎‚๎‚’๎‚‹ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚˜๎‚“ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‚๎‚ ๎‚…๎‚๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‡ฌ๎‚…๎‚๎‚๎‚‹๎‚’๎‚‡๎‚“๎‚’๎‚š๎‡ญ๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 63
8
Pointers and arrays
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚๎‚— ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚›๎‚‰ ๎‚…๎‚–๎‚‰ ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚–๎‚๎‚’๎‚‹ ๎‚“๎‚’๎‚๎‚ ๎‚˜๎‚“ ๎‚“๎‚’๎‚‰๎‡‚๎‚ˆ๎‚๎‚‘๎‚‰๎‚’๎‚—๎‚๎‚“๎‚’๎‚…๎‚ ๎‚…๎‚–๎‚–๎‚…๎‚๎‚—๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚…๎‚— ๎‚…๎‚’๎‚ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‡‚๎‚ˆ๎‚๎‚‘๎‚‰๎‚’๎‚—๎‚๎‚“๎‚’๎‚…๎‚ ๎‚…๎‚–๎‚–๎‚…๎‚
๎‚‡๎‚…๎‚’ ๎‚…๎‚๎‚—๎‚“ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚š๎‚๎‚… ๎‚… ๎‚“๎‚’๎‚‰๎‡‚๎‚ˆ๎‚๎‚‘๎‚‰๎‚’๎‚—๎‚๎‚“๎‚’๎‚…๎‚ ๎‚…๎‚–๎‚–๎‚…๎‚๎†ฝ ๎‚…๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚™๎‚๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰
๎‚…๎‚๎‚—๎‚“ ๎‚…๎‚”๎‚”๎‚๎‚ ๎‚˜๎‚“ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‡‚๎‚ˆ๎‚๎‚‘๎‚‰๎‚’๎‚—๎‚๎‚“๎‚’๎‚…๎‚ ๎‚…๎‚–๎‚–๎‚…๎‚๎‚—๎‡€
8.1 Standardised access to the elements of an array
๎€…๎‚“๎‚’๎‚Š๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚๎‚— ๎‚…๎‚’๎‚ˆ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚๎‚— ๎‚‡๎‚“๎‚‘๎‚‘๎‚“๎‚’๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚๎‚˜ ๎‚๎‚— ๎‚“๎Šน๎‚‰๎‚’ ๎‚…๎‚—๎‚—๎‚™๎‚‘๎‚‰๎‚ˆ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚‰๎‚—
๎‚…๎‚— ๎‚… ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚“ ๎‚๎‚˜๎‚— ๎Šฑ๎‚–๎‚—๎‚˜ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚— ๎‚… ๎‚—๎‚Œ๎‚“๎‚–๎‚˜๎‚‡๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚”๎‚–๎‚“๎‚š๎‚‰๎‚— ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚Š๎‚…๎‚๎‚—๎‚‰ ๎‚๎‚’
๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚๎‡€
๎€–๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰๎†ฝ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฟ
int32_t tab1 [6];
int32_t * tab2 = malloc(6 * sizeof(int32_t ));
int32_t * tab3 = tab1;
int32_t * tab4 = tab2;
printf ("tab1: %p, %p, %p\n", tab1 , &tab1 [0], &tab1);
printf ("tab2: %p, %p, %p\n", tab2 , &tab2 [0], &tab2);
printf ("tab3: %p, %p, %p\n", tab3 , &tab3 [0], &tab3);
printf ("tab4: %p, %p, %p\n", tab4 , &tab4 [0], &tab4);
๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚“๎‚†๎‚˜๎‚…๎‚๎‚’๎‚‰๎‚ˆ ๎‚๎‚— ๎‚…๎‚— ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚—๎†ฟ
tab1 : 1559248928 , 1559248928 , 1559248928 /* tab1 =&tab1 [0]=& tab1 all represent the address of
the first element in the array */
tab2 : 911295072 , 911295072 , 1559248904 /* &tab2 is the address of the pointer returned by
malloc , pointing to the array , and tab2 (or & tab2 [0])
is the address of the first element of the array
tab2 */
tab3 : 1559248928 , 1559248928 , 1559248912 /* similar case to tab2 */
tab4 : 911295072 , 911295072 , 1559248920 /* similar case to tab2 */
๎€–๎‚Œ๎‚‰ ๎‚’๎‚™๎‚…๎‚’๎‚‡๎‚‰๎‚— ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚๎‚— ๎‚…๎‚’๎‚ˆ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚“๎‚™๎‚— ๎‚…๎‚’๎‚ˆ ๎‚›๎‚‰ ๎‚‡๎‚…๎‚’ ๎‚“๎‚’๎‚๎‚ ๎‚ˆ๎‚–๎‚…๎‚› ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚…๎‚ˆ๎‚‰๎‚–๎‡‹๎‚— ๎‚…๎‚˜๎‚˜๎‚‰๎‚’๎‡‚
๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚๎‚— ๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚™๎‚–๎‚‹๎‚‰ ๎‚˜๎‚Œ๎‚‰๎‚‘ ๎‚˜๎‚“ ๎‚˜๎‚–๎‚‰๎‚…๎‚ˆ ๎‚‡๎‚…๎‚–๎‚‰๎‚Š๎‚™๎‚๎‚๎‚๎‡€
๎€ƒ๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚‡๎‚“๎‚’๎‚Š๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚—๎†ฟ
int *var[N];
int (*var2)[N];
๎€–๎‚Œ๎‚‰ ๎Šฑ๎‚–๎‚—๎‚˜ ๎‚๎‚๎‚’๎‚‰ ๎‚๎‚’๎‚š๎‚“๎‚๎‚š๎‚‰๎‚— ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚๎‚’๎‚‹ N int ๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚๎‚’ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚๎†ฝ i.e. ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚“๎‚Š N int ๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚—๎‡€
๎€–๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚‡๎‚“๎‚’๎‚ˆ ๎‚๎‚๎‚’๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚— ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚“๎‚Š N int ๎‚˜๎‚๎‚”๎‚‰ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚๎‚’ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚๎‡€
๎€–๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚‡๎‚๎‚…๎‚–๎‚๎Šฑ๎‚‰๎‚— ๎‚˜๎‚Œ๎‚๎‚— ๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚†๎‚ ๎‚‰๎‚œ๎‚”๎‚๎‚…๎‚๎‚’๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚’๎‚ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰ expression ๎‚๎‚— ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…
๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚–๎‚—๎‚˜ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚…๎‚’๎‚ˆ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚…๎‚’ lvalue ๎‚‰๎‚œ๎‚‡๎‚‰๎‚”๎‚˜ ๎‚›๎‚Œ๎‚‰๎‚’
64 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚…๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— sizeof๎†ฝ _Alignof ๎‚“๎‚– & ๎‚“๎‚– ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‡‚
๎‚—๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚… ๎‚๎‚๎‚˜๎‚‰๎‚–๎‚…๎‚ ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚๎‡€
Expression
๎€ƒ๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚…๎‚’ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚๎‚’ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚†๎‚™๎‚˜ ๎‚… ๎‚”๎‚๎‚‰๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— a+b ๎‚“๎‚–
&a๎†ฝ ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎‡€
Lvalue
๎€ƒ๎‚’ lvalue ๎‡ฆ๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚“๎‚– ๎‚š๎‚…๎‚๎‚™๎‚‰๎‡ง ๎‚๎‚— ๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚˜๎‚๎‚”๎‚‰๎†ฝ ๎‚‰๎‚š๎‚‰๎‚’ ๎‚๎‚’๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚˜๎‚‰๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜
๎‚Š๎‚–๎‚“๎‚‘ ๎‚š๎‚“๎‚๎‚ˆ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚๎‚— ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚…๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚๎‚’ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚๎‡€
๎€ƒ๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚… ๎‚‘๎‚“๎‚ˆ๎‚๎Šฑ๎‚…๎‚†๎‚๎‚‰ lvalue๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚‘๎‚‰๎‚…๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚˜ ๎‚‡๎‚…๎‚’๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ๎†ฝ ๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚– ๎‚‘๎‚“๎‚ˆ๎‚๎‡‚
๎Šฑ๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚๎‡€
int tab[N];
tab = 0; // error
tab --; // error
๎€™๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚—
๎‚… ๎‚—๎‚๎‚‘๎‚”๎‚๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚๎‚— ๎‚’๎‚“ ๎‚๎‚“๎‚’๎‚‹๎‚‰๎‚– ๎‚…๎‚’ lvalue๎‡€
Warning
๎€ˆ๎‚“๎‚– ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ tab๎†ฝ ๎‚˜๎‚Œ๎‚‰ tab ๎‚…๎‚’๎‚ˆ &tab[0] ๎‚’๎‚“๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚–๎‚—๎‚˜
๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚‡๎‚–๎‚‰๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚๎‡€ ๎€–๎‚Œ๎‚‰ &tab ๎‚’๎‚“๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚Œ๎‚…๎‚’๎‚ˆ๎†ฝ ๎‚›๎‚๎‚๎‚
๎‚š๎‚…๎‚–๎‚๎‡€ ๎€™๎‚Œ๎‚‰๎‚’ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚๎‚— ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚—๎‚˜๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚‡๎‚…๎‚’๎‚’๎‚“๎‚˜ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰
๎‚๎‚— ๎‚’๎‚“ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚‡๎‚–๎‚‰๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚— ๎‚—๎‚™๎‚‡๎‚Œ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚๎†ฟ ๎‚˜๎‚Œ๎‚‰ tab ๎‚’๎‚“๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚—๎‚๎‚‘๎‚๎‚๎‚…๎‚– ๎‚˜๎‚“ ๎‚… ๎‚๎‚…๎‚†๎‚‰๎‚
๎‚‘๎‚…๎‚’๎‚…๎‚‹๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚๎‡€
n ๎€–๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰๎†ฝ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚๎‚— ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚—๎‚˜๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‡ฆ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š tab1 ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚š๎‚๎‚“๎‚™๎‚— ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎‡ง๎†ฝ
&tab ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚–๎‚—๎‚˜ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚๎†ฝ i.e. ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚—
๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚๎‡€
n
๎€‘๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚Œ๎‚…๎‚’๎‚ˆ๎†ฝ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚๎‚— ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡๎‚…๎‚๎‚๎‚๎†ฝ ๎‚˜๎‚Œ๎‚‰ tab ๎‚’๎‚“๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚–๎‚‰๎‚”๎‡‚
๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚‡๎‚–๎‚‰๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚’๎‚ˆ
๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰๎†ฝ &tab ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‡ฆ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š tab2 ๎‚๎‚’
๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎‡ง๎‡€
๎€–๎‚Œ๎‚‰ ๎€… ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚๎„…๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚… tab ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚‰๎‚˜๎‚ ๎‚“๎‚Š ๎‚›๎‚…๎‚๎‚—๎†ฝ
๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚… ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚“๎‚– ๎‚‡๎‚“๎‚’๎‚Š๎‚™๎‚—๎‚๎‚“๎‚’๎‡€
Warning
๎€ˆ๎‚“๎‚– ๎‚… ๎‚˜๎‚…๎‚† ๎‚…๎‚–๎‚–๎‚…๎‚๎†ฝ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚๎„…๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚›๎‚–๎‚๎‚˜๎‚˜๎‚‰๎‚’๎†ฟ
*(tab+i); /* usual notation 1 */
tab[i]; /* usual notation 2 */
*(i+tab); /* interchangeable array and index ! */
i[tab ]; /* interchangeable array and index ! */
๎€–๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚’๎‚“๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚…๎‚๎‚ ๎‚–๎‚‰๎‚‡๎‚“๎‚‹๎‚’๎‚๎‚—๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚๎‚—
๎‚‡๎‚…๎‚’ ๎‚•๎‚™๎‚๎‚‡๎‚๎‚๎‚ ๎‚–๎‚‰๎‚ˆ๎‚™๎‚‡๎‚‰ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€ ๎€๎‚“๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜๎†ฝ ๎‚‰๎‚š๎‚‰๎‚’ ๎‚›๎‚๎‚˜๎‚Œ ๎‚ˆ๎‚‰๎‚‘๎‚…๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚‘๎‡‚
๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚’๎‚‰๎‚๎‚˜๎‚Œ๎‚‰๎‚– ๎„Œ๎„ˆ๎„ˆ ๎‚’๎‚“๎‚– ๎€…๎„‘๎„†๎„“๎„Œ ๎‚›๎‚๎‚๎‚ ๎‚๎‚—๎‚—๎‚™๎‚‰ ๎‚…๎‚๎‚‰๎‚–๎‚˜๎‚— ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚“๎‚Š ๎‚’๎‚“๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 65
๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚… ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚—๎‡€
๎€‹๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚…๎‚’๎‚ ๎‚…๎‚‘๎‚†๎‚๎‚‹๎‚™๎‚๎‚˜๎‚ ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚๎‚—๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚”๎‚“๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚—๎†ฝ
๎‚’๎‚“๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚“๎‚๎‚‰๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚ˆ๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚๎‚’๎‚š๎‚‰๎‚–๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚ˆ๎‚‰๎‚œ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚›๎‚๎‚๎‚
๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
RULE
83
RULE โ€” Access to the elements of an array will always be by designating
as the ๎‚พrst attribute the array and as the second attribute the index of the
element concerned
๎€ƒ๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚๎„…๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚›๎‚๎‚๎‚ ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚†๎‚‰ ๎‚›๎‚–๎‚๎‚˜๎‚˜๎‚‰๎‚’ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚
๎Šฑ๎‚–๎‚—๎‚˜ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚ˆ๎‚‰๎‚œ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚…๎‚‡๎‚Œ๎‚‰๎‚ˆ๎‡€
๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚–๎‚‘๎‚“๎‚–๎‚‰๎†ฝ ๎‚…๎‚‹๎‚…๎‚๎‚’ ๎‚Š๎‚“๎‚– ๎‚–๎‚‰๎‚…๎‚—๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚˜๎‚–๎‚…๎‚’๎‚—๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚‡๎‚๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚๎‚‡๎‚…๎‚ ๎‚’๎‚“๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚๎‚— ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚—๎‚•๎‚™๎‚…๎‚–๎‚‰
๎‚†๎‚–๎‚…๎‚‡๎‚๎‚‰๎‚˜๎‚— [] ๎‚›๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚”๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚–๎‚‰๎‚ˆ๎‡€
RECO
84
RECOMMENDATION โ€” Access to elements in an array should be using
square brackets
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚’๎‚“๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡ฆ๎‚š๎‚๎‚… ๎‚—๎‚•๎‚™๎‚…๎‚–๎‚‰ ๎‚†๎‚–๎‚…๎‚‡๎‚๎‚‰๎‚˜๎‚—๎‡ง
๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚…๎‚’๎‚ ๎‚…๎‚‘๎‚†๎‚๎‚‹๎‚™๎‚๎‚˜๎‚๎‡€
Bad example
for (i = 0; i< size_tab; i++) {
*(i+tab) = i; /* the square brackets are not used and the index is in the first
position */
...
}
Good example
for (i = 0; i < size_tab; i++) {
tab[i] = i;
...
}
8.1.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€ƒ๎€”๎€”๎ˆ๎ˆ๎‡‚๎€… ๎€—๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚Œ๎‚“๎‚› ๎‚…๎‚–๎‚–๎‚…๎‚๎‚— ๎‚›๎‚“๎‚–๎‚๎‡€
8.2 Non-use of VLAs
๎€–๎‚Œ๎‚‰ ๎€˜๎€Ž๎€ƒ๎‚—
๎ˆ˜
๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎€…๎ˆ˜๎ˆ˜ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚–๎‚–๎‚…๎‚๎‚— ๎‚›๎‚Œ๎‚“๎‚—๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜
๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚…๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚†๎‚™๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ ๎‚…๎‚’ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚๎‚‘๎‡‚
๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚๎‚’๎‚‹ ๎‚…๎‚’ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚‡๎‚๎‡€ ๎€‹๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‚๎‚ ๎‚”๎‚“๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰๎†ฝ
๎ˆ˜๎‡€ ๎€˜๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‡‚๎€Ž๎‚‰๎‚’๎‚‹๎‚˜๎‚Œ ๎€ƒ๎‚–๎‚–๎‚…๎‚
66 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎‚˜๎‚Œ๎‚๎‚— ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚“๎‚Š ๎€…๎‡€ ๎€๎‚“๎‚–๎‚‰๎‚“๎‚š๎‚‰๎‚–๎†ฝ ๎‚Š๎‚“๎‚– ๎‚…๎‚’ ๎‚‰๎‚œ๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚š๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚—๎‚๎‚ž๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘
๎‚‘๎‚…๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚‰ ๎‚…๎‚— ๎‚‰๎‚œ๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ๎‡€ ๎€ˆ๎‚๎‚’๎‚…๎‚๎‚๎‚๎†ฝ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚—๎‚…๎‚๎‚ˆ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚๎‚๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰๎‚–๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚…
๎‚š๎‚™๎‚๎‚’๎‚‰๎‚–๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚๎‡€ ๎€ˆ๎‚“๎‚– ๎‚…๎‚๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚–๎‚‰๎‚…๎‚—๎‚“๎‚’๎‚—๎†ฝ ๎€˜๎€Ž๎€ƒ๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
Information
๎€–๎‚Œ๎‚‰ -Wvla ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚–๎‚“๎‚š๎‚๎‚ˆ๎‚‰๎‚— ๎‚…๎‚๎‚‰๎‚–๎‚˜๎‚— ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎€˜๎€Ž๎€ƒ๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€
RULE
85
RULE โ€” Do not use VLAs
8.2.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ—๎‡€๎ˆ— ๎€˜๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‡‚๎‚๎‚‰๎‚’๎‚‹๎‚˜๎‚Œ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€ƒ๎€”๎€”๎ˆ’๎ˆ‘๎‡‚๎€… ๎€‡๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚Š๎‚“๎‚– ๎€˜๎€Ž๎€ƒ ๎‚…๎‚–๎‚‰ ๎‚๎‚’ ๎‚… ๎‚š๎‚…๎‚๎‚๎‚ˆ ๎‚–๎‚…๎‚’๎‚‹๎‚‰๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€๎€‡๎€๎ˆ๎ˆ”๎‡‚๎€… ๎€ƒ๎‚š๎‚“๎‚๎‚ˆ ๎‚๎‚…๎‚–๎‚‹๎‚‰ ๎‚—๎‚˜๎‚…๎‚‡๎‚ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ–๎ˆ”๎ˆ— ๎€”๎‚‰๎‚๎‚๎‚…๎‚’๎‚‡๎‚‰ ๎‚“๎‚’ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ๎†ฝ ๎‚™๎‚’๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ๎†ฝ ๎‚“๎‚– ๎€‹๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‡‚๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚–๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€–๎‚…๎‚๎‚’๎‚˜๎‚‰๎‚ˆ๎†ฝ ๎‚”๎‚“๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚๎‚๎‚ ๎‚‘๎‚™๎‚˜๎‚๎‚๎‚…๎‚˜๎‚‰๎‚ˆ๎†ฝ ๎‚“๎‚– ๎‚“๎‚™๎‚˜๎‡‚๎‚“๎‚Š๎‡‚๎‚ˆ๎‚“๎‚‘๎‚…๎‚๎‚’ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚… ๎‚–๎‚‰๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‚‰๎‚ˆ
๎‚—๎‚๎‚’๎‚ ๎‡ฌ๎‚˜๎‚…๎‚๎‚’๎‚˜๎‚—๎‚๎‚’๎‚๎‡ญ๎‡€
8.3 Explicit array size
๎€–๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚“๎‚™๎‚˜ ๎‚“๎‚Š ๎‚†๎‚“๎‚™๎‚’๎‚ˆ๎‚— ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚…๎‚
๎‚—๎‚‰๎‚‰๎‚‘ ๎‚–๎‚‰๎‚ˆ๎‚™๎‚’๎‚ˆ๎‚…๎‚’๎‚˜ ๎‚๎‚’ ๎‚–๎‚‰๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚“ ๎‚… ๎‚”๎‚–๎‚‰๎‚š๎‚๎‚“๎‚™๎‚— ๎‚–๎‚™๎‚๎‚‰ ๎‚๎‚‘๎‚”๎‚“๎‚—๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚‡๎‚™๎‚— ๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚—
๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚๎‚—๎‡€
RECO
86
RECOMMENDATION โ€” Do not use an implicit size for arrays
๎€‹๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚š๎‚…๎‚๎‚๎‚ˆ๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚—๎‚๎‚ž๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚†๎‚‰๎‚๎‚“๎‚›๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚๎‚— ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚“๎‚’ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
int32_t tab [] = { 1, 2, 3 }; /* array 3 elements , implicit size */
Good example
๎€–๎‚Œ๎‚๎‚— ๎‚˜๎‚๎‚‘๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚๎‚— ๎‚๎‚— ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ๎‡€
int32_t tab [3] = { 1, 2, 3 }; /* array of 3 elements , explicit size , with
initialisation */
int32_t tab2 [2] = { 2, 3 }; /* array of 2 รฉlรฉments , explicit size , with
initialisation */
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 67
8.3.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ—๎‡€๎ˆ๎ˆ ๎€™๎‚Œ๎‚‰๎‚’ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚›๎‚๎‚˜๎‚Œ ๎‚‰๎‚œ๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚ ๎‚๎‚๎‚’๎‚๎‚…๎‚‹๎‚‰ ๎‚๎‚— ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ๎†ฝ ๎‚๎‚˜๎‚— ๎‚—๎‚๎‚ž๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚
๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ˜๎‡€๎ˆ” ๎€™๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚—๎‚๎‚‹๎‚’๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š
๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€ƒ๎€”๎€”๎ˆ’๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚Š๎‚“๎‚–๎‚‘ ๎‚“๎‚– ๎‚™๎‚—๎‚‰ ๎‚“๎‚™๎‚˜๎‡‚๎‚“๎‚Š๎‡‚๎‚†๎‚“๎‚™๎‚’๎‚ˆ๎‚— ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚– ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚—๎‚™๎‚†๎‚—๎‚‡๎‚–๎‚๎‚”๎‚˜๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€ƒ๎€”๎€”๎ˆ๎ˆ‘๎‡‚๎€… ๎€‡๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚Š๎‚ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚†๎‚“๎‚™๎‚’๎‚ˆ๎‚—๎†ฝ ๎‚‰๎‚š๎‚‰๎‚’ ๎‚๎‚Š ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚…๎‚’ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚–๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ”๎ˆ” ๎€‹๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚“๎‚– ๎‚๎‚’๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚˜๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
8.4 Systematic check for array over๎‚ฟow
๎€ƒ๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚™๎‚˜๎‚—๎‚๎‚ˆ๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚—๎‚๎‚ž๎‚‰ ๎‚๎‚— ๎‚… ๎‚‡๎‚๎‚…๎‚—๎‚—๎‚๎‚‡ ๎‚‡๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‡€ ๎€ˆ๎‚“๎‚– ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚˜๎‚“
๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚๎†ฝ ๎‚๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ ๎‚›๎‚Œ๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚ˆ๎‚‰๎‚œ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚— ๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‚๎‚ ๎‚”๎‚“๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚“๎‚– ๎‚’๎‚™๎‚๎‚ ๎‚…๎‚’๎‚ˆ
๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‚๎‚ ๎‚๎‚‰๎‚—๎‚— ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚–๎‚…๎‚๎‡€
RULE
87
RULE โ€” Use unsigned integers for array sizes
RULE
88
RULE โ€” Do not access an array element without checking the validity of
the used index
๎€–๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚๎‚ˆ๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚๎‚’๎‚ˆ๎‚‰๎‚œ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚๎†ฟ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚๎‚’๎‚ˆ๎‚‰๎‚œ
๎‚๎‚— ๎‚š๎‚…๎‚๎‚๎‚ˆ ๎‚๎‚Š ๎‚๎‚˜ ๎‚๎‚— ๎‚‹๎‚–๎‚‰๎‚…๎‚˜๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚“๎‚– ๎‚‰๎‚•๎‚™๎‚…๎‚ ๎‚˜๎‚“ ๎‚ž๎‚‰๎‚–๎‚“ ๎‚…๎‚’๎‚ˆ ๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‚๎‚ ๎‚๎‚‰๎‚—๎‚— ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š
๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚๎‡€ ๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‚…๎‚–๎‚–๎‚…๎‚๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‡‹๎‡›๎ˆ๎‡‹ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰
๎‚˜๎‚…๎‚๎‚‰๎‚’ ๎‚๎‚’๎‚˜๎‚“ ๎‚…๎‚‡๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‡€
Bad example
i++;
tab[i] = i; /* no overflow check */
Good example
for (i = 0; i < size_tab; i++){ /* size_tab is the number of elements in the array
*/
tab[i] = i;
...
}
8.4.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€ƒ๎€”๎€”๎ˆ๎ˆ‘๎‡‚๎€… ๎€‡๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚Š๎‚ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚†๎‚“๎‚™๎‚’๎‚ˆ๎‚—๎†ฝ ๎‚‰๎‚š๎‚‰๎‚’ ๎‚๎‚Š ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚…๎‚’ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚–๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€ƒ๎€”๎€”๎ˆ’๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚Š๎‚“๎‚–๎‚‘ ๎‚“๎‚– ๎‚™๎‚—๎‚‰ ๎‚“๎‚™๎‚˜๎‡‚๎‚“๎‚Š๎‡‚๎‚†๎‚“๎‚™๎‚’๎‚ˆ๎‚— ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚– ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚—๎‚™๎‚†๎‚—๎‚‡๎‚–๎‚๎‚”๎‚˜๎‚—๎‡€
68 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€•๎€–๎€”๎ˆ’๎ˆ๎‡‚๎€… ๎€‰๎‚™๎‚…๎‚–๎‚…๎‚’๎‚˜๎‚‰๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚—๎‚˜๎‚“๎‚–๎‚…๎‚‹๎‚‰ ๎‚Š๎‚“๎‚– ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹๎‚— ๎‚Œ๎‚…๎‚— ๎‚—๎‚™๎Šฒ๎‚‡๎‚๎‚‰๎‚’๎‚˜ ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚Š๎‚“๎‚– ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚…๎‚’๎‚ˆ
๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚™๎‚๎‚ ๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚“๎‚–๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€–๎‚…๎‚๎‚’๎‚˜๎‚‰๎‚ˆ๎†ฝ ๎‚”๎‚“๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚๎‚๎‚ ๎‚‘๎‚™๎‚˜๎‚๎‚๎‚…๎‚˜๎‚‰๎‚ˆ๎†ฝ ๎‚“๎‚– ๎‚“๎‚™๎‚˜๎‡‚๎‚“๎‚Š๎‡‚๎‚ˆ๎‚“๎‚‘๎‚…๎‚๎‚’ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚… ๎‚–๎‚‰๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‚‰๎‚ˆ
๎‚—๎‚๎‚’๎‚ ๎‡ฌ๎‚˜๎‚…๎‚๎‚’๎‚˜๎‚—๎‚๎‚’๎‚๎‡ญ๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€ˆ๎‚“๎‚–๎‚‘๎‚๎‚’๎‚‹ ๎‚“๎‚– ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚“๎‚™๎‚˜๎‡‚๎‚“๎‚Š๎‡‚๎‚†๎‚“๎‚™๎‚’๎‚ˆ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚– ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚—๎‚™๎‚†๎‚—๎‚‡๎‚–๎‚๎‚”๎‚˜๎‚— ๎‡ฌ๎‚๎‚’๎‚š๎‚”๎‚˜๎‚–๎‡ญ๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€—๎‚—๎‚๎‚’๎‚‹ ๎‚… ๎‚˜๎‚…๎‚๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚˜๎‚“ ๎‚›๎‚–๎‚๎‚˜๎‚‰ ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚… ๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’๎‚”๎‚™๎‚˜ ๎‚“๎‚– ๎‚“๎‚™๎‚˜๎‚”๎‚™๎‚˜ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’
๎‡ฌ๎‚˜๎‚…๎‚๎‚’๎‚˜๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‡ญ๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€–๎‚…๎‚๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹๎‚— ๎‚…๎‚–๎‚‰ ๎‚”๎‚…๎‚—๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚… ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚”๎‚๎‚๎‚’๎‚‹ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‡ฌ๎‚˜๎‚…๎‚๎‚’๎‚˜๎‚—๎‚˜๎‚–๎‚‡๎‚”๎‚๎‡ญ๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ๎ˆ๎ˆ˜ ๎€‹๎‚‘๎‚”๎‚–๎‚“๎‚”๎‚‰๎‚– ๎€”๎‚‰๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎€‘๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚“๎‚™๎‚’๎‚ˆ๎‚— ๎‚“๎‚Š ๎‚… ๎€๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚†๎‚™๎Šฎ๎‚‰๎‚–๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ๎ˆ‘๎ˆ ๎€„๎‚™๎Šฎ๎‚‰๎‚– ๎€…๎‚“๎‚”๎‚ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎€…๎‚Œ๎‚‰๎‚‡๎‚๎‚๎‚’๎‚‹ ๎€•๎‚๎‚ž๎‚‰๎‚“๎‚Š ๎€‹๎‚’๎‚”๎‚™๎‚˜ ๎‡ฆ๎€…๎‚๎‚…๎‚—๎‚—๎‚๎‚‡ ๎€„๎‚™๎Šฎ๎‚‰๎‚– ๎€‘๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‡ง๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ๎ˆ‘๎ˆ’ ๎€™๎‚–๎‚๎‚˜๎‚‰๎‡‚๎‚›๎‚Œ๎‚…๎‚˜๎‡‚๎‚›๎‚Œ๎‚‰๎‚–๎‚‰ ๎€…๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ๎ˆ‘๎ˆ” ๎€‘๎‚™๎‚˜๎‡‚๎‚“๎‚Š๎‡‚๎‚†๎‚“๎‚™๎‚’๎‚ˆ๎‚— ๎‚–๎‚‰๎‚…๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ๎ˆ‘๎ˆ˜ ๎€‹๎‚‘๎‚”๎‚–๎‚“๎‚”๎‚‰๎‚– ๎€˜๎‚…๎‚๎‚๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎€ƒ๎‚–๎‚–๎‚…๎‚ ๎€‹๎‚’๎‚ˆ๎‚‰๎‚œ๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ๎ˆ–๎ˆ ๎€‹๎‚‘๎‚”๎‚–๎‚“๎‚”๎‚‰๎‚– ๎€๎‚™๎‚๎‚ ๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
8.5 Do not dereference NULL pointers
๎€†๎‚‰๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚๎‚’๎‚‹ ๎‚… NULL ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚๎‚‰๎‚…๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚‘๎‚…๎‚ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚๎‚’ ๎‚…๎‚’ ๎‚…๎‚†๎‚’๎‚“๎‚–๎‚‘๎‚…๎‚ ๎‚˜๎‚‰๎‚–๎‡‚
๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‡€ ๎€–๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰๎†ฝ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚๎‚’๎‚‹ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎†ฝ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚˜ ๎‚๎‚— ๎‚’๎‚“๎‚˜ NULL๎‡€
RULE
89
RULE โ€” A NULL pointer must not be dereferenced
๎€„๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚๎‚’๎‚‹ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚‘๎‚™๎‚—๎‚˜ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚˜ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎€๎€—๎€Ž๎€Ž๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚”๎‚…๎‚—๎‚—๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚… ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚†๎‚‰๎‚๎‚’๎‚‹
๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ๎‡€
void function(const unsigned char *input )
{
size_t size = strlen(input); /* the pointer may be NULL */
...
}
Good example
๎€Š๎‚…๎‚’๎‚ˆ๎‚๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚–๎‚‰๎‚๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎€๎€—๎€Ž๎€Ž ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚Œ๎‚…๎‚— ๎‚†๎‚‰๎‚‰๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚‰๎‚ˆ๎‡€
void function(const unsigned char *input )
{
if (NULL == input)
{
/* handling of the NULL pointer case */
}
else
{
size_t size = strlen(input);
/* ... */
}
}
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 69
8.5.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‡๎€š๎€’๎ˆ’๎ˆ“๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚ˆ๎‚‰๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰ ๎‚’๎‚™๎‚๎‚ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚—๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€†๎‚‰๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚๎‚’๎‚‹ ๎‚…๎‚’ ๎‚“๎‚™๎‚˜๎‡‚๎‚“๎‚Š๎‡‚๎‚ˆ๎‚“๎‚‘๎‚…๎‚๎‚’ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‡ฌ๎‚’๎‚™๎‚๎‚๎‚–๎‚‰๎‚Š๎‡ญ๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ“๎ˆ–๎ˆ• ๎€๎€—๎€Ž๎€Ž ๎€’๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎€†๎‚‰๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰๎‡€
๎‡ฌ๎€ƒ๎‚’๎‚—๎‚๎€…๎ˆ˜๎ˆ˜๎‡ญ ๎€•๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ•๎‡€๎ˆ”๎‡€๎ˆ’๎‡€๎ˆ‘๎‡€
๎‡ฌ๎€ƒ๎‚’๎‚—๎‚๎€…๎ˆ˜๎ˆ๎‡ญ ๎€•๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ•๎‡€๎ˆ’๎‡€๎ˆ’๎‡€๎ˆ’๎‡€
8.6 Assignment to NULL of deallocated pointers
๎€ˆ๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚†๎‚ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚—๎‚˜๎‚๎‚๎‚ ๎‚—๎‚˜๎‚“๎‚–๎‚‰๎‚—
๎‚๎‚˜๎‚— ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚—๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚๎‚— ๎‚๎‚’๎‚“๎‚›๎‚’ ๎‚…๎‚— ๎‚… dangling pointer๎‡€
Dangling pointer
๎€ƒ dangling pointer ๎‚๎‚— ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚…๎‚’ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚˜๎‚Œ๎‚…๎‚˜
๎‚Œ๎‚…๎‚— ๎‚†๎‚‰๎‚‰๎‚’ ๎‚Š๎‚–๎‚‰๎‚‰๎‚ˆ๎‡€
๎€‹๎‚’ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚†๎‚™๎‚‹๎‚— ๎‚…๎‚’๎‚ˆ ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚‘๎‚…๎‚ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚™๎‚”๎‚˜๎‚‰๎‚ˆ๎‡€ ๎€‘๎‚’๎‚‡๎‚‰
๎‚Š๎‚–๎‚‰๎‚‰๎‚ˆ๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚‘๎‚…๎‚ ๎‡ฆ๎‚“๎‚– ๎‚‘๎‚…๎‚ ๎‚’๎‚“๎‚˜๎‡ง ๎‚†๎‚‰ ๎‚–๎‚‰๎‚™๎‚—๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚
๎‚…๎‚–๎‚‰๎‚… ๎‡ฆ๎‚š๎‚๎‚… ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡ง ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚๎‚๎‚ ๎‚š๎‚๎‚—๎‚๎‚†๎‚๎‚‰๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚…๎‚ ๎‚‡๎‚…๎‚™๎‚—๎‚‰ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚ ๎‚”๎‚–๎‚“๎‚†๎‡‚
๎‚๎‚‰๎‚‘๎‚— ๎‡ฆuse-a๎Šนer-free๎‡ง๎‡€ ๎€„๎‚ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚“ NULL ๎‚…๎Šน๎‚‰๎‚– ๎‚ˆ๎‚‰๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚๎‚“๎‚™ ๎‚‡๎‚…๎‚’ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚Š๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰
๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚’๎‚“ ๎‚๎‚“๎‚’๎‚‹๎‚‰๎‚– ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚— ๎‚˜๎‚“ ๎‚… ๎‚š๎‚…๎‚๎‚๎‚ˆ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚–๎‚‰๎‚…๎‡€ ๎€ƒ๎‚’๎‚ˆ ๎‚๎‚’ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚…๎‚‡๎‚‡๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚…๎‚ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎†ฝ
๎‚’๎‚“ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚–๎‚‰๎‚… ๎‚›๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚™๎‚”๎‚˜๎‚‰๎‚ˆ ๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚’๎‚“ ๎‚๎‚“๎‚’๎‚‹๎‚‰๎‚– ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚— ๎‚˜๎‚“ ๎‚…๎‚’๎‚ ๎‚š๎‚…๎‚๎‚๎‚ˆ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚–๎‚‰๎‚…๎‡€
RULE
90
RULE โ€” A pointer must be assigned to NULL after deallocation
๎€ƒ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚˜๎‚“ NULL ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚๎‚˜ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚— ๎‚˜๎‚“๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚†๎‚‰๎‚๎‚“๎‚›๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚—๎‚‰๎‚˜ ๎‚˜๎‚“ ๎€๎€—๎€Ž๎€Ž ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚๎‚˜๎‚— ๎‚ˆ๎‚‰๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
list_t *p_list = NULL;
p_list = create_list();
...
if(p_list != NULL ) {
free_list (p_list);
}
/* setting of p_list to NULL is missing */
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚๎‚— ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚—๎‚‰๎‚˜ ๎‚˜๎‚“ NULL ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‡‚
๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚‰๎‚… ๎‚†๎‚‰๎‚๎‚’๎‚‹ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“๎‡€
list_t *p_list = NULL;
p_list = create_list();
...
if(p_list != NULL ) {
70 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
free_list (p_list);
p_list = NULL;
}
8.6.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€๎€‡๎€๎ˆ’๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚Š๎‚–๎‚‰๎‚‰๎‚ˆ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡ ๎€๎€‡๎€๎ˆ๎ˆ๎‡‚๎€… ๎€•๎‚˜๎‚“๎‚–๎‚‰ ๎‚… ๎‚’๎‚‰๎‚› ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚๎‚’ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚๎‚‘๎‚‘๎‚‰๎‚ˆ๎‚๎‚…๎‚˜๎‚‰๎‚๎‚ ๎‚…๎Šน๎‚‰๎‚– ๎‚Š๎‚–๎‚‰๎‚‰๎‡ฆ๎‡ง๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ—๎‡€๎ˆ•๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚…๎‚’ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚—๎‚˜๎‚“๎‚–๎‚…๎‚‹๎‚‰ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚”๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“
๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚”๎‚‰๎‚–๎‚—๎‚๎‚—๎‚˜๎‚— ๎‚…๎Šน๎‚‰๎‚– ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚–๎‚—๎‚˜ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚Œ๎‚…๎‚— ๎‚‡๎‚‰๎‚…๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚‰๎‚œ๎‚๎‚—๎‚˜๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ“๎ˆ๎ˆ”๎‡€ ๎€†๎‚“๎‚™๎‚†๎‚๎‚‰ ๎‚Š๎‚–๎‚‰๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ“๎ˆ๎ˆ•๎‡€ ๎€—๎‚—๎‚‰ ๎‚…๎Šน๎‚‰๎‚– ๎‚Š๎‚–๎‚‰๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ–๎ˆ‘ ๎€‘๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚’ ๎‚… ๎‚–๎‚‰๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚…๎Šน๎‚‰๎‚– ๎‚‰๎‚œ๎‚”๎‚๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚– ๎‚–๎‚‰๎‚๎‚‰๎‚…๎‚—๎‚‰๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€ƒ๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚Š๎‚–๎‚‰๎‚‰๎‚ˆ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‡ฌ๎‚…๎‚‡๎‚‡๎‚Š๎‚–๎‚‰๎‚‰๎‡ญ๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€ˆ๎‚–๎‚‰๎‚‰๎‚๎‚’๎‚‹ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚˜๎‚๎‚‘๎‚‰๎‚— ๎‡ฌ๎‚ˆ๎‚†๎‚Š๎‚–๎‚‰๎‚‰๎‡ญ๎‡€
8.7 Use of the restrict type quali๎‚พer
๎€–๎‚Œ๎‚‰
restrict
๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚–๎†ฝ ๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚๎‚’ ๎€…๎ˆ˜๎ˆ˜๎†ฝ ๎‚๎‚— ๎‚… ๎‚‘๎‚‰๎‚…๎‚’๎‚— ๎‚“๎‚Š ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚‰๎‚…
๎‚†๎‚‰๎‚๎‚’๎‚‹ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚‡๎‚…๎‚’๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚”๎‚…๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚š๎‚๎‚… ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚‘๎‚…๎‚–๎‚๎‚‰๎‚ˆ restrict๎‡€ ๎€ƒ restrict๎‡‚
๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‰๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚๎‚˜ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚— ๎‚˜๎‚“ ๎‚๎‚— ๎‚–๎‚‰๎‚…๎‚‡๎‚Œ๎‚‰๎‚ˆ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚“๎‚– ๎‚๎‚’๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚“๎‚’๎‚๎‚
๎‚š๎‚๎‚… ๎‚˜๎‚Œ๎‚๎‚— ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚–๎‚‰๎‚•๎‚™๎‚๎‚–๎‚‰๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚…๎‚–๎‚‰ ๎‚’๎‚“ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚…๎‚๎‚๎‚…๎‚—๎‚‰๎‚— ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚๎‚“๎‚™ ๎‚…๎‚–๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚“๎‡€
Alias
๎€–๎‚›๎‚“ ๎‚…๎‚๎‚๎‚…๎‚—๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚˜๎‚›๎‚“ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚“๎‚– ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚”๎‚…๎‚˜๎‚Œ๎‚— ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚–๎‚‰๎‚…๎‡€
Warning
๎€–๎‚Œ๎‚‰ restrict ๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚– ๎‚๎‚— ๎‚… ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–๎‡‹๎‚— ๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚“ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰ ๎‚…
๎‚—๎‚๎‚’๎‚‹๎‚๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚–๎‚‰๎‚…๎†ฝ ๎‚’๎‚“๎‚˜ ๎‚…๎‚’ ๎‚…๎‚‡๎‚˜๎‚™๎‚…๎‚ ๎‚Š๎‚…๎‚‡๎‚˜๎‡€ ๎€‹๎‚’ ๎‚”๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚‡๎‚‰๎†ฝ ๎‚’๎‚“๎‚˜๎‚Œ๎‚๎‚’๎‚‹ ๎‚”๎‚–๎‚‰๎‚š๎‚‰๎‚’๎‚˜๎‚—
๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚–๎‚‰๎‚…๎‚‡๎‚Œ๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚…๎‚–๎‚‰๎‚… ๎‚š๎‚๎‚… ๎‚… ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡€
๎€–๎‚Œ๎‚‰ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚†๎‚‰๎‚‡๎‚“๎‚‘๎‚‰๎‚— ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚๎‚Š ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚— ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚†๎‚ restrict ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚‡๎‚“๎‚‘๎‚‘๎‚“๎‚’ ๎‚‘๎‚‰๎‚‘๎‡‚
๎‚“๎‚–๎‚ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚—๎‚‰๎‚—๎‡€ ๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚˜๎‚“ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚‘๎‚‘๎‚“๎‚’ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚—๎‚‰๎‚—
๎‚“๎‚’ ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚๎‚ ๎‚›๎‚๎‚˜๎‚Œ restrict ๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚—๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚…๎‚๎‚—๎‚“ ๎‚ˆ๎‚™๎‚–๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚—๎‚…๎‚๎‚ˆ ๎‚Š๎‚™๎‚’๎‚‡๎‡‚
๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
Warning
๎€•๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚–๎‚‰๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜ ๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎€…๎ˆ˜๎ˆ˜
๎‡ฆmemcpy๎†ฝ strcat๎†ฝ strcpy๎†ฝ etc.๎‡ง๎‡€
๎€‹๎‚˜ ๎‚๎‚— ๎‚š๎‚‰๎‚–๎‚ ๎‚‰๎‚…๎‚—๎‚ ๎‚˜๎‚“ ๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚˜๎‚Œ๎‚–๎‚“๎‚™๎‚‹๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š restrict๎†ฝ ๎‚…๎‚— ๎‚๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰๎‚ˆ
๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚’๎‚“๎‚’๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚‡๎‚“๎‚’๎‚‡๎‚‰๎‚–๎‚’๎‚‰๎‚ˆ ๎‚—๎‚Œ๎‚…๎‚–๎‚‰ ๎‚… ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚–๎‚‰๎‚…๎†ฝ ๎‚›๎‚Œ๎‚๎‚๎‚‰ ๎‚˜๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚๎‚’๎‚˜๎‚“ ๎‚…๎‚‡๎‚‡๎‚“๎‚™๎‚’๎‚˜ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚๎‚๎‚—
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 71
๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚…๎‚๎‚—๎‚“ ๎‚Œ๎‚…๎‚š๎‚‰ restrict ๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎€…๎ˆ˜๎ˆ˜๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
restrict ๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚– ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰๎‚– ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ๎‡€
RULE
91
RULE โ€” Do not use the restrict pointer quali๎‚พer
๎€–๎‚Œ๎‚‰ restrict ๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚– ๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–๎‡€ ๎€‘๎‚’๎‚๎‚ ๎‚๎‚’๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜ ๎‚™๎‚—๎‚‰๎†ฝ
i.e. ๎‚š๎‚๎‚… ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚๎‚๎†ฝ ๎‚๎‚— ๎‚˜๎‚“๎‚๎‚‰๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚‘๎‚™๎‚—๎‚˜ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰
๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚’๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚›๎‚๎‚๎‚ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚—๎‚™๎‚‡๎‚Œ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
Information
๎€–๎‚Œ๎‚‰ -Wrestrict ๎„Œ๎„ˆ๎„ˆ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚๎‚˜ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚…๎‚๎‚‰๎‚–๎‚˜ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚๎‚—๎‚™๎‚—๎‚‰ ๎‚“๎‚Š restrict
๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚—๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ restrict ๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚—๎‚Œ๎‚…๎‚–๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚–๎‚‰๎‚…๎‚— ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰
๎‚‡๎‚…๎‚™๎‚—๎‚‰ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€
uint16_t * restrict ptdeb ;
uint16_t * restrict ptfin ;
uint16_t tab [12];
unsigned char * pt1;
unsigned char * pt2;
unsigned char c_str [] = "blabla ";
...
ptdeb = &tab[0];
ptfin = &tab[11];
ptdeb = ptfin; /* undefined behaviour */
...
pt1 = pt2 + 2;
memcpy (pt2 , pt1 , 3); /* undefined behaviour - restrict type memcpy parameters */
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ restrict ๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚–๎‚— ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚†๎‚‰๎‚‰๎‚’ ๎‚ˆ๎‚‰๎‚๎‚‰๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚— ๎‚’๎‚“
๎‚๎‚“๎‚’๎‚‹๎‚‰๎‚– ๎‚…๎‚’๎‚ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€
uint16_t * ptdeb; /* deletion of restrict qualifier */
uint16_t * ptfin; /* deletion of restrict qualifier */
uint16_t tab [12];
unsigned char * pt1;
unsigned char * pt2;
unsigned char c_str [] = "blabla ";
...
ptdeb = &tab[0];
ptfin = &tab[11];
ptdeb = ptfin; /* ok */
...
pt1 = pt2 + 2;
memmove (pt2 , pt1 , 3); /* change of function */
8.7.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ—๎‡€๎ˆ๎ˆ“ ๎€–๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜ ๎‚˜๎‚๎‚”๎‚‰ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€’๎‚…๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚๎‚’๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚— ๎‚…๎‚— ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚˜๎‚“ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚–๎‚‰๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‡‚๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‡‚
๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‡ฌ๎‚–๎‚‰๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‡ญ๎‡€
72 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‡๎€š๎€’๎ˆ“๎ˆ’๎‡‚๎€… ๎€ƒ๎‚š๎‚“๎‚๎‚ˆ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚– ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚–๎‚‰๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‡‚๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚—๎‡€
8.8 Limit on the number of pointer indirections
๎€™๎‚Œ๎‚‰๎‚’ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚Œ๎‚…๎‚— ๎‚‘๎‚“๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚˜๎‚›๎‚“ ๎‚๎‚‰๎‚š๎‚‰๎‚๎‚— ๎‚“๎‚Š ๎‚๎‚’๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‡ฆ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฟ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚“๎‚Š ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚“๎‚Š ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–
int32_t ***pppInt32๎‡ง๎†ฝ ๎‚๎‚˜ ๎‚†๎‚‰๎‚‡๎‚“๎‚‘๎‚‰๎‚— ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–๎‡‹๎‚— ๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚‰๎‡‚
๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€
RECO
92
RECOMMENDATION โ€” The number of levels of pointer indirection should
be limited to two
๎€–๎‚Œ๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚๎‚‰๎‚š๎‚‰๎‚๎‚— ๎‚“๎‚Š ๎‚๎‚’๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚“๎‚– ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚‰๎‚œ๎‚‡๎‚‰๎‚‰๎‚ˆ ๎‚˜๎‚›๎‚“๎‡€
Bad example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚Œ๎‚“๎‚›๎‚— ๎‚‰๎‚œ๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚š๎‚‰ ๎‚๎‚‰๎‚š๎‚‰๎‚๎‚— ๎‚“๎‚Š ๎‚๎‚’๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
void function(int8_t *** arr_pt) /* 3 levels */
{
int8_t *** pt;
...
}
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚‰๎‚‘๎‚”๎‚“๎‚–๎‚…๎‚–๎‚ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚Š๎‚…๎‚‡๎‚๎‚๎‚๎‚˜๎‚…๎‚˜๎‚‰ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚˜๎‚“
๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚…๎‚’๎‚ˆ ๎‚๎‚๎‚‘๎‚๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚’๎‚‰๎‚—๎‚˜๎‚๎‚’๎‚‹ ๎‚๎‚‰๎‚š๎‚‰๎‚๎‚—๎‡€
typedef int8_t * int8ptr_t;
void function(int8ptr_t ** arr_pt) /* reduction to two levels */
{
int8_t *pt_temp; /* temporary pointer */
int8ptr_t ** pt;
...
}
8.8.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ—๎‡€๎ˆ” ๎€†๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚’๎‚“ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚˜๎‚›๎‚“ ๎‚๎‚‰๎‚š๎‚‰๎‚๎‚— ๎‚“๎‚Š ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚’๎‚‰๎‚—๎‚˜๎‚๎‚’๎‚‹๎‡€
8.9 Give preference to the use of the indirection operator
->
๎€–๎‚›๎‚“ ๎‚›๎‚–๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚‘๎‚‰๎‚˜๎‚Œ๎‚“๎‚ˆ๎‚— ๎‚…๎‚–๎‚‰ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚˜๎‚“ ๎‚–๎‚‰๎‚…๎‚‡๎‚Œ ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎Šฑ๎‚‰๎‚๎‚ˆ ๎‚š๎‚๎‚… ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎†ฟ ๎‚˜๎‚Œ๎‚‰
๎‚๎‚’๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ptr->field ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚‰๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚๎‚’๎‚‹ (*ptr).field๎‡€ ๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚‡๎‚“๎‚’๎‚ˆ ๎‚‘๎‚‰๎‚˜๎‚Œ๎‚“๎‚ˆ
๎‚๎‚— ๎‚“๎Šน๎‚‰๎‚’ ๎‚… ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚”๎‚–๎‚‰๎‚Œ๎‚‰๎‚’๎‚—๎‚๎‚“๎‚’ ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘๎‚—๎‡€ ๎€‹๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚†๎‚‰๎‚—๎‚˜ ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚ˆ๎‚‰๎‚–๎‚‰๎‚Š๎‡‚
๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚๎‚’๎‚‹ (*ptr).field ๎‚˜๎‚“ ๎‚–๎‚‰๎‚…๎‚‡๎‚Œ ๎‚… ๎Šฑ๎‚‰๎‚๎‚ˆ ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚š๎‚๎‚… ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 73
RECO
93
RECOMMENDATION โ€” Give preference to the use of the indirection oper-
ator ->
๎€–๎‚Œ๎‚‰ ๎‚๎‚’๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– -> ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚–๎‚‰๎‚…๎‚‡๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚‰๎‚๎‚ˆ๎‚— ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚š๎‚๎‚… ๎‚…
๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚›๎‚–๎‚๎‚˜๎‚˜๎‚‰๎‚’ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‡‚
๎‚˜๎‚“๎‚–๎‡€
(*list.p_head).pNext = NULL;
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚๎‚— ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
list .p_head ->pNext = NULL;
8.10 Pointer arithmetic
๎€–๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚—๎‡€ ๎€ƒ๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰
๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚‰๎‚๎‚˜๎‚Œ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚– ๎‚ˆ๎‚‰๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚˜๎‡€
Pointer arithmetic
๎€’๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚…๎‚— ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚๎‚’ ๎‚…๎‚’ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚–๎‚
๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡ฆ๎‚—๎‚™๎‚†๎‚˜๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‡ง๎‡€
๎€’๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚๎‚— ๎‚š๎‚‰๎‚–๎‚ ๎‚“๎Šน๎‚‰๎‚’ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚’๎‚…๎‚š๎‚๎‚‹๎‚…๎‚˜๎‚‰ ๎‚†๎‚‰๎‡‚
๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚๎‡€ ๎€ƒ๎‚”๎‚…๎‚–๎‚˜ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‡๎‚…๎‚—๎‚‰๎†ฝ ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚“๎‚’ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚—๎‚‰๎‚—
๎‚๎‚— ๎‚š๎‚‰๎‚–๎‚ ๎‚–๎‚๎‚—๎‚๎‚๎‡€
RULE
94
RULE โ€” Only incrementing or decrementing array pointers is authorised
๎€‹๎‚’๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚๎‚’๎‚‹ ๎‚“๎‚– ๎‚ˆ๎‚‰๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚๎‚’๎‚‹ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚“๎‚’๎‚๎‚ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‡‚
๎‚๎‚’๎‚‹ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚“๎‚– ๎‚…๎‚’ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚๎‡€
๎€ƒ๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚“๎‚’ void* ๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ๎‡€ ๎€๎‚“ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚—๎‚๎‚ž๎‚‰ ๎‚๎‚— ๎‚๎‚’ ๎‚Š๎‚…๎‚‡๎‚˜ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ
๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ void* ๎‚˜๎‚๎‚”๎‚‰๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚‡๎‚…๎‚™๎‚—๎‚‰๎‚— ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎†ฝ ๎‚๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚๎‚“๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚š๎‚๎‚“๎‚™๎‚—
๎‚–๎‚™๎‚๎‚‰๎‡€
RULE
95
RULE โ€” No arithmetic on void* pointers is authorised
๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’๎‚ ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚“๎‚’ void* ๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ๎‡€
๎€‡๎‚š๎‚‰๎‚’ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚“๎‚’ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚๎‚’ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚๎†ฝ ๎‚… ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚…๎‚ ๎‚‡๎‚…๎‚–๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚˜๎‚…๎‚๎‚‰๎‚’ ๎‚˜๎‚“
๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚›๎‚๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚…๎‚™๎‚—๎‚‰ ๎‚ˆ๎‚‰๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚๎‚’๎‚‹ ๎‚“๎‚™๎‚˜๎‚—๎‚๎‚ˆ๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚๎‡€
74 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
RECO
96
RECOMMENDATION โ€” Controlled pointer arithmetic on arrays
๎€ƒ๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚“๎‚’ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚’๎‚‹ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚“๎‚– ๎‚…๎‚’ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰
๎‚‡๎‚…๎‚–๎‚–๎‚๎‚‰๎‚ˆ ๎‚“๎‚™๎‚˜ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚›๎‚๎‚๎‚ ๎‚—๎‚˜๎‚๎‚๎‚ ๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎‚—๎‚…๎‚‘๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚๎‡€
๎€ƒ๎‚— ๎‚… ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎†ฝ ๎‚—๎‚™๎‚†๎‚˜๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚– ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’๎‚— ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚›๎‚๎‚๎‚ ๎‚“๎‚’๎‚๎‚ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚๎‚‰๎‚š๎‚…๎‚’๎‚˜ ๎‚Š๎‚“๎‚– ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰
๎‚—๎‚…๎‚‘๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚๎‡€
RULE
97
RULE โ€” Subtraction and comparison between pointers in the same array
only
๎€‘๎‚’๎‚๎‚ ๎‚—๎‚™๎‚†๎‚˜๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚…๎‚–๎‚‰ ๎‚…๎‚™๎‚˜๎‚Œ๎‚“๎‚–๎‚๎‚—๎‚‰๎‚ˆ๎‡€
๎€ˆ๎‚๎‚’๎‚…๎‚๎‚๎‚๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚… ๎Šฑ๎‚œ๎‚‰๎‚ˆ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚˜๎‚“ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚๎‚— ๎‚—๎‚˜๎‚–๎‚“๎‚’๎‚‹๎‚๎‚ ๎‚ˆ๎‚๎‚—๎‚‡๎‚“๎‚™๎‚–๎‚…๎‚‹๎‚‰๎‚ˆ๎‡€
RECO
98
RECOMMENDATION โ€” A ๎‚พxed address should not be assigned directly to
a pointer
Bad example
#include <stddef .h>
#include <stdint .h>
void function(int8_t * ptr_param )
{
int8_t tab1 [10];
int8_t tab2 [100];
int8_t *pt1 =& tab1 [0];
int8_t *pt2 =& tab2 [0];
ptr_param ++; /* it is not known whether ptr_param points to an array ... */
pt1++; /* pt1 points to the next element of tab1 */
ptr_param = pt1 + pt2; /* illegal memory access */
if (pt2 >= 15) /* not relevant */
{
/* ... */
}
uint8_t nb_elem = pt2 - pt1; /* the two pointers are not on the same array and
the type is not adapted */
...
}
Good example
#include <stddef .h>
#include <stdint .h>
void function(int8_t * ptr_param )
{
int8_t tab1 [10];
int8_t tab2 [100];
int8_t *pt1 = &tab1 [0];
int8_t *pt2 = &tab2 [0];
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 75
pt1++; /* pt1 points to the next element of tab1 */
pt2 = pt2 + 3; /* pt2 points to tab2 [3] */
pt1 = pt1 + 8; /* pt1 points to the last elements of tab1 */
if (pt1 >= tab1 ) /* same array ok */
{
/* ... */
}
ptrdiff_t nb_elem = pt2 - tab2 ; /* both pointers are on the same array and
dedicated type used (taken from stddef .h) */
...
}
8.10.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ—๎‡€๎ˆ ๎€ƒ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚“๎‚’ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚…๎‚’
๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ—๎‡€๎ˆ‘ ๎€•๎‚™๎‚†๎‚—๎‚˜๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚“๎‚’๎‚๎‚ ๎‚†๎‚‰ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚—
๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ—๎‡€๎ˆ’ ๎€–๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚— ๎‚“๎‚Š ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰ ๎‚‰๎‚œ๎‡‚
๎‚‡๎‚‰๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚›๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚๎‚’๎‚˜๎‚“ ๎‚… ๎‚—๎‚…๎‚‘๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ—๎‡€๎ˆ“ ๎€–๎‚Œ๎‚‰ ๎ŠŠ๎†ฝ ๎‡‚๎†ฝ ๎ŠŠ๎Š ๎‚…๎‚’๎‚ˆ ๎‡‚๎Š ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–
๎‚˜๎‚๎‚”๎‚‰๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€ƒ๎€”๎€”๎ˆ’๎ˆ•๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚—๎‚™๎‚†๎‚—๎‚˜๎‚–๎‚…๎‚‡๎‚˜ ๎‚“๎‚– ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚‰ ๎‚˜๎‚›๎‚“ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚Š๎‚‰๎‚– ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€ƒ๎€”๎€”๎ˆ’๎ˆ–๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚…๎‚ˆ๎‚ˆ ๎‚“๎‚– ๎‚—๎‚™๎‚†๎‚˜๎‚–๎‚…๎‚‡๎‚˜ ๎‚…๎‚’ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚˜๎‚“ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚“ ๎‚… ๎‚’๎‚“๎‚’๎‡‚๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€ƒ๎€”๎€”๎ˆ’๎ˆ˜๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚…๎‚ˆ๎‚ˆ ๎‚“๎‚– ๎‚—๎‚™๎‚†๎‚—๎‚˜๎‚–๎‚…๎‚‡๎‚˜ ๎‚… ๎‚—๎‚‡๎‚…๎‚๎‚‰๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚˜๎‚“ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‡๎€š๎€’๎ˆ๎ˆ—๎‡‚๎€… ๎€‡๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€•๎‚™๎‚†๎‚˜๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚’๎‚‹ ๎‚“๎‚– ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚’๎‚‹ ๎‚˜๎‚›๎‚“ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚Š๎‚‰๎‚– ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‡ฌ๎‚”๎‚˜๎‚–๎‚“๎‚†๎‚Ž๎‡ญ๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€ˆ๎‚“๎‚–๎‚‘๎‚๎‚’๎‚‹ ๎‚“๎‚– ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚“๎‚™๎‚˜๎‡‚๎‚“๎‚Š๎‡‚๎‚†๎‚“๎‚™๎‚’๎‚ˆ๎‚— ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚– ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚—๎‚™๎‚†๎‚—๎‚‡๎‚–๎‚๎‚”๎‚˜๎‚— ๎‡ฌ๎‚๎‚’๎‚š๎‚”๎‚˜๎‚–๎‡ญ๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ“๎ˆ•๎ˆ˜ ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚—๎‚™๎‚†๎‚—๎‚˜๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚‰ ๎‚—๎‚๎‚ž๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ“๎ˆ•๎ˆ— ๎€‹๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚—๎‚‡๎‚…๎‚๎‚๎‚’๎‚‹๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ“๎ˆ•๎ˆ• ๎€”๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚“๎‚Š ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚™๎‚˜๎‚—๎‚๎‚ˆ๎‚‰ ๎‚“๎‚Š ๎‚‰๎‚œ๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚–๎‚…๎‚’๎‚‹๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ”๎ˆ—๎ˆ– ๎€ƒ๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚… ๎Šฑ๎‚œ๎‚‰๎‚ˆ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚˜๎‚“ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡€
๎‡ฌ๎€ƒ๎‚’๎‚—๎‚๎€…๎ˆ˜๎ˆ˜๎‡ญ ๎€•๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ•๎‡€๎ˆ‘๎‡€๎ˆ”๎†ฝ ๎ˆ•๎‡€๎ˆ’๎‡€๎ˆ‘๎‡€๎ˆ’๎†ฝ ๎ˆ•๎‡€๎ˆ”๎‡€๎ˆ‘๎‡€๎ˆ๎†ฝ ๎ˆ•๎‡€๎ˆ”๎‡€๎ˆ•๎‡€
๎‡ฌ๎€ƒ๎‚’๎‚—๎‚๎€…๎ˆ˜๎ˆ๎‡ญ ๎€•๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ•๎‡€๎ˆ๎‡€๎ˆ‘๎‡€๎ˆ”๎†ฝ ๎ˆ•๎‡€๎ˆ‘๎‡€๎ˆ‘๎‡€๎ˆ’๎†ฝ ๎ˆ•๎‡€๎ˆ’๎‡€๎ˆ•๎†ฝ ๎ˆ•๎‡€๎ˆ’๎‡€๎ˆ—๎‡€
76 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
9
Structures and unions
9.1 Declaration of structures
๎€‹๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚‘๎‚“๎‚ˆ๎‚‰๎‚ ๎‚…๎‚’ ๎‚‰๎‚’๎‚˜๎‚๎‚˜๎‚ ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚… ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚“๎Šน๎‚‰๎‚’ ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚˜๎‚“ ๎‚›๎‚…๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚
๎‚—๎‚‡๎‚…๎‚๎‚…๎‚– ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚๎‚˜๎‚‰๎‚‘๎‚— ๎‡ฆ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚–๎‚—๎†ฝ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‚—๎†ฝ etc.๎‡ง๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚๎‚’๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚˜๎‚“ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜
๎‚˜๎‚Œ๎‚๎‚— ๎‚‰๎‚’๎‚˜๎‚๎‚˜๎‚ ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚๎‚˜ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚‰๎‚ˆ๎‚๎‚“๎‚™๎‚— ๎‚˜๎‚“ ๎‚”๎‚…๎‚—๎‚— ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚˜๎‚“ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
๎€ƒ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚‹๎‚–๎‚“๎‚™๎‚” ๎‚˜๎‚“๎‚‹๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚‰๎‚’๎‚˜๎‚๎‚˜๎‚๎‡€ ๎€ƒ๎‚’๎‚ˆ ๎‚…๎‚— ๎‚‘๎‚…๎‚’๎‚ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‡‚
๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚…๎‚–๎‚‰ ๎‚‰๎‚’๎‚˜๎‚๎‚˜๎‚๎‚‰๎‚— ๎‚˜๎‚“ ๎‚‘๎‚“๎‚ˆ๎‚‰๎‚๎‡€ ๎€›๎‚“๎‚™ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚… ๎‚—๎‚๎‚’๎‚‹๎‚๎‚‰ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚…๎‚’๎‚ˆ
๎‚‹๎‚–๎‚“๎‚™๎‚” ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚–๎‚‰๎‚๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚‰๎‚’๎‚˜๎‚๎‚˜๎‚๎‚‰๎‚— ๎‚๎‚’ ๎‚๎‚˜๎‡€
RULE
99
RULE โ€” A structure must be used to group data representing the same
entity
๎€Ž๎‚๎‚’๎‚๎‚‰๎‚ˆ ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‹๎‚–๎‚“๎‚™๎‚”๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‡€
Information
๎€–๎‚Œ๎‚๎‚— ๎‚–๎‚™๎‚๎‚‰ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚๎‚๎‚’๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚๎‚‘๎‚‘๎‚‰๎‚ˆ๎‚๎‚…๎‚˜๎‚‰ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚ ๎‚–๎‚๎‚—๎‚๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚๎‚— ๎‚… ๎‚‡๎‚“๎‚‘๎‚‘๎‚“๎‚’๎‡‚๎‚—๎‚‰๎‚’๎‚—๎‚‰ ๎‚–๎‚™๎‚๎‚‰ ๎‚˜๎‚“
๎‚†๎‚‰ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚๎‚ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚…๎‚‡๎‚ ๎‚“๎‚Š ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎‚— ๎‚๎‚’ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚–๎‚“๎‚˜๎‚“๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚‰
๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‡€
void rectangle (float x0, float y0, float x1 , float y1 , float x2 ,
float y2 , float x3 , float y3);
void pyramide(float* coords); /* coords is an array */
Good example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚™๎‚—๎‚‰๎‚— ๎‚๎‚’๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚˜๎‚“ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜
๎‚‹๎‚‰๎‚“๎‚‘๎‚‰๎‚˜๎‚–๎‚๎‚‡ ๎‚—๎‚Œ๎‚…๎‚”๎‚‰๎‚—๎‡€
typedef struct point_s {
float x;
float y;
} Point_t;
typedef struct rectangle_s {
point_t xy0;
point_t xy1;
point_t xy2;
point_t xy3;
} rectangle_t;
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 77
typedef struct pyramid_s {
rectangle_t base;
point_t top;
} pyramid_t;
void rectangle (rectangle_t* rect);
void pyramid( pyramid_t* pyra);
9.2 Size of a structure
๎€–๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚—๎‚—๎‚™๎‚‘๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚‰๎‚•๎‚™๎‚…๎‚ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚™๎‚‘ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚๎‚˜๎‚— ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎‡€
๎€–๎‚Œ๎‚๎‚— ๎‚๎‚— ๎‚ˆ๎‚™๎‚‰ ๎‚˜๎‚“ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚”๎‚…๎‚ˆ๎‚ˆ๎‚๎‚’๎‚‹๎‡€ ๎€‹๎‚˜ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚… ๎‚–๎‚‰๎‚…๎‚–๎‚–๎‚…๎‚’๎‚‹๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚‰๎‚๎‚ˆ๎‚— ๎‚๎‚’ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚๎‚’
๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚”๎‚–๎‚“๎‚”๎‚‰๎‚–๎‚๎‚ ๎‚…๎‚๎‚๎‚‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‡ฆ๎‚๎‚˜ ๎‚๎‚— ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚–๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚— ๎‚”๎‚…๎‚ˆ๎‚ˆ๎‚๎‚’๎‚‹ ๎Šฑ๎‚‰๎‚๎‚ˆ๎‚—๎‡ง๎‡€ ๎€ˆ๎‚“๎‚– ๎‚˜๎‚Œ๎‚๎‚— ๎‚–๎‚‰๎‚…๎‚—๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰
๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚…๎‚๎‚‡๎‚™๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚’๎‚‹ ๎‚™๎‚” ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚๎‚˜๎‚— ๎Šฑ๎‚‰๎‚๎‚ˆ๎‚—๎†ฝ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚๎‚— ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚˜๎‚…๎‚๎‚‰ ๎‚๎‚’๎‚˜๎‚“
๎‚…๎‚‡๎‚‡๎‚“๎‚™๎‚’๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚ˆ๎‚ˆ๎‚๎‚’๎‚‹ ๎Šฑ๎‚‰๎‚๎‚ˆ๎‚—๎‡€
RULE
100
RULE โ€” Do not calculate the size of a structure as the sum of the size of
its ๎‚พelds
๎€„๎‚‰๎‚‡๎‚…๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚ˆ๎‚ˆ๎‚๎‚’๎‚‹๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚—๎‚—๎‚™๎‚‘๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚™๎‚‘
๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚๎‚˜๎‚— ๎Šฑ๎‚‰๎‚๎‚ˆ๎‚—๎‡€
Bad example
#define SIZE_TABL 100
...
typedef struct {
int tabl[ SIZE_TABL ];
size_t size;
} my_struct;
...
size_t sizestruct = sizeof (my_struct. tabl)+sizeof ( my_struct.size);
/* assumes that the size of the structure is the sum of the size of the elements */
...
Good example
#define SIZE_TABL 100
...
typedef struct {
int tabl[ SIZE_TABL ];
size_t size;
} my_struct;
...
size_t sizestruct = sizeof ( my_struct); /* good size */
...
Warning
๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚’๎‚“๎‚’๎‡‚๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚…๎‚˜๎‚˜๎‚–๎‚๎‚†๎‚™๎‚˜๎‚‰๎‚— ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— packed ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚‡๎‚“๎‚’๎‚—๎‚๎‚ˆ๎‚‰๎‚–๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰๎‡€
78 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
9.2.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€‡๎€š๎€’๎ˆ“๎ˆ‘๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚‰ ๎‚”๎‚…๎‚ˆ๎‚ˆ๎‚๎‚’๎‚‹ ๎‚ˆ๎‚…๎‚˜๎‚…๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€‡๎€š๎€’๎ˆ๎ˆ’๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚…๎‚—๎‚—๎‚™๎‚‘๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚™๎‚‘ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰๎‚— ๎‚“๎‚Š ๎‚๎‚˜๎‚— ๎‚‘๎‚‰๎‚‘๎‚†๎‚‰๎‚–๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€†๎€…๎€Ž๎ˆ’๎ˆ˜๎‡‚๎€… ๎€ƒ๎‚š๎‚“๎‚๎‚ˆ ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚‰๎‚…๎‚๎‚…๎‚‹๎‚‰ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚”๎‚…๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚…๎‚‡๎‚–๎‚“๎‚—๎‚— ๎‚… ๎‚˜๎‚–๎‚™๎‚—๎‚˜ ๎‚†๎‚“๎‚™๎‚’๎‚ˆ๎‚…๎‚–๎‚๎‡€
9.3 bit-๎‚พeld
๎€‹๎‚’ ๎€…๎†ฝ ๎‚๎‚“๎‚™ ๎‚‡๎‚…๎‚’ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚Š๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‡ฆ๎‚๎‚’ ๎‚†๎‚๎‚˜๎‚—๎‡ง ๎‚“๎‚Š ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚“๎‚– ๎‚™๎‚’๎‚๎‚“๎‚’๎†ฝ ๎‚๎‚’ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚– ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰
๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚‰๎Šฒ๎‚‡๎‚๎‚‰๎‚’๎‚˜๎‚๎‚๎‡€ ๎€’๎‚–๎‚‰๎‚‡๎‚…๎‚™๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚˜๎‚…๎‚๎‚‰๎‚’ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚™๎‚—๎‚๎‚’๎‚‹ bit-๎Šฑeld๎‡€ ๎€‘๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚’๎‚‰ ๎‚Œ๎‚…๎‚’๎‚ˆ๎†ฝ ๎‚…๎‚’
int ๎‚˜๎‚๎‚”๎‚‰ ๎‚†๎‚๎‚˜๎‡‚๎Šฑ๎‚‰๎‚๎‚ˆ ๎‚›๎‚๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ๎‡€ ๎€‹๎‚’ ๎‚Š๎‚…๎‚‡๎‚˜๎†ฝ ๎‚…๎‚’ int ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚๎‚— ๎‚๎‚’๎‚ˆ๎‚‰๎‚‰๎‚ˆ ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜
๎‚‰๎‚œ๎‚‡๎‚‰๎‚”๎‚˜ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚†๎‚๎‚˜๎‡‚๎Šฑ๎‚‰๎‚๎‚ˆ๎†ฝ ๎‚›๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚‹๎‚’ ๎‚†๎‚‰๎‚‡๎‚“๎‚‘๎‚‰๎‚— ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
RULE
101
RULE โ€” All bit-๎‚พelds must be explicitly declared as unsigned
๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚–๎‚‘๎‚“๎‚–๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚†๎‚๎‚˜๎‡‚๎Šฑ๎‚‰๎‚๎‚ˆ๎‚— ๎‚๎‚— ๎‚…๎‚๎‚—๎‚“ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‡‚๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜๎†ฝ
๎‚—๎‚“ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚’๎‚“ ๎‚…๎‚—๎‚—๎‚™๎‚‘๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚‰๎‚š๎‚‰๎‚– ๎‚†๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚…๎‚†๎‚“๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚๎‚— ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
RULE
102
RULE โ€” Do not make assumptions about the internal representation of
structures with bit-๎‚พelds
Bad example
typedef struct structure {
int ok: 1; /* bit -field of size 1 */
int value: 7; /* bit - field for which the sign depends on the compiler used */
} struct_bitfield;
..
struct_bitfield s;
int *pt_s;
pt_s =(int *) &s;
s.ok =1;
..
if(s.ok ==1) /* if compiled with gcc for example , by default the bit - fields are
signed , therefore being of size 1, s.ok equals 0 or -1 ! */
{
pt_s ++; /* ? */
*pt_s =100; /* ? */
}
Good example
typedef struct structure {
unsigned int ok: 1; /* unsigned bit - field */
unsigned int value : 7; /* unsigned bit - field */
} struct_bitfield;
..
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 79
struct_bitfield s;
s.ok =1;
..
if(s.ok ==1) /* no longer compiler dependent */
{
s.value =100;
}
9.3.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‡๎€š๎€’๎ˆ๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‘๎‚…๎‚๎‚‰ ๎‚…๎‚—๎‚—๎‚™๎‚‘๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚–๎‚‰๎‚‹๎‚…๎‚–๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚…๎‚๎‚“๎‚™๎‚˜ ๎‚“๎‚Š ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚†๎‚๎‚˜๎‡‚๎Šฑ๎‚‰๎‚๎‚ˆ๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‡๎€š๎€’๎ˆ๎ˆ‘๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‘๎‚…๎‚๎‚‰ ๎‚…๎‚—๎‚—๎‚™๎‚‘๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚†๎‚“๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚”๎‚๎‚…๎‚๎‚’ ๎‚๎‚’๎‚˜ ๎‚†๎‚๎‚˜๎‡‚๎Šฑ๎‚‰๎‚๎‚ˆ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’
๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€†๎‚๎‚–๎‡€ ๎ˆ๎‡€๎ˆ๎‡€ ๎€ƒ๎‚’๎‚ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‡‚๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚“๎‚’ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚™๎‚˜๎‚”๎‚™๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘
๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚“๎‚“๎‚ˆ๎‡€
9.4 Use of FAMs
๎€ˆ๎€ƒ๎€๎‚—
๎ˆ๎ˆ
๎‚›๎‚‰๎‚–๎‚‰ ๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎€…๎ˆ˜๎ˆ˜๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚๎‚’๎‚‹ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚…๎‚—๎‚˜ ๎‚‘๎‚‰๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰
๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚ˆ๎‚๎‚‘๎‚‰๎‚’๎‚—๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚“๎‚Š ๎Šท๎‚‰๎‚œ๎‚๎‚†๎‚๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚†๎‚ ๎‚’๎‚…๎‚˜๎‚™๎‚–๎‚‰๎‡€ ๎€‹๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰
๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‡ฆ๎‚“๎‚– ๎‚‡๎‚“๎‚”๎‚๎‚‰๎‚ˆ๎‡ง ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚†๎‚™๎‚˜ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚‡๎‚๎†ฝ ๎‚’๎‚“ ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚๎‚— ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚๎‚— ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚…๎‚’๎‚ˆ
๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚๎‚˜ ๎‚‡๎‚…๎‚™๎‚—๎‚‰๎‚— ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€
๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚–๎‚‘๎‚“๎‚–๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‘๎‚‰๎‚…๎‚’๎‚— ๎‚…๎‚‡๎‚‡๎‚‰๎‚”๎‚˜๎‚๎‚’๎‚‹ ๎‚…๎‚–๎‚–๎‚…๎‚๎‚— ๎‚“๎‚Š ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚—๎‚๎‚ž๎‚‰๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚…๎‚ˆ๎‚๎‚‡๎‚˜๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚™๎‚๎‚‰ ๎‚๎‚’ ๎‚—๎‚‰๎‚‡๎‡‚
๎‚˜๎‚๎‚“๎‚’ ๎ˆ—๎‡€๎ˆ’๎‡€ ๎€ˆ๎€ƒ๎€๎‚— ๎‚…๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ๎‡€
RULE
103
RULE โ€” Do not use FAMs
9.4.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ—๎‡€๎ˆ– ๎€ˆ๎‚๎‚‰๎‚œ๎‚๎‚†๎‚๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚‘๎‚‰๎‚‘๎‚†๎‚‰๎‚– ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€๎€‡๎€๎ˆ’๎ˆ’๎‡‚๎€… ๎€ƒ๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚”๎‚ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚๎‚’๎‚‹ ๎‚… ๎Šท๎‚‰๎‚œ๎‚๎‚†๎‚๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚‘๎‚‰๎‚‘๎‚†๎‚‰๎‚– ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡๎‚…๎‚๎‚๎‚๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€†๎€…๎€Ž๎ˆ’๎ˆ—๎‡‚๎€… ๎€—๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚—๎‚๎‚’๎‚˜๎‚…๎‚œ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚๎‚’๎‚‹ ๎‚… ๎Šท๎‚‰๎‚œ๎‚๎‚†๎‚๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚‘๎‚‰๎‚‘๎‚†๎‚‰๎‚–๎‡€
9.5 Do not use unions
๎€–๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰๎†ฝ ๎‚š๎‚๎‚… ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚’๎‚๎‚“๎‚’ ๎‚‘๎‚‰๎‚‡๎‚Œ๎‚…๎‚’๎‚๎‚—๎‚‘๎†ฝ ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚—๎‚˜๎‚“๎‚–๎‚‰
๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚˜๎‚๎‚”๎‚‰๎‚—๎‡€ ๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚— ๎‚… ๎‚–๎‚๎‚—๎‚ ๎‚“๎‚Š ๎‚‘๎‚๎‚—๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚”๎‚–๎‚‰๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚๎‚—๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚…๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰
๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚Š๎‚“๎‚– ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚†๎‚‰ ๎‚…๎‚š๎‚“๎‚๎‚ˆ๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚Š๎‚…๎‚– ๎‚…๎‚— ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰๎‡€
๎ˆ๎ˆ๎‡€ ๎€ˆ๎‚๎‚‰๎‚œ๎‚๎‚†๎‚๎‚‰ ๎€ƒ๎‚–๎‚–๎‚…๎‚ ๎€๎‚‰๎‚‘๎‚†๎‚‰๎‚–
80 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
RECO
104
RECOMMENDATION โ€” Do not use unions
๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚Š๎‚“๎‚– ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚…๎‚™๎‚˜๎‚Œ๎‚“๎‚–๎‚๎‚—๎‚‰๎‚ˆ๎‡€
๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚™๎‚’๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‚๎‚ ๎‚๎‚๎‚‘๎‚๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚‡๎‚…๎‚—๎‚‰๎‚— ๎‚›๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚— ๎‚š๎‚‰๎‚–๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚‘๎‚‰๎‚…๎‚’๎‚— ๎‚…๎‚’๎‚ˆ
๎‚“๎‚’๎‚๎‚ ๎‚๎‚Š ๎‚๎‚˜ ๎‚๎‚— ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‡ฆ๎‚Š๎‚“๎‚– ๎‚’๎‚‰๎‚˜๎‚›๎‚“๎‚–๎‚ ๎‚Š๎‚–๎‚…๎‚‘๎‚‰ ๎‚”๎‚…๎‚–๎‚—๎‚๎‚’๎‚‹ ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎‡ง๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚Ž๎‚™๎‚—๎‚˜๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚…
๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€
9.5.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ˜๎‡€๎ˆ‘ ๎€–๎‚Œ๎‚‰ ๎‚™๎‚’๎‚๎‚“๎‚’ ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 81
10
Expressions
10.1 Integer expressions
๎€•๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚†๎‚…๎‚—๎‚๎‚‡ ๎‚”๎‚–๎‚‰๎‚‡๎‚…๎‚™๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚˜๎‚…๎‚๎‚‰๎‚’ ๎‚›๎‚Œ๎‚‰๎‚’๎‚‰๎‚š๎‚‰๎‚– ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚–๎‚—๎‡€
๎€ˆ๎‚“๎‚– ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰๎‚ˆ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚— ๎‚’๎‚“ ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚› ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‡‚
๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚๎‚”๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚™๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚›๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚’๎‚“ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚›๎‚–๎‚…๎‚”๎‡€
RULE
105
RULE โ€” Remove all possible value over๎‚ฟows for signed integers
RECO
106
RECOMMENDATION โ€” Detect all possible value wraps for unsigned inte-
gers
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚’๎‚“ ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚› ๎‚๎‚— ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ๎‡€
#include <stdint .h>
void f( uint8_t i, int8_t j)
{
uint8_t ibis = i+ 2;
int8_t j_bis = j +3;
/* ... */
}
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‚— ๎‚…๎‚–๎‚‰ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ๎‡€
#include <stdint .h>
void f( uint8_t i, int8_t j)
{
uint8_t ibis;
int8_t j_bis;
if (i > (UINT8_MAX - 2))
{
/* error */
}
else
{
ibis =i+2;
82 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
}
if (j > (INT8_MAX - 3))
{
/* error */
}
else
{
jbis =j+3;
}
/* ... */
}
๎€•๎‚๎‚‘๎‚๎‚๎‚…๎‚–๎‚๎‚๎†ฝ ๎‚…๎‚๎‚ ๎‚”๎‚“๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚ˆ๎‚™๎‚‰ ๎‚˜๎‚“ ๎‚ˆ๎‚๎‚š๎‚๎‚—๎‚๎‚“๎‚’ ๎‚†๎‚ ๎‚ž๎‚‰๎‚–๎‚“ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚š๎‚“๎‚๎‚ˆ๎‚‰๎‚ˆ๎‡€
RULE
107
RULE โ€” Detect and remove any potential division by zero
๎€–๎‚Œ๎‚๎‚— ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚Š๎‚“๎‚– ๎‚…๎‚’๎‚ ๎‚ˆ๎‚๎‚š๎‚๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚– ๎‚‡๎‚“๎‚‘๎‚”๎‚™๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚–๎‚‰๎‚‘๎‚…๎‚๎‚’๎‚ˆ๎‚‰๎‚– ๎‚“๎‚Š ๎‚…
๎‚ˆ๎‚๎‚š๎‚๎‚—๎‚๎‚“๎‚’๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚’๎‚“ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚ ๎‚“๎‚’ ๎‚… ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚ˆ๎‚๎‚š๎‚๎‚—๎‚๎‚“๎‚’ ๎‚†๎‚ ๎‚ž๎‚‰๎‚–๎‚“ ๎‚๎‚— ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ๎‡€
#include <stdint .h>
void func( int8_t i, int8_t j)
{
int8_t result;
result = i / j;
...
}
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚— ๎‚… ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚ ๎‚“๎‚’ ๎‚… ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚ˆ๎‚๎‚š๎‚๎‚—๎‚๎‚“๎‚’ ๎‚†๎‚ ๎‚ž๎‚‰๎‚–๎‚“๎‡€
#include <stdint .h>
void func( int8_t i, int8_t j)
{
int8_t result;
if (0 == j)
{
/* error */
}
else
{
result = i / j;
}
...
}
10.1.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‹๎€๎€–๎ˆ’๎ˆ๎‡‚๎€… ๎€‡๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚™๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚›๎‚–๎‚…๎‚”๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‹๎€๎€–๎ˆ’๎ˆ๎‡‚๎€… ๎€‡๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚๎‚’ ๎‚๎‚“๎‚—๎‚˜ ๎‚“๎‚– ๎‚‘๎‚๎‚—๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚”๎‚–๎‚‰๎‚˜๎‚‰๎‚ˆ ๎‚ˆ๎‚…๎‚˜๎‚…๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‹๎€๎€–๎ˆ’๎ˆ‘๎‡‚๎€… ๎€‡๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚’ ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚–๎‚— ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚๎‚’ ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‹๎€๎€–๎ˆ’๎ˆ’๎‡‚๎€… ๎€‡๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚ˆ๎‚๎‚š๎‚๎‚—๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚–๎‚‰๎‚‘๎‚…๎‚๎‚’๎‚ˆ๎‚‰๎‚– ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚๎‚’ ๎‚ˆ๎‚๎‚š๎‚๎‚ˆ๎‚‰๎‡‚๎‚†๎‚๎‡‚๎‚ž๎‚‰๎‚–๎‚“
๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‹๎€๎€–๎ˆ๎ˆ—๎‡‚๎€… ๎€˜๎‚‰๎‚–๎‚๎‚Š๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚๎‚ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚๎‚’ ๎‚–๎‚…๎‚’๎‚‹๎‚‰๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 83
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‹๎€๎€–๎ˆ๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚…๎‚—๎‚—๎‚™๎‚‘๎‚‰ ๎‚… ๎‚”๎‚“๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚–๎‚‰๎‚‘๎‚…๎‚๎‚’๎‚ˆ๎‚‰๎‚– ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚™๎‚—๎‚๎‚’๎‚‹ % ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‹๎€๎€–๎ˆ๎ˆ—๎‡‚๎€… ๎€‡๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚๎‚’ ๎‚… ๎‚๎‚…๎‚–๎‚‹๎‚‰๎‚– ๎‚—๎‚๎‚ž๎‚‰ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚’๎‚‹ ๎‚“๎‚– ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚๎‚’๎‚‹ ๎‚˜๎‚“
๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚—๎‚๎‚ž๎‚‰๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‹๎€๎€–๎ˆ๎ˆ•๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‘๎‚…๎‚๎‚‰ ๎‚…๎‚—๎‚—๎‚™๎‚‘๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚†๎‚“๎‚™๎‚˜ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚–๎‚—๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ๎ˆ˜๎ˆ ๎€‹๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚› ๎‚“๎‚– ๎‚›๎‚–๎‚…๎‚”๎‚…๎‚–๎‚“๎‚™๎‚’๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ—๎ˆ‘ ๎€‹๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚‡๎‚…๎‚๎‚‡๎‚™๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ’๎ˆ•๎ˆ˜ ๎€†๎‚๎‚š๎‚๎‚ˆ๎‚‰ ๎‚†๎‚ ๎€œ๎‚‰๎‚–๎‚“๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€‹๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚ˆ๎‚๎‚š๎‚๎‚—๎‚๎‚“๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‡ฌ๎‚ˆ๎‚๎‚š๎‚‰๎‚–๎‚–๎‡ญ๎‡€
10.2 Readability of arithmetic operations
๎€—๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚…๎‚’ ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚‡๎‚…๎‚๎‚‡๎‚™๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ ๎‚๎‚Š ๎‚’๎‚“ ๎‚‰๎Šฎ๎‚“๎‚–๎‚˜ ๎‚Œ๎‚…๎‚— ๎‚†๎‚‰๎‚‰๎‚’ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚˜๎‚“ ๎‚‘๎‚…๎‚๎‚‰ ๎‚๎‚˜
๎‚๎‚‰๎‚‹๎‚๎‚†๎‚๎‚‰๎‡€ ๎€๎‚“๎‚–๎‚‰๎‚“๎‚š๎‚‰๎‚–๎†ฝ ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚›๎‚–๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚‘๎‚‰๎‚˜๎‚Œ๎‚“๎‚ˆ ๎‚‡๎‚Œ๎‚“๎‚—๎‚‰๎‚’ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚๎‚‡๎‚™๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚๎‚˜ ๎‚‘๎‚…๎‚ ๎‚”๎‚–๎‚“๎‚š๎‚‰
๎‚…๎‚‘๎‚†๎‚๎‚‹๎‚™๎‚“๎‚™๎‚—๎‡€
๎€ƒ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚›๎‚๎‚๎‚ ๎‚’๎‚‰๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚—๎‚๎‚‘๎‚”๎‚๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚๎‚ˆ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚๎‚’๎‚‹๎‡€ ๎€‹๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ๎‚๎‚˜๎‚ ๎‚๎‚— ๎‚–๎‚‰๎‚๎‚‰๎‚š๎‚…๎‚’๎‚˜
๎‡ฆ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ etc.๎‡ง๎†ฝ ๎‚… ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚‰๎‚œ๎‚”๎‚๎‚…๎‚๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚‡๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚’๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‡€ ๎€ƒ ๎‚Š๎‚…๎‚๎‚–๎‚๎‚ ๎‚‡๎‚“๎‚‘๎‚‘๎‚“๎‚’
๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚๎‚— ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰ ๎‚… n๎‡‚๎‚†๎‚๎‚˜ ๎‚๎‚‰๎Šน ๎‚—๎‚Œ๎‚๎Šน ๎‚Š๎‚“๎‚– ๎‚… ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚†๎‚ 2
n
๎‡ฆ๎‚“๎‚– ๎‚… ๎‚–๎‚๎‚‹๎‚Œ๎‚˜ ๎‚—๎‚Œ๎‚๎Šน ๎‚Š๎‚“๎‚– ๎‚… ๎‚ˆ๎‚๎‚š๎‚๎‚—๎‚๎‚“๎‚’๎‡ง๎‡€ ๎€–๎‚Œ๎‚™๎‚—๎†ฝ
๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎†ฟ
a << b;
๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ a โˆ— 2
b
๎‡€ ๎€•๎‚™๎‚‡๎‚Œ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚Œ๎‚‰๎‚๎‚” ๎‚›๎‚๎‚˜๎‚Œ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰
๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€ ๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚—๎‚Œ๎‚๎Šน๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚ ๎‚›๎‚๎‚˜๎‚Œ ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚‰ ๎‚–๎‚™๎‚๎‚‰๎‚— ๎‚˜๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚๎‚’๎‚˜๎‚“ ๎‚…๎‚‡๎‚‡๎‚“๎‚™๎‚’๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š
๎‚†๎‚๎‚˜ ๎‚—๎‚Œ๎‚๎Šน๎‚— ๎‚–๎‚‰๎‚•๎‚™๎‚‰๎‚—๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚‡๎‚‰๎‚–๎‚’๎‚‰๎‚ˆ ๎‡ฆcf. ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ๎ˆ๎‡€๎ˆ–๎‡ง๎‡€ ๎€‹๎‚˜ ๎‚๎‚— ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰
๎‚†๎‚๎‚˜ ๎‚—๎‚Œ๎‚๎Šน๎‚— ๎‚“๎‚’๎‚๎‚ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚™๎‚–๎‚”๎‚“๎‚—๎‚‰ ๎‚๎‚— ๎‚˜๎‚“ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚๎‚˜๎‚— ๎‚“๎‚Š ๎‚… ๎‚–๎‚‰๎‚‹๎‚๎‚—๎‚˜๎‚‰๎‚–๎†ฝ ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎‡€
RECO
108
RECOMMENDATION โ€” Arithmetic operations should be written in a way
that assists with readability
๎€ƒ๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚…๎‚— ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‡ฆ๎‚’๎‚…๎‚˜๎‚™๎‚–๎‚…๎‚๎‡ง ๎‚…๎‚— ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚› ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚“๎‚‹๎‚๎‚‡
๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚…๎‚ˆ๎‚…๎‚†๎‚๎‚‰๎‡€ ๎€—๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚๎‚’๎‚‹
๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚๎‚‘๎‚‘๎‚‰๎‚ˆ๎‚๎‚…๎‚˜๎‚‰๎‡€
/* In the following calculation , we want to calculate aยฒ + 4 ac + bยฒ */
uint64_t res;
uint32_t a, b, c;
res = a * a + ((a * c) << 2) + b * b; /* an explanation would be welcome */
/* a<<b is equalivaent to a* 2^b but here the bit shift is used for a
multiplication */
...
/* mask computation */
uint32_t bitfield = 0xCAFEBABE ;
uint32_t n, bitmask;
n = 4;
bitmask = 1;
for(n = n; n > 0; n--) {
bitmask = 2 * bitmask ;
} /* on the contrary here , the bit shift would have been more logical */
84 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
bitfield = bitfield & (~ bitmask);
Good example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚— ๎‚‡๎‚…๎‚๎‚‡๎‚™๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚—๎‚๎‚‘๎‚”๎‚๎‚‰ ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
/* computation of aยฒ + 4 ac + bยฒ */
uint64_t res;
uint32_t a;
uint32_t b;
uint32_t c;
res = (a * a) + (4 * (a * c)) + (b * b); /* plus clair */
...
/* mask computation */
uint32_t bitfield = 0xCAFEBABE ;
uint32_t n = 4;
uint32_t bitmask
bitmask = 2 << n; /* bits handling */
bitfield = bitfield & (~ bitmask);
10.2.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‹๎€๎€–๎ˆ๎ˆ“๎‡‚๎€… ๎€ƒ๎‚š๎‚“๎‚๎‚ˆ ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚๎‚’๎‚‹ ๎‚†๎‚๎‚˜๎‚›๎‚๎‚—๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚…๎‡€
10.3 Use of parentheses to make explicit the order of the
operators
๎€–๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚Œ๎‚…๎‚— ๎‚‘๎‚…๎‚’๎‚ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚—๎†ฝ ๎‚›๎‚๎‚˜๎‚Œ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚๎‚‰๎‚š๎‚‰๎‚๎‚— ๎‚“๎‚Š ๎‚”๎‚–๎‚๎‚“๎‚–๎‚๎‚˜๎‚ ๎‚๎‚’ ๎‚˜๎‚‰๎‚–๎‚‘๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚๎‚š๎‡‚
๎‚๎‚˜๎‚๎‡€ ๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚— ๎‚๎‚’ ๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚๎‚˜ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚…๎‚’๎‚ˆ
๎‚”๎‚–๎‚“๎‚“๎‚Š๎‚–๎‚‰๎‚…๎‚ˆ๎‡€
๎€–๎‚Œ๎‚‰ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚๎‚‡๎‚™๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚๎‚˜ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚‡๎‚๎‚‰๎‚…๎‚–๎‚๎‚ ๎‚—๎‚Œ๎‚“๎‚› ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚Œ๎‚“๎‚“๎‚—๎‚‰
๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚๎‚“๎‚–๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚๎‚’ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚๎‚‡๎‚™๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ๎‡€
Information
๎€–๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚”๎‚–๎‚๎‚“๎‚–๎‚๎‚˜๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚…๎‚”๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚œ ๎€†๎‡€
RULE
109
RULE โ€” Explanation of the order of evaluation of calculations through the
use of parentheses
๎€–๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚…๎‚’๎‚ ๎‚…๎‚‘๎‚†๎‚๎‚‹๎‚™๎‚๎‚˜๎‚ ๎‚๎‚’ ๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎†ฝ ๎‚๎‚˜๎‚— ๎‚—๎‚™๎‚†๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚—๎‚™๎‚–๎‚–๎‚“๎‚™๎‚’๎‚ˆ๎‚‰๎‚ˆ ๎‚†๎‚
๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚— ๎‚˜๎‚“ ๎‚‘๎‚…๎‚๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚“๎‚Š ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚‡๎‚…๎‚๎‚‡๎‚™๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‡€
10.3.1 References
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 85
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€‡๎€š๎€’๎ˆ๎ˆ๎‡‚๎€… ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚“๎‚Š ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚—๎‚™๎‚†๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚๎‚’
๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚—๎‚๎‚ˆ๎‚‰ ๎‚‰๎Šฎ๎‚‰๎‚‡๎‚˜๎‚— ๎‚˜๎‚…๎‚๎‚‰ ๎‚”๎‚๎‚…๎‚‡๎‚‰๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‡๎€š๎€’๎ˆ’๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚“๎‚Š ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚“๎‚– ๎‚—๎‚๎‚ˆ๎‚‰ ๎‚‰๎Šฎ๎‚‰๎‚‡๎‚˜๎‚—๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€ƒ๎‚ˆ๎‚š๎‡€ ๎ˆ๎ˆ‘๎‡€๎ˆ ๎€–๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚‡๎‚‰๎‚ˆ๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ’๎‡€๎ˆ‘ ๎€–๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚๎‚˜๎‚— ๎‚”๎‚‰๎‚–๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚’๎‚˜ ๎‚—๎‚๎‚ˆ๎‚‰ ๎‚‰๎Šฎ๎‚‰๎‚‡๎‚˜๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰
๎‚™๎‚’๎‚ˆ๎‚‰๎‚– ๎‚…๎‚๎‚ ๎‚”๎‚‰๎‚–๎‚‘๎‚๎‚˜๎‚˜๎‚‰๎‚ˆ ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚–๎‚—๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ’๎‡€๎ˆ” ๎€–๎‚Œ๎‚‰ ๎‚–๎‚๎‚‹๎‚Œ๎‚˜ ๎‚Œ๎‚…๎‚’๎‚ˆ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚… ๎‚๎‚“๎‚‹๎‚๎‚‡๎‚…๎‚ && ๎‚“๎‚– || ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’
๎‚”๎‚‰๎‚–๎‚—๎‚๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚—๎‚๎‚ˆ๎‚‰ ๎‚‰๎Šฎ๎‚‰๎‚‡๎‚˜๎‚—๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ–๎ˆ—๎ˆ’ ๎€‘๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎€’๎‚–๎‚‰๎‚‡๎‚‰๎‚ˆ๎‚‰๎‚’๎‚‡๎‚‰ ๎€Ž๎‚“๎‚‹๎‚๎‚‡ ๎€‡๎‚–๎‚–๎‚“๎‚–๎‡€
10.4 No multiple comparison of variables without
parentheses
๎€‹๎‚˜ ๎‚๎‚— ๎‚‡๎‚“๎‚‘๎‚‘๎‚“๎‚’ ๎‚˜๎‚“ ๎‚›๎‚…๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚…๎‚‹๎‚…๎‚๎‚’๎‚—๎‚˜ ๎‚… ๎‚๎‚“๎‚›๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚…๎‚’ ๎‚™๎‚”๎‚”๎‚‰๎‚– ๎‚๎‚๎‚‘๎‚๎‚˜๎†ฝ ๎‚…๎‚’๎‚ˆ
๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚Œ๎‚“๎‚–๎‚˜๎‚‡๎‚™๎‚˜ ๎‚“๎‚Š ๎‚ˆ๎‚“๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚๎‚— ๎‚๎‚’ ๎‚… ๎‚—๎‚๎‚’๎‚‹๎‚๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚— ๎‚๎‚— ๎‚…๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‡€ ๎€Ž๎‚‰๎‚˜ ๎‚™๎‚— ๎‚˜๎‚…๎‚๎‚‰ ๎‚˜๎‚Œ๎‚‰
๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚…๎‚— ๎‚…๎‚’ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฟ (0<=x<=n)๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚๎‚‰๎Šน ๎‚—๎‚๎‚ˆ๎‚‰๎†ฝ i.e. 0<=x๎†ฝ ๎‚๎‚— ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚‰๎‚ˆ ๎Šฑ๎‚–๎‚—๎‚˜๎‡€ ๎€–๎‚Œ๎‚‰
๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡ฆ๎ˆ ๎‚“๎‚– ๎ˆ๎‡ง ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚๎‚‹๎‚Œ๎‚˜๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚›๎‚๎‚๎‚ ๎‚…๎‚๎‚›๎‚…๎‚๎‚—
๎‚†๎‚‰ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚…๎‚’๎‚ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚’ ๎‚‹๎‚–๎‚‰๎‚…๎‚˜๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚“๎‚– ๎‚‰๎‚•๎‚™๎‚…๎‚ ๎‚˜๎‚“ ๎ˆ๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ (0<=x<=n) ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰
๎‚—๎‚‰๎‚‘๎‚…๎‚’๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚‰๎‚•๎‚™๎‚๎‚š๎‚…๎‚๎‚‰๎‚’๎‚˜ ๎‚˜๎‚“ ((0<=x)<=n) ๎‚…๎‚’๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚˜๎‚“ ((0<=x)&&(x<=n))๎‡€
๎€ƒ๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚‡๎‚๎‚…๎‚—๎‚—๎‚๎‚‡ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚†๎‚๎‚’๎‚‰๎‚ˆ if(a==b==c) ๎‚‰๎‚•๎‚™๎‚…๎‚๎‚๎‚˜๎‚ ๎‚˜๎‚‰๎‚—๎‚˜ ๎‚›๎‚Œ๎‚“๎‚—๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚๎‚—๎†ฝ ๎‚… ๎‚”๎‚–๎‚๎‚“๎‚–๎‚๎†ฝ ๎‚˜๎‚“
๎‚š๎‚‰๎‚–๎‚๎‚Š๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚Œ๎‚–๎‚‰๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚‰๎‚•๎‚™๎‚…๎‚๎‡€ ๎€‹๎‚’ ๎‚”๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚‡๎‚‰๎†ฝ ๎‚…๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚š๎‚๎‚“๎‚™๎‚— ๎‚‡๎‚…๎‚—๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚˜๎‚‰๎‚—๎‚˜ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜
๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚‰ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚‰๎‚œ๎‚”๎‚‰๎‚‡๎‚˜๎‚—๎‡€ ๎€‹๎‚’๎‚ˆ๎‚‰๎‚‰๎‚ˆ๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚›๎‚๎‚๎‚ ๎‚“๎‚’๎‚๎‚ ๎‚†๎‚‰ ๎‚˜๎‚–๎‚™๎‚‰ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚Œ๎‚–๎‚‰๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—
๎‚…๎‚–๎‚‰ ๎ˆ ๎‚“๎‚– ๎‚๎‚Š c ๎‚๎‚— ๎ˆ ๎‚…๎‚’๎‚ˆ a ๎‚…๎‚’๎‚ˆ b ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜๎‡€
Boolean expression
๎€–๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚… ๎‚˜๎‚–๎‚™๎‚‰ ๎‚†๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚’ ๎€…๎ˆ˜๎ˆ๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚†๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚˜๎‚๎‚”๎‚‰ ๎‚›๎‚…๎‚—
๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎€…๎ˆ˜๎ˆ˜๎‡€ ๎€‹๎‚˜ ๎‚Œ๎‚…๎‚— ๎‚…๎‚’ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‡ฆstdbool.h๎‡ง๎‡€ ๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚›๎‚‰ ๎‚›๎‚๎‚๎‚
๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚‰๎‚–๎‚‘ ๎‚†๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰๎†ฝ ๎‚‰๎‚š๎‚‰๎‚’ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎€…๎ˆ˜๎ˆ˜๎†ฝ
๎‚›๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚… ๎‚˜๎‚–๎‚™๎‚˜๎‚Œ ๎‚š๎‚…๎‚๎‚™๎‚‰๎†ฝ ๎‚…๎‚— ๎‚๎‚— ๎‚˜๎‚๎‚”๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚˜๎‚Œ๎‚‰
๎‚‡๎‚…๎‚—๎‚‰ ๎‚Š๎‚“๎‚– ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚—๎‡€ ๎€ƒ ๎‚†๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚…๎‚๎‚—๎‚‰ ๎‚˜๎‚–๎‚™๎‚˜๎‚Œ
๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚Š๎‚“๎‚– ๎‚…๎‚’ ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎ˆ๎‡€ ๎€ƒ๎‚’๎‚ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚… ๎‚†๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’
๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‡ฆ๎‚›๎‚Œ๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚๎‚˜ ๎‚๎‚— ๎ˆ ๎‚“๎‚– ๎‚… ๎‚’๎‚‰๎‚‹๎‚…๎‚˜๎‚๎‚š๎‚‰๎†ฝ ๎‚”๎‚“๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰๎†ฝ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚“๎‚– ๎‚’๎‚“๎‚’๎‡‚๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚š๎‚…๎‚๎‚™๎‚‰๎‡ง ๎‚‡๎‚“๎‚–๎‡‚
๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚–๎‚™๎‚‰ ๎‚˜๎‚–๎‚™๎‚˜๎‚Œ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‡€
๎€„๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚๎‚’๎‚‹ ๎‚…๎‚˜ ๎‚๎‚‰๎‚…๎‚—๎‚˜ ๎‚˜๎‚›๎‚“ ๎‚–๎‚‰๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‡‚
๎‚—๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚†๎‚–๎‚“๎‚๎‚‰๎‚’ ๎‚ˆ๎‚“๎‚›๎‚’ ๎‚‰๎‚๎‚˜๎‚Œ๎‚‰๎‚– ๎‚๎‚’๎‚˜๎‚“ ๎‚’๎‚‰๎‚—๎‚˜๎‚‰๎‚ˆ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚๎‚— ๎‚“๎‚– ๎‚๎‚’๎‚˜๎‚“ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚–๎‚‰๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚—๎‡€
RECO
110
RECOMMENDATION โ€” Avoid expressions of comparison or multiple equal-
ity
86 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
RULE
111
RULE โ€” Always use parentheses in expressions of comparison or multiple
equality
๎€„๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’ ๎‚“๎‚– ๎‚‰๎‚•๎‚™๎‚…๎‚๎‚๎‚˜๎‚ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚๎‚’๎‚‹ ๎‚…๎‚˜ ๎‚๎‚‰๎‚…๎‚—๎‚˜ ๎‚˜๎‚›๎‚“ ๎‚–๎‚‰๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚“๎‚”๎‡‚
๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚—๎‡€
Bad example
#define N 100
...
if (0 <= x <= N) {
/* statement 1 ALWAYS executed */
} else {
/* statement 2 NEVER executed */
}
...
if (30 < x < 40) {
printf ("pb"); /* ALWAYS executed */
}
...
Good example
#define N 100
...
if ((0 <= x) && (x <= N)) { /* case 1 : breakdown into 2 relational expressions */
/* statement 1 */
} else {
/* statement 2 */
}
...
if (30 < x) { /* case 2 : breakdown into 2 nested conditional statements */
if (x<40) {
printf ("pb");
...
}}
10.4.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ๎‡€๎ˆ ๎€”๎‚‰๎‚•๎‡€ ๎€‘๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚๎‚’๎‚…๎‚”๎‚”๎‚–๎‚“๎‚”๎‚–๎‚๎‚…๎‚˜๎‚‰ ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ‘๎‡€๎ˆ ๎€–๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚‡๎‚‰๎‚ˆ๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€‡๎€š๎€’๎ˆ๎ˆ๎‡‚๎€… ๎€”๎‚‰๎‚‡๎‡€ ๎€—๎‚—๎‚‰ ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚— ๎‚Š๎‚“๎‚– ๎‚”๎‚–๎‚‰๎‚‡๎‚‰๎‚ˆ๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€‡๎€š๎€’๎ˆ๎ˆ’๎‡‚๎€… ๎€”๎‚‰๎‚‡๎‡€ ๎€–๎‚–๎‚‰๎‚…๎‚˜ ๎‚–๎‚‰๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚…๎‚’๎‚ˆ ๎‚‰๎‚•๎‚™๎‚…๎‚๎‚๎‚˜๎‚ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚…๎‚— ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚›๎‚‰๎‚–๎‚‰ ๎‚’๎‚“๎‚’๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚๎‚š๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ–๎ˆ—๎ˆ’ ๎€‘๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎€’๎‚–๎‚‰๎‚‡๎‚‰๎‚ˆ๎‚‰๎‚’๎‚‡๎‚‰ ๎€Ž๎‚“๎‚‹๎‚๎‚‡ ๎€‡๎‚–๎‚–๎‚“๎‚–๎‡€
10.5 Parentheses around elements of a boolean expression
๎€™๎‚Œ๎‚‰๎‚’ ๎‚›๎‚–๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚†๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚‡๎‚๎‚™๎‚—๎‚๎‚š๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚๎‚š๎‚๎‚˜๎‚
๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚๎‚˜ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚— ๎‚…๎‚š๎‚“๎‚๎‚ˆ๎‚— ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹
๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚†๎‚ ๎‚‘๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚“๎‚Š ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 87
RULE
112
RULE โ€” Parentheses around the elements of a boolean expression
๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚… ๎‚†๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚†๎‚‰ ๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚—๎†ฝ
๎‚—๎‚“ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚— ๎‚’๎‚“ ๎‚…๎‚‘๎‚†๎‚๎‚‹๎‚™๎‚๎‚˜๎‚ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚“๎‚Š ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚˜๎‚“ ๎‚๎‚’๎‚“๎‚› ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚๎‚š๎‚๎‚˜๎‚ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‡‚
๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚๎‚“๎‚–๎‚๎‚˜๎‚ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰๎‚‘ ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚“๎‚Š ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
if (x > 0 && y * z > length ) {
...
}
u = z > 100 && 100 == x || x + y < z;
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚— ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚๎‚˜ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚๎‚’๎‚“๎‚› ๎‚˜๎‚Œ๎‚‰
๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚“๎‚Š ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
if ((x > 0) && ((y * z) > length)) {
...
}
u = (z > 100) && ((100 == x) || ((x + y) < z));
10.5.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€‡๎€š๎€’๎ˆ๎ˆ๎‡‚๎€… ๎€”๎‚‰๎‚‡๎‡€ ๎€—๎‚—๎‚‰ ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚— ๎‚Š๎‚“๎‚– ๎‚”๎‚–๎‚‰๎‚‡๎‚‰๎‚ˆ๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ‘๎‡€๎ˆ ๎€–๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚‡๎‚‰๎‚ˆ๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ–๎ˆ—๎ˆ’ ๎€‘๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎€’๎‚–๎‚‰๎‚‡๎‚‰๎‚ˆ๎‚‰๎‚’๎‚‡๎‚‰ ๎€Ž๎‚“๎‚‹๎‚๎‚‡ ๎€‡๎‚–๎‚–๎‚“๎‚–๎‡€
10.6 Implicit comparison with 0 prohibited
๎€‹๎‚’ ๎€…๎ˆ˜๎ˆ๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‡Œ๎‚˜๎‚–๎‚™๎‚‰๎‡ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚…๎‚’๎‚ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚’ ๎ˆ ๎‡ฆ๎‚›๎‚Œ๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚๎‚— ๎‚’๎‚‰๎‚‹๎‚…๎‚˜๎‚๎‚š๎‚‰๎†ฝ
๎‚”๎‚“๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰๎†ฝ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚“๎‚– ๎‚’๎‚“๎‚’๎‡‚๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚–๎‡ง ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‡Œ๎‚Š๎‚…๎‚๎‚—๎‚‰๎‡ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎ˆ๎‡€ ๎€ƒ๎‚— ๎‚… ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎†ฝ ๎‚๎‚˜
๎‚๎‚— ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚›๎‚–๎‚๎‚˜๎‚‰ ๎‚†๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚›๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚… ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’ ๎‚›๎‚๎‚˜๎‚Œ ๎ˆ ๎‚๎‚— ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚‘๎‚…๎‚ˆ๎‚‰๎‡€ ๎€‹๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜
๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’๎‚— ๎‚‘๎‚…๎‚๎‚‰ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚…๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚…๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜๎‡€ ๎€„๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜
๎‚™๎‚—๎‚‰ ๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎†ฟ
๎Š๎Š๎†ฝ ๎‡„๎Š๎†ฝ ๎Š”๎†ฝ ๎Š•๎†ฝ ๎Š”๎Š๎†ฝ ๎Š•๎Š
RULE
113
RULE โ€” Implicit comparison with 0 prohibited
๎€ƒ๎‚๎‚ ๎‚†๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚—๎‡€ ๎€๎‚“ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚˜๎‚‰๎‚—๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚š๎‚…๎‚๎‚™๎‚‰
๎‚‰๎‚•๎‚™๎‚…๎‚ ๎‚˜๎‚“ ๎ˆ ๎‚“๎‚– ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚Š๎‚–๎‚“๎‚‘ ๎ˆ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ๎‡€
88 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
RECO
114
RECOMMENDATION โ€” Using the bool type in C99
๎€‹๎‚’ ๎€…๎ˆ˜๎ˆ˜๎†ฝ ๎‚˜๎‚Œ๎‚‰ bool ๎‡ฆ๎‚“๎‚– _Bool๎‡ง ๎‚˜๎‚๎‚”๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚†๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚—๎‡€
๎€‹๎‚’ ๎€…๎ˆ˜๎ˆ˜๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ bool๎‡‚๎‚˜๎‚๎‚”๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚๎‚’ ๎‚… ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚๎‚— ๎‚…๎‚‡๎‚‡๎‚‰๎‚”๎‚˜๎‚‰๎‚ˆ๎‡€
Bad example
๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’๎‚— ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚‘๎‚“๎‚š๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚Š๎‚…๎‚š๎‚“๎‚™๎‚– ๎‚“๎‚Š ๎‚‰๎‚œ๎‡‚
๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’๎‚—๎‡€
#define MAX 10
uint8 z;
..
while (x) {
...
}
if (x < y) {
...
} else {
...
}
if (!z) { /* implicit comparison with 0 and z should be of the bool type */
...
}
if (ptr) {
...
}
for (x = MAX; x; x--) {
...
}
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚’๎‚“ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’ ๎‚๎‚— ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ
๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
#include <stdbool .h>
bool z; /* use of the bool type */
while (x > 0) {
...
}
if (x < y) {
...
} else {
...
}
if (FALSE== z) { /* constant on the left and explicit comparison of z; z being of
the bool type , if (! z) can be used */
...
}
if (NULL != ptr) {
...
}
for (x = MAX; x > 0; x--) {
...
}
10.6.1 References
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 89
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‡๎€š๎€’๎ˆ‘๎ˆ๎‡‚๎€… ๎€’๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚˜๎‚‰๎‚—๎‚˜๎‚— ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚‰ ๎‚—๎‚™๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎†ฝ ๎‚˜๎‚–๎‚™๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚Š๎‚…๎‚๎‚—๎‚‰๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚‰๎‚•๎‚™๎‚…๎‚๎‚๎‚˜๎‚๎‡€
10.7 Bitwise operators
๎€‰๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚๎‚๎‚ ๎‚—๎‚”๎‚‰๎‚…๎‚๎‚๎‚’๎‚‹๎†ฝ ๎‚†๎‚๎‚˜๎‚›๎‚๎‚—๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚“๎‚’๎‚๎‚ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚™๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚˜๎‚๎‚”๎‚‰๎‚—๎‡€ ๎€–๎‚Œ๎‚๎‚—
๎‚›๎‚…๎‚๎†ฝ ๎‚‘๎‚…๎‚’๎‚ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚“๎‚– ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‡‚๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚…๎‚š๎‚“๎‚๎‚ˆ๎‚‰๎‚ˆ๎‡€
RECO
115
RECOMMENDATION โ€” Bitwise operators must be used with unsigned
operands only
๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚–๎‚‘๎‚“๎‚–๎‚‰๎†ฝ ๎‚—๎‚“๎‚‘๎‚‰ ๎‚†๎‚๎‚˜๎‚›๎‚๎‚—๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— &๎†ฝ | ๎‚“๎‚– ^ ๎‚‡๎‚…๎‚’ ๎‚‰๎‚…๎‚—๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚‘๎‚๎‚—๎‚˜๎‚…๎‚๎‚‰๎‚’๎‚๎‚ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‡ก ๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚…๎‚๎‚๎‚
๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚–๎‚—๎‚˜ ๎‚˜๎‚›๎‚“ ๎‡ก ๎‚๎‚’๎‚—๎‚˜๎‚‰๎‚…๎‚ˆ ๎‚“๎‚Š ๎‚๎‚“๎‚‹๎‚๎‚‡๎‚…๎‚ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— &&๎†ฝ || ๎‚…๎‚’๎‚ˆ !๎‡€ ๎€–๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‡๎‚“๎‚’๎‚Š๎‚™๎‚—๎‚๎‚“๎‚’๎†ฝ ๎‚๎‚˜ ๎‚๎‚—
๎‚๎‚‘๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚๎‚˜๎‚›๎‚๎‚—๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚†๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚…๎‚‡๎‚˜๎‚™๎‚…๎‚๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚—๎‚๎‚–๎‚‰๎‚ˆ
๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚—๎‡€ ๎€„๎‚๎‚˜๎‚›๎‚๎‚—๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚’๎‚“ ๎‚–๎‚‰๎‚…๎‚—๎‚“๎‚’ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚’๎‚ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚˜๎‚๎‚”๎‚‰ ๎‚†๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚“๎‚–
๎‚—๎‚๎‚‘๎‚๎‚๎‚…๎‚–๎‡€
RULE
116
RULE โ€” No bitwise operator on an operand of type boolean or similar
Bad example
if ((var >= 0) & (var < 120)) { /* operator confusion */
...
if ((val && FLAG) != 0) /* operator confusion ? */
...
}
Good example
if ((var >= 0) && (var < 120)) { /* correction */
...
if ((val & FLAG) != 0) /* correct use here of the bitwise operator
in a boolean expression */
...
}
10.7.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‹๎€๎€–๎ˆ๎ˆ’๎‡‚๎€… ๎€—๎‚—๎‚‰ ๎€„๎‚๎‚˜๎‚›๎‚๎‚—๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚“๎‚’๎‚๎‚ ๎‚“๎‚’ ๎‚™๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‡๎€š๎€’๎ˆ๎ˆ“๎‡‚๎€… ๎€„๎‚‰๎‚›๎‚…๎‚–๎‚‰ ๎‚“๎‚Š ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚”๎‚–๎‚“๎‚‘๎‚“๎‚˜๎‚๎‚“๎‚’ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚๎‚’๎‚‹ ๎‚†๎‚๎‚˜๎‚›๎‚๎‚—๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚’ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚–
๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚—๎‚‘๎‚…๎‚๎‚๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚๎‚’๎‚˜๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‹๎€๎€–๎ˆ’๎ˆ“๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚—๎‚Œ๎‚๎Šน ๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚†๎‚ ๎‚… ๎‚’๎‚‰๎‚‹๎‚…๎‚˜๎‚๎‚š๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚†๎‚๎‚˜๎‚— ๎‚“๎‚– ๎‚†๎‚ ๎‚‹๎‚–๎‚‰๎‚…๎‚˜๎‚‰๎‚– ๎‚“๎‚– ๎‚‰๎‚•๎‚™๎‚…๎‚
๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚†๎‚๎‚˜๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‰๎‚œ๎‚๎‚—๎‚˜ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ๎‡€
90 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ—๎ˆ‘ ๎€‹๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚‡๎‚…๎‚๎‚‡๎‚™๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€‡๎€š๎€’๎ˆ“๎ˆ•๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚… ๎‚†๎‚๎‚˜๎‚›๎‚๎‚—๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎€„๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’๎‡‚๎‚๎‚๎‚๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ“๎ˆ—๎ˆ ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ๎‡€๎ˆ ๎€‘๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚๎‚’๎‚…๎‚”๎‚”๎‚–๎‚“๎‚”๎‚–๎‚๎‚…๎‚˜๎‚‰ ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰๎‡€
10.8 Boolean assignment and expression
๎€–๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚— ๎‚… ๎‚š๎‚…๎‚๎‚™๎‚‰๎‡€ ๎€‹๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚๎‚— ๎‚š๎‚…๎‚๎‚™๎‚‰๎‡€ ๎€Š๎‚“๎‚›๎‡‚
๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚๎‚— ๎‚“๎Šน๎‚‰๎‚’ ๎‚…๎‚’ ๎‚™๎‚’๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚‡๎‚“๎‚’๎‚Š๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’
๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– = ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚•๎‚™๎‚…๎‚๎‚๎‚˜๎‚ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ==๎‡€
GOOD
PRACTICE
117
GOOD PRACTICE โ€” Do not use the value returned during an assignment
Information
๎€†๎‚™๎‚–๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚Œ๎‚…๎‚—๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚๎‚‹๎‚Œ๎‚˜ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‡ฆ-Wall๎†ฝ etc.๎‡ง๎†ฝ ๎‚… ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹ ๎‚›๎‚๎‚๎‚
๎‚†๎‚‰ ๎‚‰๎‚‘๎‚๎‚˜๎‚˜๎‚‰๎‚ˆ ๎‚—๎‚™๎‚‹๎‚‹๎‚‰๎‚—๎‚˜๎‚๎‚’๎‚‹ ๎‚๎‚’ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚– ๎‚˜๎‚“ ๎‚”๎‚๎‚…๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚’ ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰
๎‚†๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‡ฆ-Wparentheses ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‡ง๎‡€
RULE
118
RULE โ€” Assignment prohibited in a boolean expression
๎€ƒ๎‚’ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚๎‚’ ๎‚…๎‚’๎‚ ๎‚†๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‡€ ๎€ƒ๎‚’ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜
๎‚†๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚๎‚’ ๎‚…๎‚’ ๎‚๎‚’๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€
๎€‹๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚๎‚๎‚‘๎‚๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚๎‚—๎‚๎‚— ๎‚“๎‚Š ๎‚›๎‚–๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚…๎‚’ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ = ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚๎‚’๎‚—๎‚˜๎‚‰๎‚…๎‚ˆ ๎‚“๎‚Š ๎‚… ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’
๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ == ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎†ฝ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’ ๎‚๎‚— ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚… ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ๎†ฝ
๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚›๎‚–๎‚๎‚˜๎‚˜๎‚‰๎‚’ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‰๎Šน ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ == ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰
๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚๎‚‹๎‚Œ๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚–๎‚…๎‚๎‚—๎‚‰๎‚— ๎‚… ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚–๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’ ๎‚… ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚˜๎‚“ ๎‚… ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜
๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ๎‡€
GOOD
PRACTICE
119
GOOD PRACTICE โ€” Comparison with constant operand on the left
๎€™๎‚Œ๎‚‰๎‚’ ๎‚… ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’ ๎‚๎‚’๎‚š๎‚“๎‚๎‚š๎‚‰๎‚— ๎‚… ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚”๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚…๎‚†๎‚๎‚ ๎‚†๎‚‰ ๎‚—๎‚‰๎‚˜ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰
๎‚๎‚‰๎Šน ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚…๎‚’ ๎‚™๎‚’๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚ˆ๎‚‰๎‚ˆ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜๎‡€
Information
๎€–๎‚Œ๎‚๎‚— ๎‚‹๎‚“๎‚“๎‚ˆ ๎‚”๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚‡๎‚‰ ๎‚๎‚— ๎‚ˆ๎‚‰๎‚†๎‚…๎‚˜๎‚…๎‚†๎‚๎‚‰๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚‰๎‚’๎‚Š๎‚“๎‚–๎‚‡๎‚‰๎‚ˆ ๎‚†๎‚™๎‚˜ ๎‚‘๎‚‰๎‚–๎‚‰๎‚๎‚ ๎‚…๎‚ˆ๎‚š๎‚๎‚—๎‚‰๎‚ˆ๎‡€
๎€…๎‚“๎‚‘๎‚”๎‚๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚๎‚— ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‡ฆ๎‚๎‚’ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚– ๎‚›๎‚๎‚˜๎‚Œ -Wall๎‡ง๎†ฝ ๎‚…๎‚— ๎‚–๎‚‰๎‚•๎‚™๎‚๎‚–๎‚‰๎‚ˆ
๎‚†๎‚ ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ”๎‡€๎ˆ‘๎†ฝ ๎‚๎‚— ๎‚…๎‚‡๎‚˜๎‚™๎‚…๎‚๎‚๎‚ ๎‚—๎‚™๎Šฒ๎‚‡๎‚๎‚‰๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚๎‚’๎‡‚
๎‚—๎‚˜๎‚‰๎‚…๎‚ˆ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚๎‚’ ๎‚… ๎‚†๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 91
Bad example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚— ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚๎‚’ ๎€„๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚—๎‡€ ๎€‘๎‚’๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‡‚
๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚๎‚’ ๎‚… ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚… ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‡€
if ((x = y + z) > 0) { /* assignment in a Boolean expression and use of the value
returned by the assignment and constant on the right */
...
}
if (z = VALUE) { /* constant on the right and = instead of == */
...
}
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚…๎‚๎‚ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚…๎‚–๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚๎‚’ ๎‚๎‚’๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚…๎‚’๎‚ˆ
๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚“๎‚™๎‚— ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘๎‚— ๎‚…๎‚–๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ๎‡€
x = y + z;
if (0 < x) {
...
}
if (VALUE == z) {
...
}
10.8.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ’๎‡€๎ˆ“ ๎€–๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‡๎€š๎€’๎ˆ“๎ˆ”๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚๎‚’ ๎‚—๎‚‰๎‚๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€๎‚“ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚’ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‡ฌ๎‚†๎‚“๎‚“๎‚๎‚…๎‚—๎‚๎‚‹๎‚’๎‡ญ๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ“๎ˆ—๎ˆ ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ“๎ˆ—๎ˆ ๎€ƒ๎‚—๎‚—๎‚๎‚‹๎‚’๎‚๎‚’๎‚‹ ๎‚๎‚’๎‚—๎‚˜๎‚‰๎‚…๎‚ˆ ๎‚“๎‚Š ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚’๎‚‹๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ“๎ˆ—๎ˆ‘ ๎€…๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚’๎‚‹ ๎‚๎‚’๎‚—๎‚˜๎‚‰๎‚…๎‚ˆ ๎‚“๎‚Š ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚๎‚’๎‚‹๎‡€
10.9 Multiple assignment of variables prohibited
๎€–๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚—๎‚๎‚’๎‚‹๎‚๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€
๎€–๎‚Œ๎‚๎‚— ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚— ๎‚“๎Šน๎‚‰๎‚’ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚๎‚’๎‚‹ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚๎‚— ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚–๎‚‰๎‚…๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚๎‚—๎‚“ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚‘๎‚…๎‚๎‚’๎‚˜๎‚…๎‚๎‚’๎‡€
๎€„๎‚–๎‚‰๎‚…๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚ˆ๎‚“๎‚›๎‚’ ๎‚๎‚’๎‚˜๎‚“ ๎‚…๎‚— ๎‚‘๎‚…๎‚’๎‚ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚…๎‚—
๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚…๎‚–๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎‡€
RULE
120
RULE โ€” Multiple assignment of variables prohibited
๎€๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚…๎‚™๎‚˜๎‚Œ๎‚“๎‚–๎‚๎‚—๎‚‰๎‚ˆ๎‡€
92 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
Bad example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚—๎‚Œ๎‚“๎‚›๎‚— ๎‚… ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜๎‡€
...
a = b = c = d = 1; /* multiple assignment */
Good example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚— ๎‚“๎‚’๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚”๎‚‰๎‚– ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‡€
...
a = 1;
b = 1;
c = 1;
d = 1;
10.10 Only one statement per line of code
๎€–๎‚Œ๎‚‰ ๎‚‰๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚๎‚— ๎‚‘๎‚…๎‚–๎‚๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚‘๎‚๎‚‡๎‚“๎‚๎‚“๎‚’๎‡€ ๎€–๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜
๎‚–๎‚‰๎‚•๎‚™๎‚๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚˜๎‚“ ๎‚›๎‚–๎‚๎‚˜๎‚‰ ๎‚“๎‚’๎‚๎‚ ๎‚“๎‚’๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚”๎‚‰๎‚– ๎‚๎‚๎‚’๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€
๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚…๎‚–๎‚‰ ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚๎‚๎‚’๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚— ๎‚๎‚‰๎‚—๎‚— ๎‚–๎‚‰๎‚…๎‚ˆ๎‚…๎‚†๎‚๎‚‰๎‡€ ๎€†๎‚‰๎‚†๎‚™๎‚‹๎‡‚
๎‚‹๎‚๎‚’๎‚‹ ๎‚๎‚— ๎‚…๎‚๎‚—๎‚“ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜๎†ฝ ๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎‚๎‚˜ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚’๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚…๎‚˜
๎‚… ๎‚˜๎‚๎‚‘๎‚‰๎‡€
๎€–๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚”๎‚‰๎‚– ๎‚๎‚๎‚’๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚…๎‚๎‚—๎‚“ ๎‚ˆ๎‚๎‚—๎‚˜๎‚“๎‚–๎‚˜๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚˜๎‚–๎‚๎‚‡ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚๎‚๎‚’๎‚‰๎‚—
๎‚“๎‚Š ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€
RULE
121
RULE โ€” Only one statement per line of code
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚— ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‡€
int32_t a; int64_t b;
a = 4; b = a / 6; printf("a = %d, b = %lld\n", a, b);
Good example
int32_t a;
int64_t b;
a = 4;
b = a / 6;
printf ("a = %d, b = %lld\n", a, b);
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 93
10.11 Use of ๎‚ฟoating-point numbers
Information
๎€„๎‚ ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚—๎†ฝ ๎‚›๎‚‰ ๎‚…๎‚–๎‚‰ ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚–๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚— ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚… ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‡‚
๎‚—๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
๎€–๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚— ๎‚๎‚’ ๎‚… ๎‚‘๎‚…๎‚‡๎‚Œ๎‚๎‚’๎‚‰ ๎‚๎‚— ๎‚… ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ ๎‚’๎‚“๎‚˜๎‚๎‚“๎‚’ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚๎‚— ๎‚“๎Šน๎‚‰๎‚’ ๎‚’๎‚“๎‚˜
๎‚›๎‚‰๎‚๎‚ ๎‚๎‚’๎‚“๎‚›๎‚’ ๎‚“๎‚– ๎‚†๎‚…๎‚ˆ๎‚๎‚ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚“๎‚“๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚“๎‚–๎‚‰๎‚“๎‚š๎‚‰๎‚–๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚๎‚“๎‚’ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ
๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰๎‡€ ๎€–๎‚Œ๎‚‰๎‚—๎‚‰ ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚“๎Šน๎‚‰๎‚’ ๎‚… ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚—๎‡€
๎€๎‚“๎‚˜ ๎‚…๎‚๎‚ ๎‚…๎‚‡๎‚˜๎‚™๎‚…๎‚ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚— ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚—๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‡Œ๎‚™๎‚’๎‚’๎‚…๎‚˜๎‚™๎‚–๎‚…๎‚๎‡ ๎‚”๎‚Œ๎‚‰๎‡‚
๎‚’๎‚“๎‚‘๎‚‰๎‚’๎‚… ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— absorption
๎ˆ๎ˆ
๎‚…๎‚’๎‚ˆ cancellation
๎ˆ๎ˆ‘
๎‚‡๎‚…๎‚’ ๎‚“๎‚‡๎‚‡๎‚™๎‚– ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚—๎†ฝ
๎‚…๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚‹๎‚Œ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚— ๎‚›๎‚๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚˜๎‚…๎‚๎‚๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰๎‡€ ๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚– ๎‚ˆ๎‚‰๎‚˜๎‚…๎‚๎‚๎‚— ๎‚…๎‚–๎‚‰ ๎‚‹๎‚๎‚š๎‚‰๎‚’ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ
๎€‹๎€‡๎€‡๎€‡๎ˆ–๎ˆ”๎ˆ“ ๎‡ฌ๎Šท๎‚“๎‚…๎‚˜๎‡ญ๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰๎‚— ๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚๎‚†๎‚๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡‚๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡‚๎‚…๎‚–๎‚‡๎‚Œ๎‚๎‚˜๎‚‰๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰
๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚—๎‡€
๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚— ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰๎‚‡๎‚“๎‚‘๎‚‰ ๎‚‹๎‚–๎‚‰๎‚…๎‚˜๎‚‰๎‚–
๎‚˜๎‚Œ๎‚…๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚๎‚‡๎‚™๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰๎‚‘๎‡€
๎€ˆ๎‚๎‚’๎‚…๎‚๎‚๎‚๎†ฝ ๎‚‡๎‚‰๎‚–๎‚˜๎‚…๎‚๎‚’ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚–๎‚ ๎‚”๎‚–๎‚“๎‚”๎‚‰๎‚–๎‚˜๎‚๎‚‰๎‚— ๎‚“๎‚Š ๎‚–๎‚‰๎‚…๎‚ ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚…๎‚–๎‚‰ ๎‚’๎‚“ ๎‚๎‚“๎‚’๎‚‹๎‚‰๎‚– ๎‚˜๎‚–๎‚™๎‚‰ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎Šท๎‚“๎‚…๎‚˜๎‚—๎†ฟ ๎‚‡๎‚“๎‚‘๎‡‚
๎‚‘๎‚™๎‚˜๎‚…๎‚˜๎‚๎‚š๎‚๎‚˜๎‚๎†ฝ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚๎‚š๎‚๎‚˜๎‚๎†ฝ etc.
๎€ˆ๎‚“๎‚– ๎‚…๎‚๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚–๎‚‰๎‚…๎‚—๎‚“๎‚’๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚— ๎‚๎‚— ๎‚—๎‚˜๎‚–๎‚“๎‚’๎‚‹๎‚๎‚ ๎‚ˆ๎‚๎‚—๎‚‡๎‚“๎‚™๎‚–๎‚…๎‚‹๎‚‰๎‚ˆ๎‡€ ๎€•๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰
๎‚“๎‚Š ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚— ๎‚”๎‚–๎‚“๎‚š๎‚‰ ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚Š๎‚“๎‚– ๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚๎‚‡๎‚…๎‚ ๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚›๎‚๎‚๎‚
๎‚Œ๎‚…๎‚š๎‚‰ ๎‚˜๎‚“ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎Šท๎‚“๎‚…๎‚˜ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚š๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚…๎‚–๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’
๎‚…๎‚‡๎‚‡๎‚“๎‚–๎‚ˆ๎‚…๎‚’๎‚‡๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚๎‚“๎‚’๎‡€
GOOD
PRACTICE
122
GOOD PRACTICE โ€” Avoid ๎‚ฟoating constants
๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚๎‚‡๎‚…๎‚ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚๎‚“๎‚—๎‚— ๎‚“๎‚Š ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚–
๎‚”๎‚Œ๎‚‰๎‚’๎‚“๎‚‘๎‚‰๎‚’๎‚… ๎‚–๎‚‰๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚—๎‡€ ๎€‹๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚‡๎‚…๎‚’๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚š๎‚“๎‚๎‚ˆ๎‚‰๎‚ˆ๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‡‚
๎‚—๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚š๎‚‰๎‚’๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎Šท๎‚“๎‚…๎‚˜ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚๎‚’ ๎‚•๎‚™๎‚‰๎‚—๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ๎‡€
RECO
123
RECOMMENDATION โ€” Limit the use of ๎‚ฟoating-point numbers to what is
strictly necessary
๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚๎‚๎‚‘๎‚๎‚˜๎‚‰๎‚ˆ๎‡€
๎€ˆ๎‚๎‚“๎‚…๎‚˜ ๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚“๎‚“๎‚” ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰๎‚— ๎‚“๎‚Š ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚ˆ๎‚™๎‚‰ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚๎‚‘๎‚๎‚˜๎‚‰๎‚ˆ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚š๎‚‰๎‚’๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚˜๎‚๎‚”๎‚‰ ๎‚…๎‚’๎‚ˆ
๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ๎‚๎‚˜๎‚๎‡€
๎ˆ๎ˆ๎‡€ ๎€’๎‚Œ๎‚‰๎‚’๎‚“๎‚‘๎‚‰๎‚’๎‚“๎‚’ ๎‚–๎‚‰๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎Šท๎‚“๎‚…๎‚˜๎‚—๎†ฝ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— LargeF loatV alue + F loatingEpsilon = LargeF loatV alue๎†ฝ i.e. ๎‚… ๎‚๎‚…๎‚–๎‚‹๎‚‰
๎Šท๎‚“๎‚…๎‚˜ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚›๎‚๎‚๎‚ ๎‡Œ๎‚…๎‚†๎‚—๎‚“๎‚–๎‚†๎‡ ๎‚… ๎‚—๎‚‘๎‚…๎‚๎‚ ๎Šท๎‚“๎‚…๎‚˜ ๎‚š๎‚…๎‚๎‚™๎‚‰
๎ˆ๎ˆ‘๎‡€ ๎€ƒ๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚”๎‚Œ๎‚‰๎‚’๎‚“๎‚‘๎‚‰๎‚’๎‚“๎‚’ ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚–๎‚‰๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜๎‚— ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚—๎†ฝ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚Š๎‚“๎‚– ๎‚˜๎‚›๎‚“ ๎‚‡๎‚๎‚“๎‚—๎‚‰
๎Šท๎‚“๎‚…๎‚˜ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚—๎†ฝ F loatV alue1 โˆ’ F loatV alue2 = 0๎†ฝ ๎‚›๎‚Œ๎‚‰๎‚–๎‚‰๎‚…๎‚— ๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚๎‚๎‚๎†ฝ F loatV alue1 ฬธ= F loatV alue2
94 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
RULE
124
RULE โ€” No ๎‚ฟoat type loop counter
๎€Ž๎‚“๎‚“๎‚” ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚’๎‚๎‚๎†ฝ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚› ๎‚š๎‚‰๎‚–๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š
๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚– ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚—๎‡€
๎€–๎‚Œ๎‚‰ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎Šท๎‚“๎‚…๎‚˜ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚๎‚’ ๎€„๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚๎‚— ๎‚…๎‚๎‚—๎‚“ ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚š๎‚‰๎‚–๎‚ ๎‚–๎‚๎‚—๎‚๎‚ ๎‚๎‚’ ๎‚‡๎‚“๎‚’๎‚’๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚›๎‚๎‚˜๎‚Œ
๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘๎‚— ๎‚“๎‚Š ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚—๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚“๎‚‹๎‚๎‚‡ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— != ๎‚“๎‚–
== ๎‚“๎‚’ ๎Šท๎‚“๎‚…๎‚˜๎‚— ๎‚๎‚— ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚๎‚’ ๎‚‘๎‚“๎‚—๎‚˜ ๎‚‡๎‚…๎‚—๎‚‰๎‚—๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎‚— ๎‚‘๎‚…๎‚ ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‰๎‚š๎‚‰๎‚ ๎‚“๎‚Š ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰
๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚๎‚˜๎‚—๎‚‰๎‚๎‚Š ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚๎‚…๎‚˜๎‚Š๎‚“๎‚–๎‚‘ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
RULE
125
RULE โ€” Do not use ๎‚ฟoating-point numbers for comparisons of equality or
inequality
Bad example
float y = 0.1; /* not representable in simple precision */
...
if (y == 0.1) /* comparison of float value AND 0.1 double value so promotion of y
*/
printf ("equal\n");
else
if (y == 0.1f) /* 0.1f float value - condition checked here */
printf ("equal2\n");
else printf(" not equal\n");
...
for (float x = 0.1f; x <= 1.0f; x += 0.1f) { /* the loop will be carried out 9 or
10 times */
}
Good example
double y = 0.1; /* type correction */
...
for (uint count = 1; count <= 10; count ++) {
float x = count /10.0f; /* 10 passes exactly in the loop */
}
10.11.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ“๎‡€๎ˆ ๎€ƒ ๎‚๎‚“๎‚“๎‚” ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚– ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚๎‚๎‚ ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚๎‚”๎‚‰๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€ˆ๎€Ž๎€’๎ˆ’๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚— ๎‚๎‚“๎‚“๎‚” ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚–๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€ˆ๎€Ž๎€’๎ˆ’๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚“ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚‰ ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€ˆ๎€Ž๎€’๎ˆ๎ˆ๎‡‚๎€… ๎€—๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚๎‚‘๎‚๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€ˆ๎€Ž๎€’๎ˆ๎ˆ๎‡‚๎€… ๎€–๎‚…๎‚๎‚‰ ๎‚‡๎‚…๎‚–๎‚‰ ๎‚๎‚’ ๎‚–๎‚‰๎‚…๎‚–๎‚–๎‚…๎‚’๎‚‹๎‚๎‚’๎‚‹ ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€ˆ๎€Ž๎€’๎ˆ๎ˆ‘๎‡‚๎€… ๎€ƒ๎‚š๎‚“๎‚๎‚ˆ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚— ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚™๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚’๎‚‰๎‚‰๎‚ˆ๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€ˆ๎€Ž๎€’๎ˆ๎ˆ’๎‡‚๎€… ๎€†๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚‰ ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€ˆ๎€Ž๎€’๎ˆ๎ˆ“๎‡‚๎€… ๎€…๎‚Œ๎‚‰๎‚‡๎‚ ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚๎‚’๎‚”๎‚™๎‚˜๎‚— ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚‡๎‚‰๎‚”๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€ˆ๎€Ž๎€’๎ˆ๎ˆ”๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚ˆ๎‚‰๎‚’๎‚“๎‚–๎‚‘๎‚…๎‚๎‚๎‚ž๎‚‰๎‚ˆ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚—๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ’๎ˆ•๎ˆ˜ ๎€†๎‚๎‚š๎‚๎‚ˆ๎‚‰ ๎‚†๎‚ ๎€œ๎‚‰๎‚–๎‚“๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ—๎ˆ ๎€‹๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚๎‚‡๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰๎‚—๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 95
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ—๎ˆ‘ ๎€‹๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚‡๎‚…๎‚๎‚‡๎‚™๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
10.12 Complex numbers
๎€•๎‚๎‚’๎‚‡๎‚‰ ๎€…๎ˆ˜๎ˆ˜๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚—๎‚™๎‚”๎‚”๎‚“๎‚–๎‚˜๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚๎‚‡๎‚™๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚–๎‚‰๎‚‰ ๎‚’๎‚‰๎‚› ๎‚†๎‚™๎‚๎‚๎‚˜๎‡‚๎‚๎‚’
๎‚˜๎‚๎‚”๎‚‰๎‚—๎†ฝ double_Complex๎†ฝ long double_Complex ๎‚…๎‚’๎‚ˆ float_Complex๎‡€ ๎€™๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚–
๎Šฑ๎‚๎‚‰ complex.h๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚…๎‚๎‚—๎‚“ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚š๎‚๎‚… double complex๎†ฝ long double complex ๎‚…๎‚’๎‚ˆ
float complex๎‡€ ๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚Œ๎‚–๎‚‰๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚๎‚‘๎‚…๎‚‹๎‚๎‚’๎‚…๎‚–๎‚ ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚…๎‚๎‚—๎‚“ ๎‚—๎‚™๎‚”๎‚”๎‚“๎‚–๎‚˜๎‚‰๎‚ˆ๎†ฟ double_Imaginary๎†ฝ
long double_Imaginary ๎‚…๎‚’๎‚ˆ float_Imaginary ๎‡ฆ๎‚“๎‚– ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ double imaginary๎†ฝ long double imaginary
๎‚…๎‚’๎‚ˆ float imaginary ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚–๎‡ง๎‡€
๎€ƒ๎‚— ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚†๎‚…๎‚—๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚… ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚™๎‚—๎‚‰ ๎‚๎‚— ๎‚—๎‚˜๎‚–๎‚“๎‚’๎‚‹๎‚๎‚ ๎‚ˆ๎‚๎‚—๎‚‡๎‚“๎‚™๎‚–๎‚…๎‚‹๎‚‰๎‚ˆ๎‡€
RECO
126
RECOMMENDATION โ€” No use of complex numbers
๎€…๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚— ๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎€…๎ˆ˜๎ˆ˜ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
96 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
11
Conditional and iterative structures
11.1 Use of braces for conditionals and loops
๎€–๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚•๎‚™๎‚๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚… ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚“๎‚– ๎‚… ๎‚๎‚“๎‚“๎‚” ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚๎‚๎‚‘๎‚๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚†๎‚–๎‚…๎‚‡๎‚‰๎‚—๎‡€
๎€ƒ ๎‚๎‚…๎‚‡๎‚ ๎‚“๎‚Š ๎‚†๎‚–๎‚…๎‚‡๎‚‰๎‚— ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚… ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚“๎‚– ๎‚… ๎‚๎‚“๎‚“๎‚” ๎‚‘๎‚“๎‚–๎‚‰ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚–๎‚‰๎‚…๎‚ˆ๎‡€ ๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚–๎‚‘๎‚“๎‚–๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚— ๎‚… ๎‚–๎‚๎‚—๎‚
๎‚“๎‚Š ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚— ๎‚‘๎‚“๎‚ˆ๎‚๎Šฑ๎‚‰๎‚ˆ๎†ฟ ๎‚… ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‡๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚”๎‚…๎‚–๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚๎†ฝ ๎‚๎‚˜ ๎‚›๎‚๎‚๎‚ ๎‚๎‚’๎‚—๎‚˜๎‚‰๎‚…๎‚ˆ ๎‚‰๎‚’๎‚ˆ ๎‚™๎‚” ๎‚“๎‚™๎‚˜๎‚—๎‚๎‚ˆ๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚๎‡€
RULE
127
RULE โ€” Systematic use of braces for conditionals and loops
๎€๎‚‰๎‚š๎‚‰๎‚– ๎‚“๎‚‘๎‚๎‚˜ ๎‚†๎‚–๎‚…๎‚‡๎‚‰๎‚— ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚๎‚๎‚‘๎‚๎‚˜ ๎‚… ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚†๎‚๎‚“๎‚‡๎‚๎‡€ ๎€„๎‚–๎‚…๎‚‡๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚›๎‚–๎‚๎‚˜๎‚˜๎‚‰๎‚’ ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚๎‚๎‚‘๎‚๎‚˜ ๎‚…
๎‚†๎‚๎‚“๎‚‡๎‚ ๎‚“๎‚Š ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚…๎Šน๎‚‰๎‚– ๎‚๎‚“๎‚“๎‚”๎‚— ๎‡ฆ๎‚Š๎‚“๎‚–๎†ฝ ๎‚›๎‚Œ๎‚๎‚๎‚‰๎†ฝ ๎‚ˆ๎‚“๎‡ง ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚๎‚— ๎‡ฆ๎‚๎‚Š๎†ฝ ๎‚‰๎‚๎‚—๎‚‰๎‡ง๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚†๎‚‰๎‚๎‚“๎‚›๎†ฝ ๎‚… ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚ˆ๎‚‰๎‚๎‚๎‚‘๎‚๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚†๎‚–๎‚…๎‚‡๎‚‰๎‚—๎‡€
if (x == 0) /* braces are required , even for a single statement */
printf ("X = 0\n");
/* An indented statement under the printf may visually suggest that the statement
is within the if , but it is not. For a jump statement like "goto", this may
result in a significant portion of the code not being executed . */
if (x != 0) {
if (x < 0) {
...
while (x < 0) {
x++;
...
}
} else {
while (x > 0) {
x--;
...
}
}
}
/* example of โ€™Apples goto fail */
if (err = SSLHashSHA1.update (&hashCtx , &signedParams )) != 0)
goto fail;
goto fail;
/* other checks , but not reachable due to the duplicate goto fail
without braces */
fail :
/* cleaning and releasing of buffers */
return err;
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 97
Good example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚ˆ๎‚‰๎‚๎‚๎‚‘๎‚๎‚˜๎‚— ๎‚…๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚๎‚— ๎‚…๎‚’๎‚ˆ ๎‚๎‚“๎‚“๎‚”๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚†๎‚–๎‚…๎‚‡๎‚‰๎‚—๎‡€
if (0 == x) {
printf ("X = 0\n");
} else {
if (x < 0) {
...
while (x < 0) {
x++;
...
}
} else {
while (x > 0) {
x--;
...
}
}
}
/* example of goto fail - Apple */
if (err = SSLHashSHA1.update (&hashCtx , &signedParams )) != 0) {
goto fail;
}
/* other checks */
fail :
/* cleaning and releasing of buffers */
return err;
11.1.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ”๎‡€๎ˆ• ๎€–๎‚Œ๎‚‰ ๎‚†๎‚“๎‚ˆ๎‚ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚๎‚˜๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‡‚๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚– ๎‚… ๎‚—๎‚‰๎‚๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚…
๎‚‡๎‚“๎‚‘๎‚”๎‚“๎‚™๎‚’๎‚ˆ๎‡‚๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‡๎€š๎€’๎ˆ๎ˆ˜๎‡‚๎€… ๎€—๎‚—๎‚‰ ๎‚†๎‚–๎‚…๎‚‡๎‚‰๎‚— ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚“๎‚ˆ๎‚ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚๎‚Š๎†ฝ ๎‚Š๎‚“๎‚–๎†ฝ ๎‚“๎‚– ๎‚›๎‚Œ๎‚๎‚๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€
11.2 Correct construction and use of switch statements
๎€–๎‚Œ๎‚‰ switch ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚”๎‚–๎‚“๎‚š๎‚๎‚ˆ๎‚‰๎‚— ๎‚…๎‚’ ๎‚‰๎‚๎‚‰๎‚‹๎‚…๎‚’๎‚˜ ๎‚›๎‚…๎‚ ๎‚“๎‚Š ๎‚›๎‚–๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚ˆ๎‚๎‚Š๎‡‚
๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚‡๎‚…๎‚—๎‚‰๎‚— ๎‚†๎‚…๎‚—๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚“๎‚– ๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‡€ ๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚— ๎‚…๎‚๎‚—๎‚“ ๎‚…
๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚“๎‚‘๎‚๎‚˜๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚–๎‚‰๎‚…๎‚ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎†ฟ ๎‚™๎‚’๎‚›๎‚…๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‘๎‚…๎‚ ๎‚†๎‚‰ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚‰๎‚ˆ๎‡€ ๎€‹๎‚’ ๎‚Š๎‚…๎‚‡๎‚˜๎†ฝ
๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚™๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚š๎‚‰ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ switch ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚‰๎‚œ๎‚‡๎‚๎‚™๎‚—๎‚๎‚š๎‚‰๎†ฝ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚‡๎‚…๎‚—๎‚‰๎‚— ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚…๎‚‡๎‚˜๎‚๎‡‚
๎‚š๎‚…๎‚˜๎‚‰๎‚ˆ๎‡€ ๎€‹๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚…๎‚๎‚—๎‚“ ๎‚†๎‚‰ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰๎‚ˆ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚๎‚— ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜
๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚’๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ switch ๎‡ฆ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜ ๎‚‡๎‚…๎‚—๎‚‰๎‡ง๎‡€
RULE
128
RULE โ€” Systematic de๎‚พnition of a default case in switch
๎€ƒ switch-case ๎‚‘๎‚™๎‚—๎‚˜ ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚… ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚ˆ ๎‚๎‚…๎‚—๎‚˜๎‡€
RECO
129
RECOMMENDATION โ€” Use of break in each case of switch state-
ments
๎€„๎‚ ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜๎†ฝ ๎‚… switch-case ๎‚‘๎‚™๎‚—๎‚˜ ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚… ๎‚†๎‚–๎‚‰๎‚…๎‚ ๎‚Š๎‚“๎‚– ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚‡๎‚…๎‚—๎‚‰๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰
๎‚“๎‚Š ๎‚… ๎‚†๎‚–๎‚‰๎‚…๎‚ ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚ˆ๎‚™๎‚”๎‚๎‚๎‚‡๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚— ๎‚˜๎‚“๎‚๎‚‰๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚™๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚๎‚’ ๎‚… ๎‚‡๎‚“๎‚‘๎‡‚
๎‚‘๎‚‰๎‚’๎‚˜๎‡€
98 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
Information
๎€–๎‚Œ๎‚‰ -Wimplicit-fallthrough ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚—
๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚’ ๎‚‰๎‚…๎‚‡๎‚Œ case ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚—๎‚๎‚‘๎‚”๎‚๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚Š๎‚‰๎‚› ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎‡€ ๎€‹๎‚Š ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ ๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚๎‚— ๎‚˜๎‚“ ๎‚†๎‚‰
๎‚‡๎‚…๎‚–๎‚–๎‚๎‚‰๎‚ˆ ๎‚“๎‚™๎‚˜ ๎‚๎‚’ ๎‚… case๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚๎‚— ๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚๎‚— ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜
๎‚†๎‚‰ ๎‚‡๎‚…๎‚๎‚๎‚‰๎‚ˆ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ case๎‡€
RECO
130
RECOMMENDATION โ€” No nesting of control structure in a
switch-case
๎€‡๎‚š๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚“๎‚™๎‚‹๎‚Œ ๎€… ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚๎‚˜๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚‰๎‚—๎‚˜๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚๎‚’๎‚—๎‚๎‚ˆ๎‚‰ ๎‚… switch ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰
๎‚…๎‚š๎‚“๎‚๎‚ˆ๎‚‰๎‚ˆ๎‡€
๎€ˆ๎‚๎‚’๎‚…๎‚๎‚๎‚๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚Š๎‚“๎‚–๎‚†๎‚๎‚ˆ๎‚ˆ๎‚‰๎‚’ ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎†ฝ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚“๎‚– ๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚… switch
๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚–๎‚—๎‚˜ ๎‚๎‚…๎‚†๎‚‰๎‚ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚–๎‚—๎‚˜ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ switch-case๎‡€
RULE
131
RULE โ€” Do not insert statements before the ๎‚พrst label of a
switch-case
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚’๎‚…๎‚ default ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚— ๎‚‘๎‚๎‚—๎‚—๎‚๎‚’๎‚‹๎‡€
switch (var) {
int i = 0; /* not authorized */
case VAL1:
...
break;
case VAL2:
...
case VAL3:
...
break;
/* absence of default and break and no comment to explain the case on the value
VAL2 */
}
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚… ๎‚†๎‚–๎‚‰๎‚…๎‚ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚— ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜ ๎‚Š๎‚“๎‚– ๎‚‰๎‚…๎‚‡๎‚Œ case๎‡€ ๎€–๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚— ๎‚…๎‚๎‚—๎‚“
๎‚… ๎Šฑ๎‚’๎‚…๎‚ ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚๎‚— ๎‚‡๎‚…๎‚–๎‚–๎‚๎‚‰๎‚ˆ ๎‚“๎‚™๎‚˜ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚ˆ๎‚๎‚ˆ ๎‚’๎‚“๎‚˜
๎‚‘๎‚…๎‚˜๎‚‡๎‚Œ ๎‚…๎‚’๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰๎‚—๎‡€
int i = 0; /* displaced declaration and initialisation */
switch (var) {
case VAL1:
...
break;
case VAL2:
...
/* break voluntarily missing */
case VAL3:
...
break;
default :
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 99
...
}
11.2.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ•๎‡€๎ˆ ๎€ƒ๎‚๎‚ ๎‚—๎‚›๎‚๎‚˜๎‚‡๎‚Œ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚›๎‚‰๎‚๎‚๎‡‚๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ•๎‡€๎ˆ‘ ๎€ƒ ๎‚—๎‚›๎‚๎‚˜๎‚‡๎‚Œ ๎‚๎‚…๎‚†๎‚‰๎‚ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚“๎‚’๎‚๎‚ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚“๎‚—๎‚˜ ๎‚‡๎‚๎‚“๎‚—๎‚‰๎‚๎‚๎‡‚๎‚‰๎‚’๎‚‡๎‚๎‚“๎‚—๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚‘๎‚”๎‚“๎‚™๎‚’๎‚ˆ
๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚“๎‚ˆ๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚›๎‚๎‚˜๎‚‡๎‚Œ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ•๎‡€๎ˆ’ ๎€ƒ๎‚’ ๎‚™๎‚’๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚†๎‚–๎‚‰๎‚…๎‚ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚‰ ๎‚‰๎‚š๎‚‰๎‚–๎‚ ๎‚—๎‚›๎‚๎‚˜๎‚‡๎‚Œ๎‡‚๎‚‡๎‚๎‚…๎‚™๎‚—๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ•๎‡€๎ˆ“ ๎€‡๎‚š๎‚‰๎‚–๎‚ ๎‚—๎‚›๎‚๎‚˜๎‚‡๎‚Œ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚… ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜ ๎‚๎‚…๎‚†๎‚‰๎‚๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ•๎‡€๎ˆ• ๎€‡๎‚š๎‚‰๎‚–๎‚ ๎‚—๎‚›๎‚๎‚˜๎‚‡๎‚Œ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚…๎‚˜ ๎‚๎‚‰๎‚…๎‚—๎‚˜ ๎ˆ‘ ๎‚—๎‚›๎‚๎‚˜๎‚‡๎‚Œ๎‡‚๎‚‡๎‚๎‚…๎‚™๎‚—๎‚‰๎‚—๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ•๎‡€๎ˆ– ๎€ƒ ๎‚—๎‚›๎‚๎‚˜๎‚‡๎‚Œ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚… ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚๎‚๎‚ ๎‚†๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚˜๎‚๎‚”๎‚‰๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€†๎€…๎€Ž๎ˆ“๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚๎‚’๎‚—๎‚๎‚ˆ๎‚‰ ๎‚… ๎‚—๎‚›๎‚๎‚˜๎‚‡๎‚Œ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚–๎‚—๎‚˜ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚๎‚…๎‚†๎‚‰๎‚๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€๎€•๎€…๎ˆ๎ˆ๎‡‚๎€… ๎€•๎‚˜๎‚–๎‚๎‚š๎‚‰ ๎‚Š๎‚“๎‚– ๎‚๎‚“๎‚‹๎‚๎‚‡๎‚…๎‚ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚˜๎‚‰๎‚’๎‚‰๎‚—๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€๎€•๎€…๎ˆ๎ˆ–๎‡‚๎€… ๎€ˆ๎‚๎‚’๎‚๎‚—๎‚Œ ๎‚‰๎‚š๎‚‰๎‚–๎‚ ๎‚—๎‚‰๎‚˜ ๎‚“๎‚Š ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚‡๎‚…๎‚—๎‚‰ ๎‚๎‚…๎‚†๎‚‰๎‚ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚†๎‚–๎‚‰๎‚…๎‚ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‡‚
๎‚‘๎‚‰๎‚’๎‚˜๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ“๎ˆ—๎ˆ“๎†ฟ ๎€‘๎‚‘๎‚๎‚˜๎‚˜๎‚‰๎‚ˆ ๎€„๎‚–๎‚‰๎‚…๎‚ ๎€•๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚’ ๎€•๎‚›๎‚๎‚˜๎‚‡๎‚Œ๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‰๎‚ˆ ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜ ๎‚๎‚’ ๎‚… switch ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‡ฌ๎‚—๎‚›๎‚˜๎‚‡๎‚Œ๎‚ˆ๎Šท๎‚˜๎‡ญ๎‡€
11.3 Correct construction of for loops
๎€–๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚–๎‚—๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚๎‚…๎‚—๎‚˜ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚… for ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‡‚
๎‚‘๎‚‰๎‚’๎‚˜๎†ฝ ๎‚—๎‚‰๎‚”๎‚…๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚… ๎‚‡๎‚“๎‚‘๎‚‘๎‚…๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰
๎Šฑ๎‚–๎‚—๎‚˜ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ for๎†ฝ ๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚Œ๎‚๎‚–๎‚ˆ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
for๎‡€ ๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚‘๎‚‘๎‚…๎‡‚๎‚—๎‚‰๎‚”๎‚…๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ for ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚๎‚˜ ๎‚ˆ๎‚๎Šฒ๎‡‚
๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚๎‚— ๎‚… ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚—๎‡€ ๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚๎‚—
๎‚…๎‚๎‚–๎‚‰๎‚…๎‚ˆ๎‚ ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ๎ˆ“๎‡€๎ˆ๎‡€ ๎€‹๎‚Š ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚–๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚“๎‚“๎‚”๎†ฝ
๎‚˜๎‚Œ๎‚‰๎‚ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰๎‚ˆ ๎‚Ž๎‚™๎‚—๎‚˜ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰ for ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€ ๎€‹๎‚Š ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ
๎‚“๎‚– ๎‚ˆ๎‚‰๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‘๎‚“๎‚ˆ๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚“๎‚“๎‚”๎‡€
๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚–๎‚‘๎‚“๎‚–๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚•๎‚™๎‚๎‚–๎‚‰ ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‡ฆ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚—๎‚˜๎‚“๎‚” ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ
๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡š๎‚ˆ๎‚‰๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡ง ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ for ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚˜๎‚‰๎‚ˆ๎‡€ ๎€‹๎‚˜ ๎‚๎‚— ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚๎‚‰๎‚…๎‚š๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’
๎‚‰๎‚‘๎‚”๎‚˜๎‚๎†ฝ ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚“๎‚– ๎‚‰๎‚š๎‚‰๎‚’ ๎‚˜๎‚“ ๎‚๎‚‰๎‚…๎‚š๎‚‰ ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‰๎‚‘๎‚”๎‚˜๎‚๎†ฝ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚๎‚’ ๎‚…๎‚’ ๎‚๎‚’๎Šฑ๎‚’๎‚๎‚˜๎‚‰ ๎‚๎‚“๎‚“๎‚”๎‡€ ๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰
๎‚“๎‚Š ๎‚…๎‚’ ๎‚๎‚’๎Šฑ๎‚’๎‚๎‚˜๎‚‰ ๎‚๎‚“๎‚“๎‚”๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚–๎‚‘ while (1) { ... } ๎‚๎‚— ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚”๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚–๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚–๎‚‘ for(;;) { ... }๎‡€
RULE
132
RULE โ€” Correct construction of for loops
๎€‡๎‚…๎‚‡๎‚Œ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚… for ๎‚๎‚“๎‚“๎‚” ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚‰๎‚œ๎‚…๎‚‡๎‚˜๎‚๎‚ ๎‚“๎‚’๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€
๎€–๎‚Œ๎‚™๎‚— ๎‚… for ๎‚๎‚“๎‚“๎‚” ๎‚‘๎‚™๎‚—๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚…๎‚’ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚๎‚˜๎‚— ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚–๎†ฝ ๎‚… ๎‚—๎‚˜๎‚“๎‚” ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚’ ๎‚๎‚˜๎‚—
๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚–๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚… ๎‚๎‚“๎‚“๎‚” ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚– ๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚– ๎‚ˆ๎‚‰๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚‘๎‚… ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚—๎‚‰๎‚”๎‚…๎‚–๎‚…๎‚˜๎‚‰ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ
๎‚‘๎‚“๎‚ˆ๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚–๎‚—๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚๎‚–๎‚ˆ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ for๎‡€ ๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ for
๎‚๎‚“๎‚“๎‚”๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚ˆ ๎‚†๎‚ while() { ... } ๎‚“๎‚– do { ... } while () ๎‚๎‚“๎‚“๎‚”๎‚—๎‡€
100 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
#define MAX_LOOP 10U
for (;;) {
data = read();
if (0 == data) {
break;
}
...
}
for (i = 0;;) { /* missing elements */
max = some_function(i);
i++;
if (i >= max) {
break;
}
}
...
for (i = 0, a = 0; i < MAX_LOOP; i++, a += 2) {
...
some_function (a);
...
}
Good example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚’๎‚๎‚ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚“๎‚“๎‚” ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚– ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚–๎‚—๎‚˜ for ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚“๎‚’๎‚๎‚
๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚– ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚Œ๎‚๎‚–๎‚ˆ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚– ๎‚๎‚“๎‚“๎‚”๎‚— ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚…๎‚๎‚ ๎‚๎‚˜๎‚— ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚—
๎‡ฆ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚—๎‚˜๎‚“๎‚” ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚– ๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡ง๎‡€
#define MAX_LOOP 10U
for (i = 0; i < arraySize; i ++) {
...
dataArray [i] = some_function(i);
...
}
while (1) {
data = read();
if (0 == data) {
break;
}
...
}
i = 0;
do {
max = some_function(i);
i++;
}
while (i < max);
...
a = 0;
for (i = 0; i < MAX_LOOP; i++) {
...
some_function (a);
...
a += 2;
}
11.3.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ“๎‡€๎ˆ‘ ๎€ƒ ๎‚Š๎‚“๎‚– ๎‚๎‚“๎‚“๎‚” ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚›๎‚‰๎‚๎‚๎‡‚๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ”๎‡€๎ˆ• ๎€–๎‚Œ๎‚‰ ๎‚†๎‚“๎‚ˆ๎‚ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚๎‚˜๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‡‚๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚– ๎‚… ๎‚—๎‚‰๎‚๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚…
๎‚‡๎‚“๎‚‘๎‚”๎‚“๎‚™๎‚’๎‚ˆ๎‡‚๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‡๎€š๎€’๎ˆ๎ˆ”๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚”๎‚๎‚…๎‚‡๎‚‰ ๎‚… ๎‚—๎‚‰๎‚‘๎‚๎‚‡๎‚“๎‚๎‚“๎‚’ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚๎‚๎‚’๎‚‰ ๎‚…๎‚— ๎‚…๎‚’ ๎‚๎‚Š๎†ฝ ๎‚Š๎‚“๎‚–๎†ฝ ๎‚“๎‚– ๎‚›๎‚Œ๎‚๎‚๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‡๎€š๎€’๎ˆ๎ˆ˜๎‡‚๎€… ๎€—๎‚—๎‚‰ ๎‚†๎‚–๎‚…๎‚‡๎‚‰๎‚— ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚“๎‚ˆ๎‚ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚๎‚Š๎†ฝ ๎‚Š๎‚“๎‚–๎†ฝ ๎‚“๎‚– ๎‚›๎‚Œ๎‚๎‚๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 101
11.4 Changing of a for loop counter forbidden in the body of
the loop
๎€…๎‚Œ๎‚…๎‚’๎‚‹๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚– ๎‚“๎‚Š ๎‚… for ๎‚๎‚“๎‚“๎‚” ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚“๎‚“๎‚” ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚๎‚˜ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚…๎‚๎‚’๎‚˜๎‚…๎‚๎‚’
๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€ ๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚–๎‚‘๎‚“๎‚–๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‡๎‚…๎‚’ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚๎‚’๎Šฑ๎‚’๎‚๎‚˜๎‚‰ ๎‚๎‚“๎‚“๎‚” ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚“๎‚“๎‚”
๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚…๎‚’ ๎‚‰๎‚•๎‚™๎‚…๎‚๎‚๎‚˜๎‚ ๎‚“๎‚– ๎‚๎‚’๎‚‰๎‚•๎‚™๎‚…๎‚๎‚๎‚˜๎‚๎‡€ ๎€‹๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚“๎‚“๎‚” ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚‰๎‚— ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚— ๎‚…๎‚๎‚—๎‚“ ๎‚… ๎‚–๎‚๎‚—๎‚ ๎‚“๎‚Š ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‡€
๎€–๎‚Œ๎‚‰ ๎‚๎‚“๎‚“๎‚” ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚– ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚“๎‚’๎‚๎‚ ๎‚†๎‚‰ ๎‚‘๎‚“๎‚ˆ๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚Œ๎‚๎‚–๎‚ˆ ๎‚”๎‚…๎‚–๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ for ๎‚๎‚“๎‚“๎‚”๎‡€
๎€‹๎‚˜ ๎‚๎‚— ๎‚‡๎‚“๎‚‘๎‚‘๎‚“๎‚’ ๎‚˜๎‚“ ๎‚‘๎‚“๎‚ˆ๎‚๎‚Š๎‚ ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚“๎‚ˆ๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚“๎‚“๎‚” ๎‚… ๎Šท๎‚…๎‚‹ ๎‚“๎‚– ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚š๎‚‰๎‚’๎‚‰๎‚— ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰
๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚—๎‚˜๎‚“๎‚” ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ for ๎‚๎‚“๎‚“๎‚”๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚—๎‚‡๎‚‰๎‚’๎‚…๎‚–๎‚๎‚“ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚…
break ๎‚…๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚๎‚˜ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚“๎‚“๎‚”๎‡€
RULE
133
RULE โ€” Change to a counter of a for loop forbidden in the body of the
loop
๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚– ๎‚“๎‚Š ๎‚… for ๎‚๎‚“๎‚“๎‚” ๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰๎‚ˆ ๎‚๎‚’๎‚—๎‚๎‚ˆ๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚“๎‚ˆ๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ for ๎‚๎‚“๎‚“๎‚”๎‡€
Bad example
๎€–๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚†๎‚‰๎‚๎‚“๎‚› ๎‚—๎‚Œ๎‚“๎‚›๎‚— ๎‚… for ๎‚๎‚“๎‚“๎‚” ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚‘๎‚“๎‚ˆ๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚๎‚˜๎‚— ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚– ๎‚๎‚’ ๎‚๎‚˜๎‚— ๎‚†๎‚“๎‚ˆ๎‚๎‡€
๎€–๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚— ๎‚… ๎‚–๎‚๎‚—๎‚ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚๎‚’๎Šฑ๎‚’๎‚๎‚˜๎‚‰ ๎‚๎‚“๎‚“๎‚”๎‡€
#define MAX_LOOP 10U
for (i = 0; i != MAX_LOOP; i ++) {
...
if (...) {
/* if the condition is met with i == 9, we end up in an infinite loop . */
i++;
}
...
}
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚’๎‚“ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚– ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚“๎‚ˆ๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ for
๎‚๎‚“๎‚“๎‚”๎‡€
#define MAX_LOOP 10U
for (i = 0; i < MAX_LOOP; i++) {
...
if (some_function ()) {
break; /* loop stopped */
}
...
}
11.4.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ“๎‡€๎ˆ‘
102 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€ƒ๎€”๎€”๎ˆ’๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚Š๎‚“๎‚–๎‚‘ ๎‚“๎‚– ๎‚™๎‚—๎‚‰ ๎‚“๎‚™๎‚˜๎‡‚๎‚“๎‚Š๎‡‚๎‚†๎‚“๎‚™๎‚’๎‚ˆ๎‚— ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚– ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚—๎‚™๎‚†๎‚—๎‚‡๎‚–๎‚๎‚”๎‚˜๎‚—๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 103
12
Jumps in the code
12.1 Do not use backward goto
๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚†๎‚…๎‚‡๎‚๎‚›๎‚…๎‚–๎‚ˆ goto ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚”๎‚–๎‚“๎‚“๎‚Š๎‚–๎‚‰๎‚…๎‚ˆ๎‚๎‚’๎‚‹ ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚…๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚…๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚š๎‚‰๎‚–๎‚ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚๎‚—
๎‚… ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— ๎‚™๎‚’๎‚›๎‚…๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’๎Šฑ๎‚’๎‚๎‚˜๎‚‰ ๎‚๎‚“๎‚“๎‚”๎‚—๎‡€ ๎€‹๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚’๎‚‰๎‚‰๎‚ˆ ๎‚…๎‚–๎‚๎‚—๎‚‰๎‚—๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚†๎‚‰๎‚‡๎‚…๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚๎‚‹๎‚“๎‚–๎‚๎‚˜๎‚Œ๎‚‘
๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚‡๎‚˜๎‚™๎‚…๎‚๎‚๎‚ ๎‚‡๎‚“๎‚‘๎‚”๎‚–๎‚๎‚—๎‚‰๎‚— ๎‚… ๎‚๎‚“๎‚“๎‚”๎‡€ ๎€–๎‚Œ๎‚‰๎‚’ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚”๎‚–๎‚“๎‚”๎‚“๎‚—๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰
๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚“๎‚“๎‚”๎‚—๎†ฝ ๎‚๎‚’ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚›๎‚“๎‚–๎‚ˆ๎‚— for๎†ฝ while ๎‚“๎‚– do-while๎‡€
RULE
134
RULE โ€” No use of backward goto
๎€’๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š goto ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚–๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚… ๎‚๎‚…๎‚†๎‚‰๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚—
๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚๎‚— goto ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€
Bad example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚— ๎‚… ๎‚†๎‚…๎‚‡๎‚๎‚›๎‚…๎‚–๎‚ˆ goto๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚‡๎‚Œ๎‚“๎‚๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’
๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚…๎‚—๎‚—๎‚‰๎‚‘๎‚†๎‚๎‚‰๎‚– ๎‚…๎‚”๎‚”๎‚–๎‚“๎‚…๎‚‡๎‚Œ๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚˜๎‚…๎‚๎‚‰ ๎‚…๎‚ˆ๎‚š๎‚…๎‚’๎‚˜๎‚…๎‚‹๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Œ๎‚๎‚‹๎‚Œ๎‚‰๎‚–
๎‚๎‚‰๎‚š๎‚‰๎‚ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚๎‚๎‚˜๎‚๎‚‰๎‚— ๎‚“๎Šฎ๎‚‰๎‚–๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰๎‡€
#define BUFFER_SIZE 100U
void foo() {
uint8_t s[BUFFER_SIZE ];
uint8_t x = 0;
my_loop :
s[x] = x;
x++;
if(x < BUFFER_SIZE )
{
goto my_loop; /* backward goto prohibited */
}
}
Good example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚™๎‚—๎‚‰๎‚— ๎‚… ๎‚๎‚“๎‚“๎‚” ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‡€ ๎€–๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚— ๎‚’๎‚“ ๎‚’๎‚‰๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚…
๎‚†๎‚…๎‚‡๎‚๎‚›๎‚…๎‚–๎‚ˆ goto๎‡€
#define BUFFER_SIZE 100U
void foo() {
uint8_t s[BUFFER_SIZE ];
uint8_t x = 0;
for(x = 0; x < BUFFER_SIZE; x++) {
s[x] = x;
}
}
104 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
12.1.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ”๎‡€๎ˆ ๎€–๎‚Œ๎‚‰ ๎‚‹๎‚“๎‚˜๎‚“ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
12.2 Limited use of forward goto
๎€–๎‚Œ๎‚‰ forward goto ๎‚‡๎‚…๎‚’ ๎‚—๎‚๎‚‘๎‚”๎‚๎‚๎‚Š๎‚ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‘๎‚…๎‚’๎‚…๎‚‹๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚–๎‚‰๎‚ˆ๎‚™๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚‰๎‚œ๎‚๎‚˜ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚— ๎‚Š๎‚–๎‚“๎‚‘ ๎‚…
๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… forward goto ๎‚“๎‚™๎‚˜๎‚—๎‚๎‚ˆ๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎†ฝ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‘๎‚…๎‚ ๎‚†๎‚‰ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚‰๎‚ˆ
๎‚›๎‚๎‚˜๎‚Œ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰๎‚‰๎‚’ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰๎‚ˆ๎‡€ ๎€ƒ๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚—๎‚‰๎‚–๎‚๎‚“๎‚™๎‚— ๎‚‡๎‚“๎‚’๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰ ๎‚๎‚—๎†ฝ ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ
๎‚Š๎‚“๎‚–๎‚‹๎‚‰๎‚˜๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚Š๎‚–๎‚‰๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚“๎‚– ๎‚–๎‚‰๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰๎‚— ๎‚…๎‚‘๎‚“๎‚’๎‚‹ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚˜๎‚Œ๎‚๎‚’๎‚‹๎‚—๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚†๎‚‰ ๎‚‘๎‚“๎‚ˆ๎‚๎Šฑ๎‚‰๎‚ˆ
๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š goto๎‡€
๎€–๎‚Œ๎‚‰ forward goto ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚“๎‚’๎‚๎‚ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‘๎‚…๎‚’๎‚…๎‚‹๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚๎‚…๎‚†๎‚‰๎‚๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰
๎‚๎‚‰๎‚”๎‚˜ ๎‚˜๎‚“ ๎‚… ๎‚‘๎‚๎‚’๎‚๎‚‘๎‚™๎‚‘๎‡€
RECO
135
RECOMMENDATION โ€” Limited use of forward goto
๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚“๎‚–๎‚›๎‚…๎‚–๎‚ˆ goto ๎‚๎‚— ๎‚˜๎‚“๎‚๎‚‰๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚’๎‚๎‚ ๎‚๎‚’ ๎‚‡๎‚…๎‚—๎‚‰๎‚— ๎‚›๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚˜ ๎‚…๎‚๎‚๎‚“๎‚›๎‚—๎†ฟ
n
๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚‰๎‚œ๎‚๎‚˜ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚— ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚—๎‚๎‚‹๎‚’๎‚๎Šฑ๎‚‡๎‚…๎‚’๎‚˜๎‚๎‚ ๎‚๎‚๎‚‘๎‚๎‚˜๎‚‰๎‚ˆ๎†พ
n
๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚‘๎‚™๎‚‡๎‚Œ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚–๎‚‰๎‚…๎‚ˆ๎‚…๎‚†๎‚๎‚‰๎‡€
๎€–๎‚Œ๎‚‰ ๎‚๎‚…๎‚†๎‚‰๎‚๎‡ฆ๎‚—๎‡ง ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ goto ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
Good example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚… ๎‚Š๎‚“๎‚–๎‚›๎‚…๎‚–๎‚ˆ goto๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚™๎‚—๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚”๎‚–๎‚“๎‡‚
๎‚”๎‚“๎‚—๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰๎‡€
#define BUFFER_LEN (128 U)
int32_t my_function (int32_t a) {
FILE * f = NULL;
uint8_t *buffer = NULL;
int32_t result = ERR_UNDEFINED ;
f = fopen("/my/path", "r");
if(NULL == f) {
result = ERR_FOPEN;
} else {
buffer = (uint8_t *) malloc(BUFFER_LEN * sizeof(uint8_t));
if(NULL == buffer ) {
result = ERR_MALLOC;
}
else
{
...
free (buffer);
buffer = NULL;
result = ERR_NOERROR;
}
fclose (f);
f = NULL;
}
return result;
}
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 105
Tolerated example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚™๎‚—๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚–๎‚›๎‚…๎‚–๎‚ˆ goto ๎‚Š๎‚“๎‚– ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‘๎‚…๎‚’๎‚…๎‚‹๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚—๎‚‡๎‚‰๎‚’๎‚…๎‚–๎‚๎‚“
๎‚๎‚— ๎‚˜๎‚“๎‚๎‚‰๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ๎‡€
int32_t my_function (int32_t a) {
FILE * f = NULL;
uint8_t *buffer = NULL;
int32_t result = ERR_UNDEFINED ;
f = fopen("/my/path", "r");
if(NULL == f) {
result = ERR_FOPEN;
goto cleanup;
}
buffer =( uint8_t *)malloc(BUFFER_LEN * sizeof ( uint8_t));
if(NULL == buffer ) {
result = ERR_MALLOC;
goto cleanup;
}
...
result = ERR_NOERROR;
cleanup :
if(NULL != f) {
fclose (f);
f = NULL;
}
if(NULL != buffer ) {
free (buffer);
buffer = NULL;
}
return result;
}
12.2.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ”๎‡€๎ˆ ๎ˆ๎ˆ”๎‡€๎ˆ”๎‡€
106 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
13
Functions
13.1 Correct and consistent declaration and de๎‚พnition
Declaration/Prototype of function
๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚– ๎‚๎‚˜๎‚— prototype ๎‚๎‚— ๎‚… ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚— ๎‚˜๎‚Œ๎‚–๎‚‰๎‚‰ ๎‚‰๎‚๎‡‚
๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎†ฟ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚๎‚˜๎‚— ๎‚’๎‚…๎‚‘๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚๎‚—๎‚˜ ๎‚“๎‚Š ๎‚๎‚˜๎‚— ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎†ฝ ๎‚Š๎‚“๎‚๎‡‚
๎‚๎‚“๎‚›๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚… ๎‚—๎‚‰๎‚‘๎‚๎‚‡๎‚“๎‚๎‚“๎‚’๎‡€
De๎‚พnition of function
๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚“๎‚ˆ๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ i.e. ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚˜ ๎‚“๎‚Š ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚๎‚˜
๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚‰๎‚—๎‡€ ๎€ƒ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚๎‚—๎‚“ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚— ๎‚… ๎‚”๎‚–๎‚“๎‚˜๎‚“๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
๎€…๎ˆ˜๎ˆ ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚›๎‚Œ๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚๎‚˜ ๎‚†๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚–
๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€…๎ˆ˜๎ˆ˜ ๎‚๎‚— ๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚๎‚‘๎‚”๎‚“๎‚—๎‚‰๎‚— ๎‚…๎‚˜ ๎‚๎‚‰๎‚…๎‚—๎‚˜ ๎‚“๎‚’๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚–๎‡€
๎€–๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰๎†ฝ ๎‚๎‚’ ๎‚๎‚˜๎‚— ๎‚—๎‚™๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚š๎‚‰ ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚”๎‚–๎‚“๎‚”๎‚“๎‚—๎‚‰๎‚— ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚Š๎‚“๎‚–๎‚‘๎‚— ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎€…๎‚“๎‚‘๎‚†๎‚๎‚’๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚Š๎‚“๎‚–๎‚‘๎‚— ๎‚“๎‚Š ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚‰๎‚ˆ ๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚๎‚— ๎‚–๎‚๎‚—๎‚๎‚— ๎‚–๎‚‰๎‡‚
๎‚—๎‚™๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚๎‚’ ๎‚… ๎‚‘๎‚™๎‚‡๎‚Œ ๎‚๎‚‰๎‚—๎‚— ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚‰ ๎‚…๎‚’๎‚…๎‚๎‚๎‚—๎‚๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚๎‚‰๎‚…๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘๎‚— ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚‰๎‚ˆ๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚๎‚๎‚’๎‚๎‚—๎‡€
Information
๎€…๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‚–๎‚…๎‚๎‚—๎‚‰ ๎‚… ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹ ๎‡ฆ-Wimplicit-function-declaration ๎‚“๎‚– -Wimplicit-int
๎‚“๎‚– -Wreturn-type๎‡ง๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚…๎‚—๎‚—๎‚™๎‚‘๎‚‰ ๎‚…๎‚’ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚‰๎‚œ๎‚˜๎‚‰๎‚–๎‚’ ๎‚๎‚’๎‚˜ ๎‚˜๎‚๎‚”๎‚‰๎†ฝ i.e. ๎‚†๎‚ ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜๎†ฝ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’
๎‚’๎‚“๎‚˜ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚˜๎‚๎‚”๎‚‰ ๎‚Œ๎‚…๎‚— ๎‚…๎‚’ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰๎‡€
RULE
136
RULE โ€” Any (non-static) function de๎‚พned must have a function declara-
tion/prototype
RULE
137
RULE โ€” The prototype declaration of a function must be consistent with
its de๎‚พnition
๎€–๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚“๎‚Š ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 107
RULE
138
RULE โ€” Every function must have an explicit return type and parameter
list associated with it
๎€‡๎‚…๎‚‡๎‚Œ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚˜๎‚๎‚”๎‚‰๎‡€ ๎€ˆ๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚… ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’
๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… void ๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‡€ ๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚›๎‚…๎‚๎†ฝ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’
๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚… ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ void ๎‚…๎‚— ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‡€
๎€ƒ๎‚‡๎‚˜๎‚๎‚š๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎Šฑ๎‚’๎‚ˆ ๎‚“๎‚™๎‚˜ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ
๎‡ฆ๎‚‘๎‚๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚˜๎‚๎‚”๎‚‰๎†ฝ ๎‚๎‚’๎‚‡๎‚“๎‚’๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚’๎‚‡๎‚ ๎‚“๎‚Š ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’๎‡ง๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚†๎‚‰๎‚๎‚“๎‚›๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚— ๎‚‘๎‚๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚Š๎‚“๎‚– ๎‚… ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚›๎‚๎‚˜๎‚Œ๎‡‚
๎‚“๎‚™๎‚˜ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚— ๎‚…๎‚’ ๎‚๎‚’๎‚‡๎‚“๎‚’๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚’๎‚‡๎‚ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰
๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’๎‡€
/* header .h */
foo( uint8_t a); /* the return type is missing */
uint32_t bar(uint16_t b);
void car(); /* void is missing in the parameter type to indicate that the
function does not take parameters */
/* file.c */
foo( uint8_t a) {
...
}
uint32_t bar(int32_t b) { /* after the declaration , b must be a uint16_t */
...
}
void car() {
...
}
Good example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚… ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
/* header .h */
void foo( uint8_t a);
uint32_t bar(uint16_t b);
void car( void );
/* file.c */
void foo( uint8_t a) {
...
}
uint32_t bar(uint16_t b) {
...
}
void car( void ) {
...
}
13.1.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ—๎‡€๎ˆ ๎€–๎‚๎‚”๎‚‰๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ—๎‡€๎ˆ‘ ๎€ˆ๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚๎‚’ ๎‚”๎‚–๎‚“๎‚˜๎‚“๎‚˜๎‚๎‚”๎‚‰ ๎‚Š๎‚“๎‚–๎‚‘ ๎‚›๎‚๎‚˜๎‚Œ ๎‚’๎‚…๎‚‘๎‚‰๎‚ˆ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚—
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ—๎‡€๎ˆ’ ๎€ƒ๎‚๎‚ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚…๎‚’ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚“๎‚– ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚๎‚”๎‚‰
๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚–๎‚—
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ–๎‡€๎ˆ’ ๎€ƒ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚
108 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ–๎‡€๎ˆ” ๎€–๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚… ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚…๎‚’
๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚…๎‚’ ๎‚…๎‚”๎‚”๎‚–๎‚“๎‚”๎‚–๎‚๎‚…๎‚˜๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚—
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ๎ˆ–๎‡‚๎€… ๎€‹๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚”๎‚”๎‚–๎‚“๎‚”๎‚–๎‚๎‚…๎‚˜๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚’ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚—
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ‘๎ˆ๎‡‚๎€… ๎€‡๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚Š๎‚ ๎‚š๎‚“๎‚๎‚ˆ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚‡๎‚‡๎‚‰๎‚”๎‚˜๎‚— ๎‚’๎‚“ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚—
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€†๎€…๎€Ž๎ˆ’๎ˆ๎‡‚๎€… ๎€†๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚– ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰๎‚‘
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ‘๎ˆ— ๎€ˆ๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚๎‚ ๎‚›๎‚๎‚˜๎‚Œ ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚—
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€—๎‚—๎‚๎‚’๎‚‹ ๎‚… ๎‚˜๎‚…๎‚๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚…๎‚— ๎‚…๎‚’ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚™๎‚’๎‚”๎‚–๎‚“๎‚˜๎‚“๎‚˜๎‚๎‚”๎‚‰๎‚ˆ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‡ฌ๎‚˜๎‚…๎‚๎‚’๎‚˜๎‚’๎‚“๎‡‚
๎‚”๎‚–๎‚“๎‚˜๎‚“๎‡ญ๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€…๎‚…๎‚๎‚๎‚๎‚’๎‚‹ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‡ฌ๎‚…๎‚–๎‚‹๎‚‡๎‚“๎‚‘๎‚”๎‡ญ๎‡€
13.2 Documentation of functions
๎€‹๎‚’๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚˜๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚’ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚—๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‡‚
๎‚˜๎‚๎‚“๎‚’๎‚…๎‚๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚… ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚‰ ๎‚ˆ๎‚‰๎‚—๎‚‡๎‚–๎‚๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚Ž๎‚™๎‚—๎‚˜๎‚๎‚Š๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚—๎‚—๎‚…๎‚‹๎‚‰๎‚— ๎‚†๎‚ ๎‚š๎‚…๎‚๎‚™๎‚‰
๎‚“๎‚– ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚…๎‚๎‚—๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
Passing of parameter by value or copying
๎€™๎‚Œ๎‚‰๎‚’ ๎‚”๎‚…๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚… ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚†๎‚ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‡ฆ๎‚“๎‚– ๎‚†๎‚ ๎‚‡๎‚“๎‚”๎‚๎‚๎‚’๎‚‹๎‡ง๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚‡๎‚˜๎‚™๎‚…๎‚ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜
๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚๎‚ ๎‚๎‚— ๎‚—๎‚‰๎‚’๎‚˜ ๎‡ฆ๎‚‡๎‚“๎‚”๎‚๎‚‰๎‚ˆ๎‡ง ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚๎‚๎‚‰๎‚ˆ
๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜ ๎‚‡๎‚“๎‚’๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰ ๎‚๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚’๎‚ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚˜๎‚“ ๎‚… ๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚—
๎‚’๎‚“๎‚˜ ๎‚”๎‚–๎‚“๎‚”๎‚…๎‚‹๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚‡๎‚˜๎‚™๎‚…๎‚ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‡€
Passing of parameters by reference or pointer
๎€™๎‚Œ๎‚‰๎‚’ ๎‚”๎‚…๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚… ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚†๎‚ ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚‡๎‚˜๎‚™๎‚…๎‚ ๎‚…๎‚–๎‡‚
๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚๎‚ ๎‚๎‚— ๎‚—๎‚‰๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚๎‚๎‚‰๎‚ˆ
๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜ ๎‚‡๎‚“๎‚’๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰ ๎‚๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚’๎‚ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚˜๎‚“ ๎‚… ๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜ ๎‚›๎‚๎‚๎‚
๎‚†๎‚‰ ๎‚”๎‚–๎‚“๎‚”๎‚…๎‚‹๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚‡๎‚˜๎‚™๎‚…๎‚ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‡€
RECO
139
RECOMMENDATION โ€” Documentation of functions
๎€ƒ๎‚๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚—๎†ฟ
n
๎‚… ๎‚ˆ๎‚‰๎‚—๎‚‡๎‚–๎‚๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚‡๎‚…๎‚–๎‚–๎‚๎‚‰๎‚ˆ ๎‚“๎‚™๎‚˜๎†พ
n
๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‡ฆ๎‚๎‚’๎‚”๎‚™๎‚˜๎†ฝ ๎‚“๎‚™๎‚˜๎‡‚
๎‚”๎‚™๎‚˜๎†ฝ ๎‚๎‚’๎‚”๎‚™๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚“๎‚™๎‚˜๎‚”๎‚™๎‚˜๎‡ง ๎‚…๎‚’๎‚ˆ ๎‚…๎‚’๎‚ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚‰๎‚œ๎‚๎‚—๎‚˜๎‚๎‚’๎‚‹ ๎‚“๎‚’ ๎‚๎‚˜๎†พ
n
๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚—๎‚‡๎‚–๎‚๎‚†๎‚‰๎‚ˆ๎‡€
๎€–๎‚Œ๎‚๎‚— ๎‚…๎‚๎‚—๎‚“ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚Š๎‚“๎‚– ๎‚”๎‚–๎‚“๎‚”๎‚‰๎‚– ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚˜๎‚“๎‚˜๎‚๎‚”๎‚‰๎†ฝ
๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚…๎‚๎‚๎‚ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚†๎‚๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‡ฆ๎€Ž๎‚๎‚’๎‚™๎‚œ๎†ฝ ๎€™๎‚๎‚’๎‚ˆ๎‚“๎‚›๎‚—๎‡ง๎‡€
RECO
140
RECOMMENDATION โ€” Specify call conditions for each function
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 109
13.3 Validation of input parameters
๎€’๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚‡๎‚…๎‚’ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚๎‚’๎‚š๎‚…๎‚๎‚๎‚ˆ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚†๎‚‰๎‚๎‚’๎‚‹ ๎‚”๎‚…๎‚—๎‚—๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚—๎‡€ ๎€‹๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚— ๎‚’๎‚“
๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚š๎‚…๎‚๎‚๎‚ˆ๎‚๎‚˜๎‚ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ๎‡€
๎€‹๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚˜๎‚“ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎†ฟ
n
๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚‡๎‚“๎‚’๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚’๎‚‡๎‚ ๎‡ฆ๎‚’๎‚“๎‚’๎‡‚๎‚’๎‚™๎‚๎‚๎†ฝ ๎‚…๎‚๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜๎†ฝ etc.๎‡ง๎†พ
n
๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‡ฆ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚–๎‚…๎‚’๎‚‹๎‚‰๎‚—๎†ฝ etc.๎‡ง๎‡€
๎€•๎‚“๎‚‘๎‚‰ ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚๎‚‡ ๎‚‘๎‚‰๎‚…๎‚—๎‚™๎‚–๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚ˆ๎†ฝ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎‡€
RULE
141
RULE โ€” The validity of all the parameters of a function must systematically
be questioned
๎€–๎‚Œ๎‚๎‚— ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚—๎†ฟ
n
๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚๎‚ˆ๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚—๎‚‰๎‚— ๎‚Š๎‚“๎‚– ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡‚๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ ๎‡ฆ๎‚”๎‚“๎‚๎‚’๎‚˜๎‡‚
๎‚‰๎‚–๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚’๎‚“๎‚’๎‡‚๎‚’๎‚™๎‚๎‚๎†ฝ ๎‚”๎‚–๎‚“๎‚”๎‚‰๎‚–๎‚๎‚ ๎‚…๎‚๎‚๎‚‹๎‚’๎‚‰๎‚ˆ๎†ฝ etc.๎‡ง๎†พ
n
๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚†๎‚‰๎‚๎‚“๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚ˆ๎‚“๎‚‘๎‚…๎‚๎‚’๎‡€
๎€–๎‚Œ๎‚๎‚— ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚— ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‡ฆcf. ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ๎ˆ’๎‡€๎ˆ‘๎‡ง ๎‚…๎‚— ๎‚›๎‚‰๎‚๎‚ ๎‚…๎‚— ๎‚˜๎‚“
๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚๎‚ˆ๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ๎‡€
double division(int32_t n, int32_t d) {
/* d != 0 not verified */
return (( double )n) / (( double)d);
}
Good example
๎€‡๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎ˆ๎†ฟ
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚Œ๎‚“๎‚›๎‚— ๎‚…๎‚’ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚›๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚๎‚ˆ๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚๎‚—
๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ๎†ฟ
double division(int32_t n, int32_t d) {
double res = 0.0;
if (0 == d) {
/* error handling */
}
else {
res = (( double)n) / (( double )d);
}
return res;
}
๎€‡๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎ˆ‘๎†ฟ
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚Œ๎‚“๎‚›๎‚— ๎‚… ๎‚—๎‚‰๎‚‡๎‚“๎‚’๎‚ˆ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚›๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚๎‚ˆ๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚๎‚—
๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ๎†ฟ
uint8_t encrypt(uint8_t *output , int32_t * output_len ,
const uint8_t *input , const int32_t input_len ,
encrypted_ctx *ctx) {
uint8_t err = 0;
110 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
if (( NULL == output ) || (NULL == output_len) || (NULL == encrypted_ctx)) {
err++; /* error handling */
}
if (( NULL == input ) || ( input_len <= 0) || (input_len > MAX_INPUT_LEN)) {
err++; /* error handling */
}
if (0 == err) {
/* API code */
}
return err;
}
13.3.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€–๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚๎‚ˆ๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚”๎‚…๎‚—๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€†๎‚๎‚– ๎ˆ“๎‡€๎ˆ ๎€”๎‚™๎‚’๎‡‚๎‚˜๎‚๎‚‘๎‚‰ ๎‚Š๎‚…๎‚๎‚๎‚™๎‚–๎‚‰๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚‘๎‚๎‚’๎‚๎‚‘๎‚๎‚ž๎‚‰๎‚ˆ
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€ƒ๎€’๎€‹๎ˆ๎ˆ๎‡‚๎€… ๎€ˆ๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚š๎‚…๎‚๎‚๎‚ˆ๎‚…๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚—
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€ƒ๎€”๎€”๎ˆ’๎ˆ—๎‡‚๎€… ๎€‰๎‚™๎‚…๎‚–๎‚…๎‚’๎‚˜๎‚‰๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚Š๎‚“๎‚–๎‚‘ ๎‚๎‚’๎‚š๎‚…๎‚๎‚๎‚ˆ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€๎€‡๎€๎ˆ๎ˆ๎‡‚๎€… ๎€†๎‚‰๎Šฑ๎‚’๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚™๎‚—๎‚‰ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚š๎‚…๎‚๎‚๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ‘๎ˆ ๎€‹๎‚’๎‚—๎‚™๎Šฒ๎‚‡๎‚๎‚‰๎‚’๎‚˜ ๎‚๎‚’๎‚”๎‚™๎‚˜ ๎‚š๎‚…๎‚๎‚๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ‘๎ˆ— ๎€ˆ๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚๎‚ ๎‚›๎‚๎‚˜๎‚Œ ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ—๎ˆ• ๎€ˆ๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚๎‚ ๎‚›๎‚๎‚˜๎‚Œ ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜ ๎‚˜๎‚๎‚”๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ—๎ˆ– ๎€ˆ๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚๎‚ ๎‚›๎‚๎‚˜๎‚Œ ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€…๎‚…๎‚๎‚๎‚๎‚’๎‚‹ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‡ฌ๎‚…๎‚–๎‚‹๎‚‡๎‚“๎‚‘๎‚”๎‡ญ๎‡€
13.4 Use of the quali๎‚พer const for pointer-type function
parameters
๎€™๎‚Œ๎‚‰๎‚’ ๎‚–๎‚‰๎‚…๎‚ˆ๎‚๎‚’๎‚‹ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚–๎‚“๎‚˜๎‚“๎‚˜๎‚๎‚”๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡‚๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ const ๎‚•๎‚™๎‚…๎‚๎‚๎‡‚
๎Šฑ๎‚‰๎‚– ๎‚‘๎‚…๎‚ ๎‚—๎‚™๎‚‹๎‚‹๎‚‰๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚… ๎‚‘๎‚“๎‚ˆ๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚›๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚–๎‚‰๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š
๎‚˜๎‚Œ๎‚๎‚— ๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚– ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚๎‚˜ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚Š๎‚…๎‚‡๎‚‰๎‚— ๎‚™๎‚’๎‚‡๎‚๎‚‰๎‚…๎‚– ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰
๎‚”๎‚–๎‚“๎‚“๎‚Š๎‚–๎‚‰๎‚…๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€ ๎€™๎‚Œ๎‚‰๎‚’ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚๎‚’๎‚‹ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚›๎‚๎‚˜๎‚Œ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ
๎‚๎‚‘๎‚‘๎‚‰๎‚ˆ๎‚๎‚…๎‚˜๎‚‰๎‚๎‚ ๎‚‡๎‚“๎‚’๎‚—๎‚๎‚ˆ๎‚‰๎‚– ๎‚Œ๎‚“๎‚› ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚›๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚™๎‚—๎‚‰ const ๎‚†๎‚ ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜ ๎‚™๎‚’๎‚๎‚‰๎‚—๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚
๎‚…๎‚–๎‚‰๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚๎‚— ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰๎‚ˆ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚‰๎‚ˆ๎‡€
RULE
142
RULE โ€” Pointer-type function parameters which point to memory that is
not to be changed must be declared as const
๎€๎‚…๎‚–๎‚ ๎‚…๎‚— const ๎‚…๎‚๎‚ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡‚๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚… ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚–๎‚‰๎‚…
๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚— ๎‚–๎‚‰๎‚…๎‚ˆ๎‡‚๎‚“๎‚’๎‚๎‚ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚“๎‚ˆ๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€–๎‚Œ๎‚‰ const ๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚– ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“
๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‡€
Bad example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚™๎‚—๎‚‰ const ๎‚Š๎‚“๎‚– ๎‚๎‚˜๎‚— ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‡€
uint32_t foo(uint32_t *val) {
/* val lue */
uint32_t ret = 0;
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 111
if(TEST_VALUE > *val) {
ret = (*val) * 2;
} else {
ret = (*val);
}
return ret;
}
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ const ๎‚๎‚— ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‡€ ๎€–๎‚Œ๎‚‰
๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚–๎‚‰๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚๎‚’ ๎‚Š๎‚…๎‚‡๎‚˜ ๎‚‘๎‚“๎‚ˆ๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚“๎‚ˆ๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
uint32_t foo(const uint32_t *val) {
uint32_t ret = 0;
if (NULL != val){
if(TEST_VALUE > *val) {
ret = (*val) * 2;
} else {
ret = (*val);
}
}
return ret;
}
13.4.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€‡๎€…๎€Ž๎ˆ๎ˆ๎‡‚๎€… ๎€…๎‚“๎‚’๎‚—๎‚˜๎‡‚๎‚•๎‚™๎‚…๎‚๎‚๎‚Š๎‚ ๎‚๎‚‘๎‚‘๎‚™๎‚˜๎‚…๎‚†๎‚๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡ ๎€†๎€‡๎€…๎€Ž๎ˆ๎ˆ’๎‡‚๎€… ๎€†๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚˜๎‚“ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰
๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚— ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‡๎€š๎€’๎ˆ“๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‘๎‚“๎‚ˆ๎‚๎‚Š๎‚ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚—๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ—๎‡€๎ˆ๎ˆ’ ๎€ƒ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚… ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‡‚๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚˜๎‚๎‚”๎‚‰ ๎‚›๎‚Œ๎‚‰๎‚’๎‚‰๎‚š๎‚‰๎‚– ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ‘๎ˆ ๎€‹๎‚‘๎‚”๎‚–๎‚“๎‚”๎‚‰๎‚– ๎€‹๎‚’๎‚”๎‚™๎‚˜ ๎€˜๎‚…๎‚๎‚๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ’๎ˆ•๎ˆ˜ ๎€†๎‚๎‚š๎‚๎‚ˆ๎‚‰ ๎‚†๎‚ ๎€œ๎‚‰๎‚–๎‚“๎‡€
13.5 Using inline functions
๎€…๎ˆ˜๎ˆ˜ ๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚‰๎‚› inline ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ ๎‚…๎‚— ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚–๎‡€ ๎€ƒ๎‚’ inline ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ
๎‚‰๎‚œ๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚ ๎‚๎‚๎‚’๎‚๎‚…๎‚‹๎‚‰ ๎‚†๎‚™๎‚˜ ๎‚’๎‚“๎‚˜ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚๎‚‰๎‚…๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’
๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚…๎‚’ inline ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚†๎‚‰ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚™๎‚’๎‚๎‚˜๎‡€
Information
๎€ƒ๎‚’ inline ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎Šฑ๎‚๎‚‰๎‚— ๎‚†๎‚ ๎‚†๎‚‰๎‚๎‚’๎‚‹ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚… ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚–
๎Šฑ๎‚๎‚‰๎‡€
RULE
143
RULE โ€” inline functions must be declared as static
๎€–๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎†ฝ ๎‚…๎‚’ inline ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ static๎‡€
112 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
13.5.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ—๎‡€๎ˆ๎ˆ ๎€ƒ๎‚’ ๎‚๎‚’๎‚๎‚๎‚’๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚๎‚‡ ๎‚—๎‚˜๎‚“๎‚–๎‚…๎‚‹๎‚‰ ๎‚‡๎‚๎‚…๎‚—๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ๎ˆ”๎‡‚๎€… ๎€†๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰ ๎Šฑ๎‚๎‚‰๎‡‚๎‚—๎‚‡๎‚“๎‚”๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚— ๎‚“๎‚– ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚’๎‚‰๎‚‰๎‚ˆ ๎‚‰๎‚œ๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚ ๎‚๎‚๎‚’๎‚๎‚…๎‚‹๎‚‰ ๎‚…๎‚—
๎‚—๎‚˜๎‚…๎‚˜๎‚๎‚‡๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€๎€•๎€…๎ˆ“๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚š๎‚๎‚“๎‚๎‚…๎‚˜๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚–๎‚…๎‚๎‚’๎‚˜๎‚—๎‡€
13.6 Rede๎‚พning functions
๎€ƒ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚‰๎‚– ๎‚‰๎‚š๎‚‰๎‚’ ๎‚๎‚Š ๎‚๎‚˜ ๎‚๎‚— ๎‚… ๎‚’๎‚…๎‚‘๎‚‰ ๎‚…๎‚๎‚–๎‚‰๎‚…๎‚ˆ๎‚ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰
๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚“๎‚– ๎‚๎‚’ ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚…๎‚ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚‡๎‚“๎‚’๎‚Š๎‚™๎‚—๎‚๎‚“๎‚’๎‡€ ๎€‡๎‚š๎‚‰๎‚–๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜
๎‚Œ๎‚…๎‚š๎‚‰ ๎‚๎‚˜๎‚— ๎‚“๎‚›๎‚’ ๎‚’๎‚…๎‚‘๎‚‰๎‡€
RULE
144
RULE โ€” Do not rede๎‚พne functions or macros from the standard library or
another library
๎€‹๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚—๎†ฝ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚—๎†ฝ ๎‚“๎‚– ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚’๎‚…๎‚‘๎‚‰๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚”๎‚…๎‚–๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚“๎‚– ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚–
๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚‡๎‚“๎‚’๎‚Š๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚›๎‚๎‚๎‚ ๎‚“๎‚‡๎‚‡๎‚™๎‚– ๎‚ˆ๎‚™๎‚‰ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚’๎‚…๎‚‘๎‚‰
๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚๎‚–๎‚‰๎‚…๎‚ˆ๎‚ ๎‚‰๎‚œ๎‚๎‚—๎‚˜๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‡€
/* do not reuse the name of the standard library */
void* malloc (size_t taille );
Good example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚— ๎‚… ๎‚’๎‚…๎‚‘๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚‡๎‚๎‚…๎‚—๎‚Œ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‡‚
๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‡€
/* renaming of the function */
void* mymalloc (size_t taille );
13.6.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ”๎‡€๎ˆ— ๎€‹๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚— ๎‚“๎‚– ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚‰๎‚œ๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚ ๎‚๎‚๎‚’๎‚๎‚…๎‚‹๎‚‰ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰
๎‚™๎‚’๎‚๎‚•๎‚™๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ”๎‡€๎ˆ˜ ๎€‹๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚— ๎‚“๎‚– ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚ ๎‚๎‚๎‚’๎‚๎‚…๎‚‹๎‚‰ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰
๎‚™๎‚’๎‚๎‚•๎‚™๎‚‰๎‡€
13.7 Mandatory use of the return value of a function
๎€ƒ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚›๎‚Œ๎‚“๎‚—๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚— ๎‚’๎‚“๎‚˜ void๎†ฝ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚— ๎‚… ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚™๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚“๎‚– ๎‚Š๎‚…๎‚๎‚๎‚™๎‚–๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚™๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚๎‚— ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚๎‚— ๎‚… ๎‚š๎‚‰๎‚–๎‚ ๎‚๎‚‘๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚’๎‚˜
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 113
๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚™๎‚’๎‚‰๎‚œ๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚“๎‚– ๎‚‰๎‚š๎‚‰๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚—๎‚“๎‚“๎‚’ ๎‚…๎‚—
๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰๎‡€ ๎€–๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚†๎‚‰ ๎‚–๎‚‰๎‚…๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚…๎‚’๎‚…๎‚‹๎‚‰๎‚ˆ๎‡€
๎€–๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚๎‚๎‚๎‚’๎‚‹ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚˜๎‚‰๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚“ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚๎‚˜๎‚— ๎‚š๎‚…๎‚๎‚๎‚ˆ๎‚๎‚˜๎‚ ๎‚…๎‚‹๎‚…๎‚๎‚’๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚‰
๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚Š๎‚…๎‚‡๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡ฆ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚‰๎‚ˆ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚–๎‚…๎‚’๎‚‹๎‚‰ ๎‚“๎‚– ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚‰๎‚ˆ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚๎‚’๎‚‹
๎‚˜๎‚“ ๎‚… ๎‚—๎‚™๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚“๎‚– ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡ง๎‡€
RULE
145
RULE โ€” The return value of a function must always be tested
๎€™๎‚Œ๎‚‰๎‚’ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚— ๎‚… ๎‚š๎‚…๎‚๎‚™๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚‰๎‚ˆ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚˜๎‚‰๎‚—๎‚˜๎‚‰๎‚ˆ๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚˜๎‚‰๎‚—๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚’๎‚“ ๎‚”๎‚–๎‚“๎‡‚
๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚๎‚— ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ ๎‚๎‚Š ๎‚…๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚Œ๎‚…๎‚— ๎‚“๎‚‡๎‚‡๎‚™๎‚–๎‚–๎‚‰๎‚ˆ๎‡€
struct stat o_stat_buffer;
stat (" somefile.txt", & o_stat_buffer );
/* success of the stat function not tested */
...
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚˜๎‚‰๎‚—๎‚˜๎‚‰๎‚ˆ๎‡€
struct stat o_stat_buffer;
uint8_t i_result = 0;
i_result = stat(" somefile.txt", &o_stat_buffer );
if (0 != i_result) {
/* erreur */
return 0;
}
...
13.7.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‡๎€š๎€’๎ˆ๎ˆ‘๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚๎‚‹๎‚’๎‚“๎‚–๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€†๎‚๎‚–๎‡€ ๎ˆ“๎‡€๎ˆ–๎†ฟ ๎€‹๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚— ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰
๎‚˜๎‚‰๎‚—๎‚˜๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ–๎‡€๎ˆ– ๎€–๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚Œ๎‚…๎‚š๎‚๎‚’๎‚‹ ๎‚’๎‚“๎‚’๎‡‚๎‚š๎‚“๎‚๎‚ˆ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚˜๎‚๎‚”๎‚‰ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ‘๎ˆ”๎ˆ‘ ๎€—๎‚’๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ ๎€”๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎€˜๎‚…๎‚๎‚™๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ‘๎ˆ”๎ˆ’ ๎€‹๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎€…๎‚Œ๎‚‰๎‚‡๎‚ ๎‚“๎‚Š ๎€ˆ๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎€”๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎€˜๎‚…๎‚๎‚™๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ–๎ˆ”๎ˆ“ ๎€‹๎‚‘๎‚”๎‚–๎‚“๎‚”๎‚‰๎‚– ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚ ๎‚Š๎‚“๎‚– ๎‚™๎‚’๎‚™๎‚—๎‚™๎‚…๎‚ ๎‚“๎‚– ๎‚‰๎‚œ๎‚‡๎‚‰๎‚”๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
13.8 Implicit return prohibited for non-void functions
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚Š๎‚“๎‚– ๎‚…๎‚๎‚ ๎‚”๎‚…๎‚˜๎‚Œ๎‚— ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚๎‚’๎‚‹ ๎‚… ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‡ฆ๎‚’๎‚“๎‚’๎‡‚void
๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡ง๎†ฝ ๎‚—๎‚“๎‚‘๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚˜๎‚‰ ๎‚…๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚’
๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ๎‡€ ๎€•๎‚“๎‚‘๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚…๎‚’ ๎‚…๎‚–๎‚†๎‚๎‚˜๎‚–๎‚…๎‚–๎‚ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‡€
114 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
RULE
146
RULE โ€” Implicit return prohibited for non-void type functions
๎€ƒ๎‚๎‚ ๎‚”๎‚…๎‚˜๎‚Œ๎‚— ๎‚“๎‚Š ๎‚… ๎‚’๎‚“๎‚’๎‡‚void ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚… ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚…๎‚–๎‚‰ ๎‚”๎‚…๎‚˜๎‚Œ๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚… ๎‚š๎‚…๎‚๎‚™๎‚‰๎‡€
uint32_t encr_data(const uint8_t * p_data , uint32_t ui32_data_len ,
uint8_t ** pp_encrypted_data , uint32_t * ui32_encrypted_data_len)
{
uint8_t *p_encrypted_data = NULL;
if (NULL != p_data
&& NULL != pp_encrypted_data
&& NULL != ui32_encrypted_data_len) {
if (ui32_data_len > 0) {
p_encrypted_data = ( uint8_t *)calloc(ui32_data_len , sizeof ( uint8_t));
...
return 1;
}
}
/* implicit return */
}
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚— ๎‚… ๎‚š๎‚…๎‚๎‚™๎‚‰๎‡€
uint32_t encr_data(const uint8_t * p_data , uint32_t ui32_data_len ,
uint8_t ** pp_encrypted_data , uint32_t * ui32_encrypted_data_len)
{
uint32_t ui32_result_code = 0;
uint8_t *p_encrypted_data = NULL;
if (NULL == p_data
|| NULL == pp_encrypted_data
|| NULL == ui32_encrypted_data_len) {
ui32_result_code = 0;
goto End;
}
if (0 == ui32_data_len) {
ui32_result_code = 0;
goto End;
}
p_encrypted_data = ( uint8_t *)calloc(ui32_data_len , sizeof ( uint8_t));
...
(* pp_encrypted_data ) = p_encrypted_data;
ui32_result_code = 1;
End:
return ui32_result_code ;
}
13.8.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ–๎‡€๎ˆ“ ๎€ƒ๎‚๎‚ ๎‚‰๎‚œ๎‚๎‚˜ ๎‚”๎‚…๎‚˜๎‚Œ๎‚— ๎‚Š๎‚–๎‚“๎‚‘ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚›๎‚๎‚˜๎‚Œ ๎‚’๎‚“๎‚’๎‡‚๎‚š๎‚“๎‚๎‚ˆ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚˜๎‚๎‚”๎‚‰ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜
๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ ๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€๎€•๎€…๎ˆ’๎ˆ–๎‡‚๎€… ๎€‡๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚ ๎‚’๎‚‰๎‚š๎‚‰๎‚– ๎‚–๎‚‰๎‚…๎‚‡๎‚Œ๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚… ๎‚’๎‚“๎‚’๎‡‚๎‚š๎‚“๎‚๎‚ˆ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 115
13.9 No passing by value of a structure as function
parameter
๎€‹๎‚˜ ๎‚๎‚— ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚˜๎‚“ ๎‚”๎‚…๎‚—๎‚— ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚…๎‚— ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€–๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚…๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰๎‚’
๎‚‡๎‚“๎‚”๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚‡๎‚๎‡€ ๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚๎‚— ๎‚ˆ๎‚‰๎‚˜๎‚–๎‚๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚ ๎‚˜๎‚“ ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚’๎‚‡๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚…๎‚—๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚๎‚—๎‚ ๎‚“๎‚Š ๎‚—๎‚˜๎‚…๎‚‡๎‚
๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚› ๎‚“๎‚– ๎‚‰๎‚š๎‚‰๎‚’ ๎‚๎‚‰๎‚…๎‚๎‚…๎‚‹๎‚‰ ๎‚“๎‚Š ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚…๎‡€
๎€–๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚”๎‚…๎‚—๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚–๎‚‘ ๎‚“๎‚Š ๎‚… const๎‡ž๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚ˆ
๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡€ ๎€‘๎‚’๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚‡๎‚“๎‚”๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚‡๎‚๎‡€ ๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚–๎‚‘๎‚“๎‚–๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ const
๎‚‘๎‚“๎‚ˆ๎‚๎Šฑ๎‚‰๎‚– ๎‚”๎‚–๎‚‰๎‚š๎‚‰๎‚’๎‚˜๎‚— ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰๎‚— ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‡ฆ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚๎‚— ๎‚ˆ๎‚‰๎‚—๎‚๎‚–๎‚…๎‚†๎‚๎‚‰ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚”๎‚…๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚†๎‚
๎‚š๎‚…๎‚๎‚™๎‚‰๎‡ง๎‡€
RULE
147
RULE โ€” Structures must be passed by reference to a function
๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚”๎‚…๎‚—๎‚— ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚†๎‚ ๎‚‡๎‚“๎‚”๎‚๎‚๎‚’๎‚‹ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚‡๎‚…๎‚๎‚๎‚๎‚’๎‚‹ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚๎‚— ๎‚”๎‚…๎‚—๎‚—๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚—๎‡€
#define STR_SIZE 20U
typedef struct
{
unsigned char surname[STR_SIZE ];
unsigned char firstname[STR_SIZE];
} person_t;
uint32_t add_person (person_t person ) {
size_t sz_surname_len = 0;
size_t sz_firstname_len = 0;
sz_surname_len = strlen(person.surname);
sz_firstname_len = strlen(person.firstname);
if (0 != sz_surname_len && 0 != sz_firstname_len ) {
...
ui32_result = 1;
} else {
ui32_result = 0;
}
return ui32_result;
}
...
void some_function () {
person_t person;
...
add_person ( person);
...
}
Good example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚”๎‚…๎‚—๎‚—๎‚‰๎‚— ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡€
#define STR_SIZE 20U
typedef struct
{
unsigned char surname[STR_SIZE ];
unsigned char firstname[STR_SIZE];
} person_t;
116 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
uint32_t add_person (const person_t *person ) {
uint32_t ui32_result = 0;
size_t sz_surname_len = 0;
size_t sz_firstname_len = 0;
if (NULL != person) {
sz_surname_len = strlen(person ->surname);
sz_firstname_len = strlen(person ->firstname);
if (0 != sz_surname_len && 0 != sz_firstname_len ) {
...
ui32_result = 1;
} else {
ui32_result = 0;
}
}
else {
ui32_result = 0;
}
return ui32_result;
}
void some_function () {
person_t person;
...
add_person (& person);
...
}
13.10 Passing an array as a parameter for a function
๎€™๎‚Œ๎‚‰๎‚’ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚…๎‚๎‚‰๎‚— ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚…๎‚— ๎‚… ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚‰ ๎‚›๎‚Œ๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–
๎‚๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚–๎‚—๎‚˜ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚“๎‚– ๎‚›๎‚Œ๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚๎‚˜ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚— ๎‚˜๎‚“ ๎‚… ๎‚—๎‚๎‚’๎‚‹๎‚๎‚‰ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€
๎€–๎‚“ ๎‚—๎‚™๎‚”๎‚”๎‚–๎‚‰๎‚—๎‚— ๎‚˜๎‚Œ๎‚๎‚— ๎‚…๎‚‘๎‚†๎‚๎‚‹๎‚™๎‚๎‚˜๎‚๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚”๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚…๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚–๎‚‘ ๎‚›๎‚๎‚˜๎‚Œ [] ๎‚Š๎‚“๎‚– ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚…๎‚—
๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚—๎‚™๎‚†๎‡‚๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ—๎‡€๎ˆ๎‡€
RECO
148
RECOMMENDATION โ€” Passing of an array as a parameter for a function
๎€–๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚…๎‚–๎‚‰ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚›๎‚…๎‚๎‚— ๎‚˜๎‚“ ๎‚”๎‚…๎‚—๎‚— ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚…๎‚— ๎‚… ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚Š๎‚“๎‚– ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€™๎‚Œ๎‚‰๎‚’ ๎‚”๎‚…๎‚—๎‚—๎‚๎‚’๎‚‹
๎‚†๎‚ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎†ฝ ๎‚๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–
๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚๎‚—๎‚“ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚’๎‚“๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
Warning
๎€ˆ๎‚“๎‚– ๎‚… ๎‚‘๎‚™๎‚๎‚˜๎‚๎‡‚๎‚ˆ๎‚๎‚‘๎‚‰๎‚’๎‚—๎‚๎‚“๎‚’๎‚…๎‚ ๎‚…๎‚–๎‚–๎‚…๎‚๎†ฝ ๎‚“๎‚’๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚–๎‚—๎‚˜ ๎‚ˆ๎‚๎‚‘๎‚‰๎‚’๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚‡๎‚…๎‚’ ๎‚–๎‚‰๎‚‘๎‚…๎‚๎‚’ ๎‚™๎‚’๎‡‚
๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚”๎‚…๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚…๎‚— ๎‚… ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚‘๎‚‰๎‚…๎‚’๎‚— ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹
๎‚ˆ๎‚๎‚‘๎‚‰๎‚’๎‚—๎‚๎‚“๎‚’๎‚—๎‡€ ๎€ˆ๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚Š๎‚“๎‚– ๎‚… ๎‚˜๎‚›๎‚“๎‡‚๎‚ˆ๎‚๎‚‘๎‚‰๎‚’๎‚—๎‚๎‚“๎‚’๎‚…๎‚ ๎‚…๎‚–๎‚–๎‚…๎‚๎†ฝ ๎‚™๎‚—๎‚๎‚’๎‚‹ tab[][] ๎‚…๎‚— ๎‚… ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–
๎‚๎‚— ๎‚… ๎‚‘๎‚๎‚—๎‚˜๎‚…๎‚๎‚‰๎†ฝ ๎‚…๎‚— ๎‚… ๎‚‘๎‚๎‚’๎‚๎‚‘๎‚™๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚‡๎‚“๎‚’๎‚ˆ ๎‚ˆ๎‚๎‚‘๎‚‰๎‚’๎‚—๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ๎‡€
Bad example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚—๎‚Œ๎‚“๎‚›๎‚— ๎‚… ๎‚”๎‚–๎‚“๎‚˜๎‚“๎‚˜๎‚๎‚”๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡‚๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‡€
๎€‹๎‚˜ ๎‚๎‚— ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚”๎‚…๎‚—๎‚—๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚… ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚†๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‡๎‚…๎‚’๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚‹๎‚™๎‚‰๎‚—๎‚—๎‚‰๎‚ˆ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’
๎‚—๎‚๎‚‹๎‚’๎‚…๎‚˜๎‚™๎‚–๎‚‰๎‡€ ๎€‹๎‚’ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚…๎‚† ๎‚‡๎‚…๎‚’๎†ฟ
n
๎‚‰๎‚๎‚˜๎‚Œ๎‚‰๎‚– ๎‚†๎‚‰ ๎‚…๎‚’ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚”๎‚…๎‚—๎‚—๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚—
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 117
n
๎‚“๎‚– ๎‚†๎‚‰ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚“๎‚Š ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚–๎‚—๎‡€
void func( int32_t *tab , uint32_t count);
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚๎‚— ๎‚—๎‚‰๎‚‡๎‚“๎‚’๎‚ˆ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚“๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚˜ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚๎‚‘๎‚‘๎‚‰๎‚ˆ๎‚๎‡‚
๎‚…๎‚˜๎‚‰๎‚๎‚ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚‰๎‚ˆ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚๎‚— ๎‚๎‚’๎‚ˆ๎‚‰๎‚‰๎‚ˆ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚๎‡€
void func( int32_t tab[], uint32_t count ); /* tab is an array of count elements */
13.11 Mandatory use in a function of all its parameters
๎€ˆ๎‚…๎‚๎‚๎‚™๎‚–๎‚‰ ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰ ๎‚… ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚™๎‚—๎‚™๎‚…๎‚๎‚๎‚ ๎‚… ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‡€ ๎€‹๎‚˜ ๎‚…๎‚๎‚—๎‚“
๎‚‡๎‚“๎‚’๎‚—๎‚™๎‚‘๎‚‰๎‚— ๎‚™๎‚’๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚‡๎‚๎‡€
๎€–๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚˜๎‚“๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‘๎‚“๎‚ˆ๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰๎‚Š๎‚™๎‚๎‡€
๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚๎‚’ ๎‚—๎‚“๎‚‘๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰๎‚—๎†ฝ ๎‚๎‚˜ ๎‚‘๎‚…๎‚ ๎‚†๎‚‰ ๎‚Ž๎‚™๎‚—๎‚˜๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰ ๎‚“๎‚’๎‚‰ ๎‡ฆ๎‚“๎‚– ๎‚‘๎‚“๎‚–๎‚‰๎‡ง ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฟ
n
๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚… ๎‚‡๎‚…๎‚๎‚๎‚†๎‚…๎‚‡๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚›๎‚Œ๎‚“๎‚—๎‚‰ ๎‚”๎‚–๎‚“๎‚˜๎‚“๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚— ๎‚๎‚‘๎‚”๎‚“๎‚—๎‚‰๎‚ˆ๎†พ
n
๎‚Š๎‚“๎‚– ๎‚–๎‚‰๎‚…๎‚—๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚˜๎‚๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚›๎‚๎‚˜๎‚Œ ๎‚‰๎‚œ๎‚๎‚—๎‚˜๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚™๎‚”๎‚‹๎‚–๎‚…๎‚ˆ๎‚๎‚’๎‚‹ ๎‚… ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‡€ ๎€ƒ ๎‚”๎‚–๎‚‰๎‚š๎‚๎‚“๎‚™๎‚—๎‚๎‚ ๎‚™๎‚—๎‚‰๎‚ˆ
๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚๎‚— ๎‚’๎‚“ ๎‚๎‚“๎‚’๎‚‹๎‚‰๎‚– ๎‚™๎‚—๎‚‰๎‚ˆ๎†พ
n
๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚˜๎‚™๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚’ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚›๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎€‹๎‚’ ๎‚…๎‚๎‚ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰๎‚—๎†ฝ ๎‚… ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰ ๎‚›๎‚Œ๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚๎‚— ๎‚๎‚‹๎‚’๎‚“๎‚–๎‚‰๎‚ˆ๎‡€
RECO
149
RECOMMENDATION โ€” Mandatory use in a function of all its parameters
๎€ƒ๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚˜๎‚“๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚๎‚˜๎‚— ๎‚๎‚‘๎‡‚
๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
Information
๎€–๎‚Œ๎‚‰ -Wunused-parameter ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚”๎‚–๎‚“๎‚š๎‚๎‚ˆ๎‚‰ ๎‚…๎‚๎‚‰๎‚–๎‚˜๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚๎‚— ๎‚—๎‚‡๎‚‰๎‚’๎‚…๎‚–๎‚๎‚“๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚… ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‡‚
๎‚Š๎‚“๎‚–๎‚‰ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚๎‚‰๎‚˜๎‚‰๎‚ˆ๎‡€
uint32_t compute_data ( uint32_t ui32A , uint32_t ui32B , uint32_t ui32C) {
uint32_t ui32_result = 0;
ui32_result = 2 * ui32A + 2 * ui32B;
return ui32_result;
}
118 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚…๎‚–๎‚‰ ๎‚’๎‚“ ๎‚™๎‚’๎‚™๎‚—๎‚‰๎‚ˆ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
uint32_t compute_data ( uint32_t ui32A , uint32_t ui32B) {
uint32_t ui32_result = 0;
ui32_result = 2 * ui32A + 2 * ui32B;
return ui32_result;
}
13.11.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎‡€๎ˆ– ๎€–๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚’๎‚“ ๎‚™๎‚’๎‚™๎‚—๎‚‰๎‚ˆ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚๎‚’ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‡๎€š๎€’๎ˆ’๎ˆ–๎‡‚๎€… ๎€…๎‚…๎‚๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎‡€
13.12 Variadic functions
๎€˜๎‚…๎‚–๎‚๎‚…๎‚ˆ๎‚๎‚‡ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‡ฆi.e. ๎‚˜๎‚Œ๎‚“๎‚—๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚– ๎‚›๎‚๎‚˜๎‚Œ ๎‚š๎‚…๎‚–๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚๎‚”๎‚‰๎‚—๎‡ง ๎‚‡๎‚…๎‚’ ๎‚”๎‚“๎‚—๎‚‰
๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘๎‚—๎‡€ ๎€‹๎‚˜ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚…๎‚ˆ๎‚š๎‚๎‚—๎‚…๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚ˆ๎‚๎‚‡ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚๎‚˜๎‚—๎‚‰๎‚๎‚Š
๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚— ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚“๎Šน๎‚‰๎‚’ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚ˆ๎‚๎‚‡ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’
๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎†ฝ ๎‚†๎‚ ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚‡๎‚…๎‚’๎†ฝ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚๎†ฝ ๎‚๎‚‰๎‚…๎‚ˆ
๎‚˜๎‚“ ๎‚—๎‚“๎‚‘๎‚‰ ๎‚—๎‚™๎‚–๎‚”๎‚–๎‚๎‚—๎‚‰๎‚— ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— ๎‚…๎‚†๎‚’๎‚“๎‚–๎‚‘๎‚…๎‚ ๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚– ๎‚™๎‚’๎‚‰๎‚œ๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€
Information
๎€–๎‚Œ๎‚‰ -Wformat=2 ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚–๎‚‰๎‚•๎‚™๎‚๎‚–๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚—๎‚™๎‚†๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ”๎‡€๎ˆ’๎‡€๎ˆ๎†ฝ ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚˜๎‚“ ๎‚‰๎‚œ๎‚˜๎‚‰๎‚’๎‚ˆ
๎‚๎‚˜๎‚— ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚— ๎‚˜๎‚“ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚ˆ๎‚๎‚‡ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎€™๎‚Œ๎‚‰๎‚’ NULL ๎‚๎‚— ๎‚”๎‚…๎‚—๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚… ๎‚‡๎‚๎‚…๎‚—๎‚—๎‚๎‚‡ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ NULL ๎‚๎‚— ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚๎‚”๎‚‰๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‡‚
๎‚—๎‚๎‚“๎‚’ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚›๎‚“๎‚–๎‚ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚ˆ๎‚๎‚‡ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‡Œ๎‚–๎‚๎‚‹๎‚Œ๎‚˜ ๎‚˜๎‚๎‚”๎‚‰๎‡ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚๎‚’๎‚“๎‚›๎‚’๎‡€ ๎€‹๎‚’ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚–๎†ฝ
๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚…๎‚๎‚๎‚“๎‚›๎‚— NULL ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚…๎‚’ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚“๎‚– ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚—๎‚“ ๎‚“๎‚’ ๎‚”๎‚๎‚…๎‚˜๎‚Š๎‚“๎‚–๎‚‘๎‚— ๎‚›๎‚Œ๎‚‰๎‚–๎‚‰ NULL
๎‚๎‚— ๎‚…๎‚๎‚—๎‚“ ๎‚…๎‚’ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎†ฝ ๎‚”๎‚…๎‚—๎‚—๎‚๎‚’๎‚‹ NULL ๎‚Š๎‚“๎‚– ๎‚š๎‚…๎‚–๎‚๎‚…๎‚ˆ๎‚๎‚‡ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‡๎‚…๎‚’ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€
RULE
150
RULE โ€” Do not call variadic functions with NULL as an argument
Bad example
...
unsigned char * string = NULL;
printf ("%s %d\n", string , 1); /* undefined behaviour */
...
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 119
Good example
...
unsigned char * string = NULL;
printf ("%s %d\n", ( string ? string : "null"), 1); /* not passing NULL */
...
13.12.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€–๎‚Œ๎‚‰ ๎‚Š๎‚‰๎‚…๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚“๎‚Š <stdarg.h> ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ๎ˆ๎‡‚๎€… ๎€๎‚…๎‚๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚…๎‚‡๎‚˜ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚›๎‚–๎‚๎‚˜๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚๎‚๎‚‰๎‚– ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚ˆ๎‚๎‚‡ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ๎ˆ๎‡‚๎€… ๎€—๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚—๎‚—๎‚™๎‚‰๎‚— ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚ˆ๎‚๎‚‡ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‡๎€š๎€’๎ˆ“๎ˆ–๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚…๎‚๎‚ ๎‚š๎‚…๎‡ ๎‚…๎‚–๎‚‹ ๎‚›๎‚๎‚˜๎‚Œ ๎‚…๎‚’ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚๎‚”๎‚‰๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€๎€•๎€…๎ˆ’๎ˆ˜๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚…๎‚๎‚ ๎‚š๎‚…๎‡ ๎‚…๎‚–๎‚‹ ๎‚“๎‚’ ๎‚… ๎‚š๎‚…๎‡ ๎‚๎‚๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚Œ๎‚…๎‚— ๎‚…๎‚’ ๎‚๎‚’๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ‘๎ˆ— ๎€ˆ๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚๎‚ ๎‚›๎‚๎‚˜๎‚Œ ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€…๎‚…๎‚๎‚๎‚๎‚’๎‚‹ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‡ฌ๎‚…๎‚–๎‚‹๎‚‡๎‚“๎‚‘๎‚”๎‡ญ๎‡€
120 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
14
Sensitive operators
14.1 Use of the comma prohibited for statement sequences
๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚‘๎‚… ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚… ๎‚—๎‚‰๎‚”๎‚…๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚– ๎‚…๎‚— ๎‚… ๎‚—๎‚‰๎‚”๎‚…๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚Š๎‚“๎‚–
๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚๎‚’๎‚‹ ๎Šฑ๎‚‰๎‚๎‚ˆ๎‚— ๎‚๎‚’ ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚“๎‚– ๎‚…๎‚–๎‚–๎‚…๎‚๎‡€ ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚‘๎‚… ๎‚๎‚— ๎‚…๎‚๎‚—๎‚“ ๎‚˜๎‚“๎‚๎‚‰๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚‘๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚… ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‡‚
๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚—๎‚™๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚“ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚’๎‚‡๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚–๎‚™๎‚๎‚‰๎‚— ๎‚“๎‚’ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€ ๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š
๎‚‡๎‚“๎‚‘๎‚‘๎‚…๎‚— ๎‚˜๎‚“ ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹ ๎‚˜๎‚“๎‚‹๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚–๎‚‰๎‚…๎‚ˆ๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚…๎‚
๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚™๎‚’๎‚‰๎‚œ๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎‚—๎‡€
RULE
151
RULE โ€” Use of the comma prohibited for statement sequences
๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚‘๎‚… ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚…๎‚™๎‚˜๎‚Œ๎‚“๎‚–๎‚๎‚—๎‚‰๎‚ˆ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎‡€
๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚‘๎‚… ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚… ๎‚—๎‚‰๎‚‘๎‚๎‚‡๎‚“๎‚๎‚“๎‚’ ๎‚Š๎‚“๎‚– ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰๎‚—๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚‘๎‚‰๎‚…๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜๎†ฟ
n
๎‚†๎‚–๎‚…๎‚‡๎‚‰๎‚— ๎‚†๎‚‰๎‚‡๎‚“๎‚‘๎‚‰ ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚๎†พ
n
๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚Š for ๎‚๎‚“๎‚“๎‚”๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚“๎‚–๎‚‹๎‚…๎‚’๎‚๎‚—๎‚‰๎‚ˆ๎‡€
Bad example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚‘๎‚… ๎‚๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚—๎‡€ ๎€‹๎‚˜ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚˜๎‚“
๎‚๎‚’๎‚“๎‚› ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚–๎‚‰๎‚…๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€
int32_t i = (j = 2, 1);
y = x ? (a++, a + 4) : c;
z = 3 * b + 2, 7 * c + 42;
a = (b = 2, c = 3, d = 4);
for(i = 0, j = SZ_MAX; i < SZ_MAX ; i++, j--) {
...
}
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚‘๎‚… ๎‚๎‚— ๎‚“๎‚’๎‚๎‚ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎‡€
int32_t i, j;
i=1;
j=2;
if (0 != x) {
a++;
y = a + 4;
} else {
y = c;
}
z = 3 * b + 2;
j = SZ_MAX;
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 121
for(i = 0; i < SZ_MAX; i++) {
...
j--;
}
14.1.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ‘๎‡€๎ˆ’๎†ฟ ๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚‘๎‚… ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
14.2 Using pre/post๎‚พx ++ and -- operators and
compound assignment operators
๎€™๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‡š๎‚”๎‚“๎‚—๎‚˜๎Šฑ๎‚œ ++ ๎‚…๎‚’๎‚ˆ -- ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚… ๎‚‡๎‚…๎‚๎‚‡๎‚™๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚š๎‚‰๎‚–๎‚ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚‰๎‚—๎‚˜๎‚…๎‚†๎‡‚
๎‚๎‚๎‚—๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚๎‚‡๎‚™๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚…๎‚’๎‚…๎‚๎‚๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€ ๎€๎‚“๎‚–๎‚‰๎‚“๎‚š๎‚‰๎‚–๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚…๎‚๎‚—๎‚“ ๎‚… ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚’๎‚Š๎‚™๎‚—๎‚๎‚“๎‚’
๎‚…๎‚’๎‚ˆ ๎‚‰๎‚š๎‚‰๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–๎‡€ ๎€–๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚…๎‚๎‚“๎‚’๎‚‰ ๎‚๎‚’ ๎‚… ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€ ๎€ƒ๎‚—
๎‚… ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎†ฝ ๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎‚”๎‚–๎‚‰๎‡š๎‚”๎‚“๎‚—๎‚˜๎Šฑ๎‚œ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚—๎‚‰๎‚‘๎‚…๎‚’๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚‰๎‚•๎‚™๎‚๎‚š๎‚…๎‚๎‚‰๎‚’๎‚˜ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚๎‚—๎‚“๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚’ ๎‚… ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‡‚
๎‚‘๎‚‰๎‚’๎‚˜ ๎‡ฆi.e. i++; ++j;๎‡ง๎†ฝ ๎‚“๎‚’๎‚๎‚ ๎‚”๎‚“๎‚—๎‚˜๎Šฑ๎‚œ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚…๎‚™๎‚˜๎‚Œ๎‚“๎‚–๎‚๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚…๎‚’๎‚ ๎‚…๎‚‘๎‚†๎‚๎‚‹๎‚™๎‚๎‚˜๎‚
๎ˆ๎ˆ’
๎‡€
๎€’๎‚–๎‚‰๎Šฑ๎‚œ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚›๎‚๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
RECO
152
RECOMMENDATION โ€” The pre๎‚พx operators ++ and -- should not be used
๎€’๎‚–๎‚‰๎‡‚๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚”๎‚–๎‚‰๎‡‚๎‚ˆ๎‚‰๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚›๎‚๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎€Ž๎‚…๎‚—๎‚˜๎‚๎‚๎†ฝ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚›๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚†๎‚–๎‚“๎‚๎‚‰๎‚’ ๎‚ˆ๎‚“๎‚›๎‚’ ๎‚๎‚’๎‚˜๎‚“ ๎‚—๎‚๎‚‘๎‚”๎‚๎‚‰ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎‡€
RECO
153
RECOMMENDATION โ€” No combined use of post๎‚พx operators with other
operators
๎€’๎‚“๎‚—๎‚˜๎‡‚๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚”๎‚“๎‚—๎‚˜๎‡‚๎‚ˆ๎‚‰๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚‘๎‚๎‚œ๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚“๎‚”๎‚‰๎‚–๎‡‚
๎‚…๎‚˜๎‚“๎‚–๎‚—๎‡€
๎€Ž๎‚…๎‚—๎‚˜๎‚๎‚๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚‹๎‚…๎‚๎‚’ ๎‚Š๎‚“๎‚– ๎‚–๎‚‰๎‚…๎‚ˆ๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚–๎‚‰๎‚…๎‚—๎‚“๎‚’๎‚—๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚‰๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰ ๎‚‡๎‚“๎‚‘๎‚†๎‚๎‚’๎‚‰๎‚ˆ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‡‚
๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‡ฆ>>=๎†ฝ &=๎†ฝ *=๎†ฝ etc.๎‡ง๎‡€
RECO
154
RECOMMENDATION โ€” Avoid the use of combined assignment operators
Bad example
๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚†๎‚‰๎‚๎‚“๎‚› ๎‚™๎‚—๎‚‰๎‚— ๎‚”๎‚“๎‚—๎‚˜๎Šฑ๎‚œ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚‘๎‚๎‚œ๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚—๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–
๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ๎‡€ ๎€‹๎‚˜ ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚— ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎ˆ๎ˆ’๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚‡๎‚Œ๎‚“๎‚๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚”๎‚“๎‚—๎‚˜๎Šฑ๎‚œ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚ˆ๎‚๎‚—๎‚‡๎‚™๎‚—๎‚—๎‚‰๎‚ˆ ๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚›๎‚“ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚๎‚—๎‚“๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚–๎‚‰ ๎‚‰๎‚•๎‚™๎‚๎‚š๎‚…๎‚๎‚‰๎‚’๎‚˜๎‡€
122 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
#define TAB_SIZE 25U
...
uint32_t x;
uint8_t b[ TAB_SIZE] = { 0 };
uint16_t i = 0;
x = foo(i++, i); /* not specified: problem with the order of evaluation of the
parameters */
...
uint32_t foo(uint16_t a, uint16_t b) {
return a * b;
}
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚”๎‚“๎‚—๎‚˜๎Šฑ๎‚œ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚๎‚—๎‚“๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎‡€
#define TAB_SIZE 25U
...
uint32_t x;
uint8_t b[ TAB_SIZE] = { 0 };
uint16_t i = 0;
i++; /* N.B. replacing this statement with ++i; would not change the behaviour of
the program in any way */
x = foo(i, i);
...
uint32_t foo(uint16_t a, uint16_t b) {
return a * b;
}
14.2.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ’๎‡€๎ˆ’ ๎€ƒ ๎‚Š๎‚™๎‚๎‚ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚๎‚’๎‚‹ ๎‚…๎‚’ ๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚– ๎‚ˆ๎‚‰๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ
๎‚Œ๎‚…๎‚š๎‚‰ ๎‚’๎‚“ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚”๎‚“๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚—๎‚๎‚ˆ๎‚‰ ๎‚‰๎Šฎ๎‚‰๎‚‡๎‚˜๎‚— ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‡๎‚…๎‚™๎‚—๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚– ๎‚ˆ๎‚‰๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‡๎€š๎€’๎ˆ’๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚“๎‚Š ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚“๎‚– ๎‚—๎‚๎‚ˆ๎‚‰ ๎‚‰๎Šฎ๎‚‰๎‚‡๎‚˜๎‚—๎‡€
14.3 No nested use of the ternary operator โ€œ?:โ€
๎€–๎‚Œ๎‚‰ ๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚–๎‚ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ?: ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚‡๎‚“๎‚’๎‚‡๎‚๎‚—๎‚‰๎‚๎‚ ๎‚›๎‚–๎‚๎‚˜๎‚‰ ๎‚…๎‚’ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚†๎‚…๎‚—๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚…
๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‡€
๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚–๎‚ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎†ฝ ๎‚“๎‚– ๎‚๎‚Š ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚
๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚–๎‚ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚’๎‚‰๎‚—๎‚˜๎‚‰๎‚ˆ๎†ฝ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚๎‚˜๎‚— ๎‚‘๎‚…๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚…๎‚’๎‚‡๎‚‰ ๎‚†๎‚‰๎‚‡๎‚“๎‚‘๎‚‰๎‚— ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜๎‡€
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚…๎‚’ if-else ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
RULE
155
RULE โ€” No nested use of the ternary operator ?:
๎€–๎‚Œ๎‚‰ ๎‚’๎‚‰๎‚—๎‚˜๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚–๎‚ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ?: ๎‚๎‚— ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 123
๎€๎‚“๎‚–๎‚‰๎‚“๎‚š๎‚‰๎‚–๎†ฝ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚“๎‚Š ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚›๎‚“ ๎‡Œ๎‚†๎‚–๎‚…๎‚’๎‚‡๎‚Œ๎‚‰๎‚—๎‡๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚๎‚‘๎‚”๎‚๎‚๎‚‰๎‚— ๎‚…๎‚’ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜
๎‚‡๎‚…๎‚—๎‚˜ ๎‚…๎‚‡๎‚‡๎‚“๎‚–๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚–๎‚ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‡€
RULE
156
RULE โ€” Correct construction of the expressions with the ternary opera-
tor ?:
๎€–๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚–๎‚ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ?: ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‰๎‚œ๎‚…๎‚‡๎‚˜๎‚๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰
๎‚˜๎‚๎‚”๎‚‰ ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚…๎‚’๎‚ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‡€
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚‰๎‚—๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚–๎‚ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚๎‚˜ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‡‚
๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚›๎‚“ ๎‚†๎‚–๎‚…๎‚’๎‚‡๎‚Œ๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚˜๎‚๎‚”๎‚‰๎‡€
y = (x <42) ? 1042 : (t> 0) ? -1042 : 0.0;
/* integer and float , therefore implicit cast */
Good example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚™๎‚—๎‚‰๎‚— ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ if-else ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚๎‚— ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‡‚
๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚… ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚— ๎‚“๎‚’ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
if(x < 42) {
y = 1042;
} else {
if(t > 0) {
y = -1042;
} else {
y = 0;
}
}
14.3.1 References
124 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
15
Memory management
15.1 Dynamic memory allocation
๎€ˆ๎‚“๎‚– ๎‚…๎‚๎‚ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚— ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–๎†ฝ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚–๎‚™๎‚๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ๎‡€ ๎€ˆ๎‚๎‚–๎‚—๎‚˜ ๎‚“๎‚Š
๎‚…๎‚๎‚๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚‘๎‚™๎‚—๎‚˜ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚‰๎‚’๎‚“๎‚™๎‚‹๎‚Œ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚๎‚’ ๎‚•๎‚™๎‚‰๎‚—๎‚˜๎‚๎‚“๎‚’๎‡€
๎€ƒ ๎‚‡๎‚“๎‚‘๎‚‘๎‚“๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚๎‚— ๎‚˜๎‚“ ๎‚…๎‚”๎‚”๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ sizeof ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚“๎‚’ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’๎‚—๎‚˜๎‚‰๎‚…๎‚ˆ
๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚๎†ฝ ๎‚“๎‚– ๎‚’๎‚“๎‚˜ ๎‚˜๎‚“ ๎‚…๎‚”๎‚”๎‚๎‚ ๎‚˜๎‚Œ๎‚๎‚— ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚๎‚”๎‚‰๎‡€
RULE
157
RULE โ€” Dynamically allocate suf๎‚พcient memory space for the allocated
object
๎€ˆ๎‚“๎‚– ๎‚… ptr ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚”๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚…๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰ ptr=malloc(sizeof(*ptr)); ๎‚›๎‚Œ๎‚‰๎‚–๎‚‰๎‚š๎‚‰๎‚– ๎‚”๎‚“๎‚—๎‡‚
๎‚—๎‚๎‚†๎‚๎‚‰๎‡€
๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚…๎‚’๎‚ ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚Š๎‚–๎‚‰๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚—๎‚“๎‚“๎‚’ ๎‚…๎‚— ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰๎‡€
RULE
158
RULE โ€” Free dynamically-allocated memory as soon as possible
๎€ƒ๎‚’๎‚ ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡๎‚…๎‚๎‚๎‚๎‡‚๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚Š๎‚–๎‚‰๎‚‰๎‚ˆ ๎‚™๎‚” ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚๎‚˜ ๎‚๎‚— ๎‚’๎‚“ ๎‚๎‚“๎‚’๎‚‹๎‚‰๎‚–
๎‚’๎‚‰๎‚‰๎‚ˆ๎‚‰๎‚ˆ๎‡€
๎€–๎‚Œ๎‚๎‚— ๎‚–๎‚™๎‚๎‚‰ ๎‚‰๎‚‡๎‚Œ๎‚“๎‚‰๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚“๎‚Š ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ—๎‡€๎ˆ•๎‡€
๎€ˆ๎‚“๎‚– ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚— ๎‚—๎‚˜๎‚“๎‚–๎‚๎‚’๎‚‹ ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚…๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚–๎‚‰๎‚…๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚—๎‚‰๎‚˜ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚†๎‚‰๎‚๎‚’๎‚‹ ๎‚Š๎‚–๎‚‰๎‚‰๎‚ˆ๎‡€
RULE
159
RULE โ€” Sensitive memory areas must be reset before being freed
Warning
๎€‹๎‚˜ ๎‚๎‚— ๎‚‡๎‚–๎‚™๎‚‡๎‚๎‚…๎‚ ๎‚˜๎‚“ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚–๎‚‰๎‚—๎‚‰๎‚˜ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚—๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚๎‚— ๎‚–๎‚‰๎‚˜๎‚…๎‚๎‚’๎‚‰๎‚ˆ ๎‚“๎‚’
๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€๎‚“๎‚—๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‚‡๎‚“๎‚’๎‚—๎‚๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚Œ๎‚๎‚— ๎‚–๎‚‰๎‚—๎‚‰๎‚˜ ๎‚…๎‚— ๎‚ˆ๎‚‰๎‚…๎‚ˆ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‡‚
๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚…๎Šน๎‚‰๎‚–๎‚›๎‚…๎‚–๎‚ˆ๎‚—๎‡€ ๎€‰๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚๎‚๎‚ ๎‚—๎‚”๎‚‰๎‚…๎‚๎‚๎‚’๎‚‹๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‰๎‚š๎‚‰๎‚๎‚— ๎‚“๎‚Š ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ
๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚”๎‚™๎‚—๎‚Œ๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚†๎‚™๎‚˜ ๎‚—๎‚“๎‚‘๎‚‰๎‚˜๎‚๎‚‘๎‚‰๎‚—๎†ฝ ๎‚‰๎‚š๎‚‰๎‚’ ๎‚…๎‚˜ ๎‚… ๎‚๎‚“๎‚› ๎‚๎‚‰๎‚š๎‚‰๎‚ ๎‚“๎‚Š ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚“๎‚’๎‚‰
๎‚‘๎‚™๎‚—๎‚˜ ๎‚™๎‚’๎‚Š๎‚“๎‚–๎‚˜๎‚™๎‚’๎‚…๎‚˜๎‚‰๎‚๎‚ ๎‚–๎‚‰๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚’๎‚‰๎‡‹๎‚— ๎‚“๎‚›๎‚’ memset ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚˜๎‚Œ๎‚๎‚— ๎‚๎‚๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚๎‚’๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚๎‚‰๎‚’๎‚‡๎‚‰๎‡€
๎€‹๎‚˜ ๎‚๎‚— ๎‚…๎‚๎‚—๎‚“ ๎‚๎‚‘๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚’๎‚“๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚–๎‚‰๎‚๎‚‰๎‚…๎‚—๎‚‰ ๎‚๎‚— ๎‚“๎‚’๎‚๎‚ ๎‚…๎‚™๎‚˜๎‚Œ๎‚“๎‚–๎‚๎‚—๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡๎‚…๎‚๎‚๎‚๎‡‚๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚†๎‡‚
๎‚Ž๎‚‰๎‚‡๎‚˜๎‚—๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 125
RULE
160
RULE โ€” Do not free memory not allocated dynamically
๎€ˆ๎‚๎‚’๎‚…๎‚๎‚๎‚๎†ฝ realloc ๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚‘๎‚“๎‚ˆ๎‚๎‚Š๎‚ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡๎‚…๎‚๎‚๎‚๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’
๎‚‡๎‚…๎‚’ ๎‚๎‚’ ๎‚Š๎‚…๎‚‡๎‚˜ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚†๎‚ ๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚…๎‚—๎‚๎‚’๎‚‹ ๎‚“๎‚– ๎‚ˆ๎‚‰๎‚‡๎‚–๎‚‰๎‚…๎‚—๎‚๎‚’๎‚‹ ๎‚๎‚˜๎‚— ๎‚—๎‚๎‚ž๎‚‰๎†ฝ ๎‚†๎‚™๎‚˜
๎‚‡๎‚…๎‚’ ๎‚…๎‚๎‚—๎‚“ ๎‚Š๎‚–๎‚‰๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚”๎‚…๎‚—๎‚—๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‡€ ๎€„๎‚‰๎‚‡๎‚…๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚๎‚—๎‚๎‚— ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ
๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚๎‚’๎‚‹ ๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚ˆ๎‚“๎‚™๎‚†๎‚๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚–๎‚‰๎‚๎‚‰๎‚…๎‚—๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎†ฝ
๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚…๎‚š๎‚“๎‚๎‚ˆ๎‚‰๎‚ˆ๎‡€
RULE
161
RULE โ€” Do not change the dynamic allocation via realloc
Warning
๎€‹๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚Š๎‚…๎‚๎‚๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚‰ realloc ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚— NULL๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚–๎‚‰๎‡‚
๎‚‘๎‚…๎‚๎‚’๎‚— ๎‚๎‚’๎‚˜๎‚…๎‚‡๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚—๎‚˜๎‚๎‚๎‚ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚†๎‚๎‚‰๎‡€
Bad example
#include <stdlib .h>
void fonc( size_t len){
long *p;
p = ( long *) malloc(len * sizeof(int)); /* incorrect type */
...
p = ( long *) realloc(p,0) ; /* release of p via realloc */
...
free (p); /* double release of p */
}
Good example
#include <stdlib .h>
void fonc( size_t len){
long *p;
p = ( long *) malloc(len * sizeof(long )); /* corrected type */
...
free (p); /* realloc deleted and replaced by free */
}
15.1.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€๎€‡๎€๎ˆ๎ˆ๎‡‚๎€… ๎€ƒ๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚Š๎‚–๎‚‰๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚‘๎‚“๎‚ˆ๎‚™๎‚๎‚‰๎†ฝ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚๎‚‰๎‚š๎‚‰๎‚ ๎‚“๎‚Š ๎‚…๎‚†๎‚—๎‚˜๎‚–๎‚…๎‚‡๎‡‚
๎‚˜๎‚๎‚“๎‚’๎‡€
๎‡ฌ
๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€๎€‡๎€๎ˆ’๎ˆ๎‡‚๎€… ๎€ˆ๎‚–๎‚‰๎‚‰ ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚’๎‚“ ๎‚๎‚“๎‚’๎‚‹๎‚‰๎‚– ๎‚’๎‚‰๎‚‰๎‚ˆ๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€๎€‡๎€๎ˆ’๎ˆ“๎‡‚๎€… ๎€‘๎‚’๎‚๎‚ ๎‚Š๎‚–๎‚‰๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡๎‚…๎‚๎‚๎‚๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€๎€‡๎€๎ˆ’๎ˆ”๎‡‚๎€… ๎€ƒ๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰ ๎‚—๎‚™๎Šฒ๎‚‡๎‚๎‚‰๎‚’๎‚˜ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚Š๎‚“๎‚– ๎‚…๎‚’ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€๎€‡๎€๎ˆ’๎ˆ•๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‘๎‚“๎‚ˆ๎‚๎‚Š๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎‚— ๎‚†๎‚ ๎‚‡๎‚…๎‚๎‚๎‚๎‚’๎‚‹ ๎‚–๎‚‰๎‚…๎‚๎‚๎‚“๎‚‡๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€๎€‡๎€๎ˆ๎ˆ’๎‡‚๎€… ๎€…๎‚๎‚‰๎‚…๎‚– ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚˜๎‚“๎‚–๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚–๎‚‰๎‚™๎‚—๎‚…๎‚†๎‚๎‚‰ ๎‚–๎‚‰๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰๎‚—๎‡€
126 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€๎€‡๎€๎ˆ๎ˆ“๎‡‚๎€… ๎€„๎‚‰๎‚›๎‚…๎‚–๎‚‰ ๎‚“๎‚Š ๎‚ž๎‚‰๎‚–๎‚“๎‡‚๎‚๎‚‰๎‚’๎‚‹๎‚˜๎‚Œ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎ˆ‘๎‡€๎ˆ ๎€ƒ๎‚๎‚ ๎‚–๎‚‰๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰๎‚— ๎‚“๎‚†๎‚˜๎‚…๎‚๎‚’๎‚‰๎‚ˆ ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚†๎‚ ๎‚‘๎‚‰๎‚…๎‚’๎‚— ๎‚“๎‚Š ๎€•๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎€Ž๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—
๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚–๎‚‰๎‚๎‚‰๎‚…๎‚—๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎ˆ‘๎‡€๎ˆ‘ ๎€ƒ ๎‚†๎‚๎‚“๎‚‡๎‚ ๎‚“๎‚Š ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚“๎‚’๎‚๎‚ ๎‚†๎‚‰ ๎‚Š๎‚–๎‚‰๎‚‰๎‚ˆ ๎‚๎‚Š ๎‚๎‚˜ ๎‚›๎‚…๎‚— ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚‘๎‚‰๎‚…๎‚’๎‚— ๎‚“๎‚Š ๎‚…
๎€•๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎€Ž๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ‘๎ˆ‘๎ˆ• ๎€•๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚™๎‚’๎‚‡๎‚๎‚‰๎‚…๎‚–๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚–๎‚‰๎‚๎‚‰๎‚…๎‚—๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ‘๎ˆ“๎ˆ“ ๎€ˆ๎‚…๎‚๎‚๎‚™๎‚–๎‚‰ ๎‚˜๎‚“ ๎‚‡๎‚๎‚‰๎‚…๎‚– ๎‚Œ๎‚‰๎‚…๎‚” ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚–๎‚‰๎‚๎‚‰๎‚…๎‚—๎‚‰ ๎‡ฆ๎‡๎‚Œ๎‚‰๎‚…๎‚” ๎‚๎‚’๎‚—๎‚”๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡๎‡ง๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ”๎ˆ˜๎ˆ ๎€ˆ๎‚–๎‚‰๎‚‰ ๎‚“๎‚Š ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚’๎‚“๎‚˜ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Œ๎‚‰๎‚…๎‚”๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ–๎ˆ‘ ๎€‘๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚’ ๎‚… ๎‚–๎‚‰๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚…๎Šน๎‚‰๎‚– ๎‚‰๎‚œ๎‚”๎‚๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚– ๎‚–๎‚‰๎‚๎‚‰๎‚…๎‚—๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ๎ˆ’๎ˆ ๎€‹๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎€…๎‚…๎‚๎‚‡๎‚™๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎€„๎‚™๎Šฎ๎‚‰๎‚– ๎€•๎‚๎‚ž๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ—๎ˆ ๎€‹๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎€‘๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚› ๎‚˜๎‚“ ๎€„๎‚™๎Šฎ๎‚‰๎‚– ๎€‘๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ–๎ˆ—๎ˆ˜ ๎€—๎‚’๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚๎‚๎‚‰๎‚ˆ ๎€๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎€ƒ๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€ƒ๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚Š๎‚–๎‚‰๎‚‰๎‚ˆ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‡ฌ๎‚…๎‚‡๎‚‡๎‚Š๎‚–๎‚‰๎‚‰๎‡ญ๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€ˆ๎‚–๎‚‰๎‚‰๎‚๎‚’๎‚‹ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚˜๎‚๎‚‘๎‚‰๎‚— ๎‡ฌ๎‚ˆ๎‚†๎‚๎‚Š๎‚–๎‚‰๎‚‰๎‡ญ๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€”๎‚‰๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚“๎‚– ๎‚Š๎‚–๎‚‰๎‚‰๎‚๎‚’๎‚‹ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚›๎‚…๎‚— ๎‚’๎‚“๎‚˜ ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‡ฌ๎‚œ๎‚Š๎‚–๎‚‰๎‚‰๎‡ญ๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€–๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚ˆ๎‡‚๎‚˜๎‚“ ๎‚˜๎‚๎‚”๎‚‰ ๎‡ฌ๎‚—๎‚๎‚ž๎‚‰๎‚“๎‚Š๎‚”๎‚˜๎‚–๎‡ญ๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€ƒ๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚๎‚’๎‚—๎‚™๎Šฒ๎‚‡๎‚๎‚‰๎‚’๎‚˜ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‡ฌ๎‚๎‚’๎‚—๎‚™๎‚Š๎‚‘๎‚‰๎‚‘๎‡ญ๎‡€
15.2 Use of the sizeof operator
๎€–๎‚Œ๎‚‰ sizeof ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚๎‚— ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚๎‚’ ๎€… ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚๎‚’๎‚“๎‚› ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚๎‚’ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚๎‡€ ๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ
๎‚‡๎‚…๎‚–๎‚‰๎‚๎‚‰๎‚—๎‚— ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚‡๎‚…๎‚’ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚™๎‚’๎‚‰๎‚œ๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚…๎‚’๎‚ˆ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚๎‚’ ๎‚…๎‚’ ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚
๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚– ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚™๎‚’๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‡€
๎€‹๎‚˜ ๎‚๎‚— ๎‚”๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚…๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚๎‚”๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚– ๎‚“๎‚Š ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚…๎‚— ๎‚… ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
sizeof ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‡€ ๎€—๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚– ๎‚Œ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚ˆ๎‚š๎‚…๎‚’๎‚˜๎‚…๎‚‹๎‚‰ ๎‚“๎‚Š ๎‚†๎‚‰๎‚๎‚’๎‚‹ ๎‡Œ๎‚–๎‚‰๎‚—๎‚๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‡ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚๎‚”๎‚‰
๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚˜๎‚“ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ sizeof ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚๎‚— ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‡€
๎€‹๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘๎‚— ๎‚–๎‚‰๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚†๎‚‰๎‚–๎‚— ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚‡๎‚“๎‚‘๎‚‘๎‚“๎‚’
๎‚˜๎‚“ ๎‚™๎‚—๎‚‰๎†ฟ
n
๎‚‰๎‚๎‚˜๎‚Œ๎‚‰๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‡‚๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ pack๎†พ
n
๎‚“๎‚– ๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚”๎‚…๎‚ˆ๎‚ˆ๎‚๎‚’๎‚‹ ๎Šฑ๎‚‰๎‚๎‚ˆ๎‡€
Warning
๎€–๎‚Œ๎‚‰ pack ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‡€
๎€‹๎‚Š ๎‚…๎‚’ ๎‚…๎‚๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚†๎‚‰๎‚–๎‚— ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚๎‚— ๎‚–๎‚‰๎‚•๎‚™๎‚๎‚–๎‚‰๎‚ˆ๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‡‚๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ pack ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚“๎‚–
๎‚”๎‚…๎‚ˆ๎‚ˆ๎‚๎‚’๎‚‹ ๎Šฑ๎‚‰๎‚๎‚ˆ๎‚— ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎€๎‚“๎‚–๎‚‰๎‚“๎‚š๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚ˆ๎‚๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ sizeof(array)/sizeof(array[0]) ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚‰
๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚๎‚’ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚๎‚— ๎‚•๎‚™๎‚๎‚˜๎‚‰ ๎‚˜๎‚–๎‚…๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚‹๎‚–๎‚‰๎‚…๎‚˜ ๎‚‡๎‚…๎‚–๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚˜๎‚…๎‚๎‚‰๎‚’ ๎‚๎‚’ ๎‚๎‚˜๎‚— ๎‚™๎‚—๎‚‰๎‡€
๎€–๎‚Œ๎‚๎‚— ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚“๎‚’๎‚๎‚ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ sizeof ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚๎‚— ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚๎‚“๎‚‡๎‚ ๎‚๎‚’ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ
๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚๎‚— ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚›๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚•๎‚™๎‚๎‚˜๎‚‰ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ sizeof ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 127
๎‚๎‚— ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚”๎‚…๎‚—๎‚—๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚†๎‚‰๎‚‡๎‚…๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚๎‚— ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚›๎‚๎‚๎‚ ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚†๎‚‰ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚’๎‚“
๎‚๎‚“๎‚’๎‚‹๎‚‰๎‚– ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚๎‡€
RULE
162
RULE โ€” Correct use of the sizeof operator
๎€ƒ๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚…
sizeof
๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚“๎‚˜๎†ฟ
n
๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‡Œ๎Š๎‡๎†ฝ ๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚›๎‚๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚‰๎‚ˆ๎†พ
n
๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚ˆ๎‚‰๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚๎‚’๎‚‹๎†พ
n
๎‚†๎‚‰ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚’๎‚‹ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚๎‡€
Warning
๎€–๎‚Œ๎‚‰ sizeof ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚‘๎‚‰๎‚‘๎‡‚
๎‚“๎‚–๎‚๎‡€
Bad example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚—๎‚Œ๎‚“๎‚›๎‚— ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ sizeof ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‡€
uint8_t tab[LEN ];
typedef struct s_example {
uint32_t ui_field1;
uint8_t ui_field2;
} t_example;
int32_t i, isize;
t_exemple test;
t_exemple * ptr;
i = 5;
ptr = NULL;
isize = sizeof (i = 1234); /* the expression i= 1234 will not be processed */
/* i has the value 5, and not 1234. isize equals 4 */
isize = sizeof ( t_example); /* the value returned by sizeof is 8 for 32 - bit
alignment */
isize = sizeof (* ptr); /* the expression sizeof(* ptr) returns the size of the
structure t_example */
void crawl_tab(uint8_t tab [])
{
for (size_t i = 0; i < sizeof(tab) / sizeof (tab [0]); i++) /* tab is therefore a
pointer type parameter ! */
...
}
Tolerated example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚‹๎‚“๎‚“๎‚ˆ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰
๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚…๎‚— ๎‚… ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚๎‚ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ sizeof ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‡€
#pragma pack(push , 1) /* 1 byte alignment - NOT STANDARD - */
uint8_t tab[LEN ];
typedef struct s_example
{
uint32_t ui_field1 ;
uint8_t ui_field2 ;
} t_exemple;
#pragma pack(pop) /* return default alignment */
int32_t i;
size_t isize;
128 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
i = 5;
isize = sizeof ( int32_t);
i = 1234;
isize = sizeof ( t_example); /* the value returned by sizeof is 5 since the structure
was declared with a 1 byte alignment */
void crawl_tab(uint8_t tab[], size_t n) /* known array size */
{
for (size_t i = 0; i < n; i++) /* tab is therefore a pointer type parameter! */
...
}
15.2.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‡๎€š๎€’๎ˆ๎ˆ˜๎‡‚๎€… ๎€—๎‚—๎‚‰ ๎‚—๎‚๎‚ž๎‚‰๎‚“๎‚Š ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‡๎€š๎€’๎ˆ“๎ˆ“๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚๎‚ ๎‚“๎‚’ ๎‚—๎‚๎‚ˆ๎‚‰ ๎‚‰๎Šฎ๎‚‰๎‚‡๎‚˜๎‚— ๎‚๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ ๎‚˜๎‚“ ๎‚—๎‚๎‚ž๎‚‰๎‚“๎‚Š๎†ฝ ๎‡ ๎€ƒ๎‚๎‚๎‚‹๎‚’๎‚“๎‚Š๎†ฝ ๎‚“๎‚– ๎‡ ๎€‰๎‚‰๎‚’๎‚‰๎‚–๎‚๎‚‡๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€ƒ๎€”๎€”๎ˆ๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚…๎‚”๎‚”๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰๎‚“๎‚Š ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚˜๎‚“ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ’๎‡€๎ˆ• ๎€–๎‚Œ๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰๎‚“๎‚Š ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚…๎‚’๎‚ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ
๎‚Œ๎‚…๎‚— ๎‚”๎‚“๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚—๎‚๎‚ˆ๎‚‰ ๎‚‰๎Šฎ๎‚‰๎‚‡๎‚˜๎‚—๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€–๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚ˆ๎‡‚๎‚˜๎‚“ ๎‚˜๎‚๎‚”๎‚‰ ๎‡ฌ๎‚—๎‚๎‚ž๎‚‰๎‚“๎‚Š๎‚”๎‚˜๎‚–๎‡ญ๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ๎ˆ’๎ˆ ๎€‹๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎€…๎‚…๎‚๎‚‡๎‚™๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎€„๎‚™๎Šฎ๎‚‰๎‚– ๎€•๎‚๎‚ž๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ“๎ˆ•๎ˆ– ๎€—๎‚—๎‚‰ ๎‚“๎‚Š sizeof() ๎‚“๎‚’ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ—๎ˆ๎ˆ” ๎€„๎‚™๎Šฎ๎‚‰๎‚– ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚๎‚‰๎‚’๎‚‹๎‚˜๎‚Œ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‡€
15.3 Mandatory veri๎‚พcation of the success of a memory
allocation
๎€™๎‚Œ๎‚‰๎‚’ ๎‚… ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚‘๎‚…๎‚ˆ๎‚‰๎†ฝ ๎‚๎‚˜ ๎‚‘๎‚…๎‚ ๎‚Š๎‚…๎‚๎‚ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚— ๎‚’๎‚“ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚Š๎‚–๎‚‰๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‡€
๎€ˆ๎‚…๎‚๎‚๎‚™๎‚–๎‚‰ ๎‚˜๎‚“ ๎‚˜๎‚‰๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚›๎‚๎‚๎‚ ๎‚‡๎‚…๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚˜๎‚“ ๎‚‡๎‚–๎‚…๎‚—๎‚Œ ๎‚˜๎‚Œ๎‚‰
๎Šฑ๎‚–๎‚—๎‚˜ ๎‚˜๎‚๎‚‘๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎€—๎‚—๎‚™๎‚…๎‚๎‚๎‚๎†ฝ ๎‚๎‚Š ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚…๎‚๎‚๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚— ๎‚… NULL ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡€ ๎€‹๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰
๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚˜๎‚“ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚’๎‚“๎‚˜ NULL๎‡€
๎€‹๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚‰๎‚— ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜๎‚๎‚ ๎‚“๎‚’ ๎‚…๎‚’ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎†ฝ ๎‚–๎‚‰๎‚Š๎‚‰๎‚– ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š
๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚“๎‚– ๎‚Œ๎‚“๎‚› ๎‚˜๎‚“ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚…๎‚”๎‚”๎‚–๎‚“๎‚”๎‚–๎‚๎‚…๎‚˜๎‚‰๎‚๎‚๎‡€
RULE
163
RULE โ€” Mandatory veri๎‚พcation of the success of a memory allocation
๎€–๎‚Œ๎‚‰ ๎‚—๎‚™๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚… ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚†๎‚‰ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ๎‡€
๎€–๎‚Œ๎‚๎‚— ๎‚–๎‚™๎‚๎‚‰ ๎‚๎‚— ๎‚… ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚…๎‚ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ๎ˆ’๎‡€๎ˆ– ๎‚†๎‚™๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚…๎‚ ๎‚…๎‚˜๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚…๎‚๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚
๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚—๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 129
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚™๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚ ๎‚๎‚— ๎‚‘๎‚๎‚—๎‚—๎‚๎‚’๎‚‹๎‡€
point_t *p_point;
p_point = (point_t *)malloc ( sizeof(point_t ));
/* no check on function return */
p_point ->x = 0.0f;
p_point ->y = 0.0f;
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚™๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰
๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
point_t *p_point = NULL;
p_point = (point_T *)malloc ( sizeof(point_t ));
if (NULL != p_point) {
p_point ->x = 0.0f;
p_point ->y = 0.0f;
} else {
/* error handling */
}
15.4 Isolation of sensitive data
๎€™๎‚Œ๎‚‰๎‚’ ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚๎‚— ๎‚๎‚“๎‚…๎‚ˆ๎‚‰๎‚ˆ ๎‚๎‚’๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‡ฆ๎‚‰๎‡€๎‚‹๎‡€ ๎‚‰๎‚’๎‚‡๎‚–๎‚๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚‰๎‚๎‚—๎‡ง๎†ฝ ๎‚๎‚˜ ๎‚–๎‚‰๎‚‘๎‚…๎‚๎‚’๎‚— ๎‚๎‚’ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎Šน๎‚‰๎‚–
๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚Œ๎‚…๎‚— ๎Šฑ๎‚’๎‚๎‚—๎‚Œ๎‚‰๎‚ˆ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚๎‚˜๎‡€ ๎€ƒ๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚‡๎‚…๎‚’ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚“๎‚™๎‚– ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‡‹๎‚— ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚š๎‚๎‚…
auxiliary channels
๎ˆ๎ˆ“
๎‡€
๎€‹๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚˜๎‚“ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚–๎‚‰๎‚…๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚™๎‚—๎‚‰๎†ฟ ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚’๎‚‹ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜
๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚๎‚— ๎‚—๎‚˜๎‚“๎‚–๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚—๎‚‰๎‚”๎‚…๎‚–๎‚…๎‚˜๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚—๎‚”๎‚…๎‚‡๎‚‰๎‚—๎‡€ ๎€‹๎‚Š ๎‚… ๎‚—๎‚Œ๎‚…๎‚–๎‚‰๎‚ˆ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚–๎‚‰๎‚… ๎‚๎‚— ๎‚–๎‚‰๎‚‡๎‚๎‚‡๎‚๎‚‰๎‚ˆ๎†ฝ ๎‚‘๎‚…๎‚๎‚‰ ๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚˜
๎‚๎‚— ๎‚‰๎‚–๎‚…๎‚—๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚†๎‚‰๎‚๎‚’๎‚‹ ๎‚–๎‚‰๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎€ƒ๎‚๎‚ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚–๎‚‰๎‚…๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜๎‚๎‚ ๎‚ˆ๎‚‰๎‚๎‚‰๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚’๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚’๎‚“ ๎‚๎‚“๎‚’๎‚‹๎‚‰๎‚–
๎‚’๎‚‰๎‚‰๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚˜๎‚Œ๎‚๎‚— ๎‚ˆ๎‚…๎‚˜๎‚…๎‡€
Warning
๎€…๎‚๎‚‰๎‚…๎‚–๎‚๎‚’๎‚‹ ๎‚†๎‚™๎Šฎ๎‚‰๎‚–๎‚— ๎‚—๎‚“ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚‘๎‚…๎‚๎‚’ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚‡๎‚ ๎‚š๎‚๎‚… ๎‚… memset๎†ฝ ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ
๎‚‘๎‚…๎‚ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚๎‚ˆ๎‚‰๎‚–๎‚‰๎‚ˆ ๎‚™๎‚’๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚‡๎‚…๎‚๎‚๎‚— ๎‚‡๎‚…๎‚’ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰
๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚๎‚‰๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚…๎‚›๎‚…๎‚–๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚–๎‚๎‚—๎‚
๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚’๎‚—๎‚™๎‚๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚๎‚๎‚—
๎‚๎‚’ ๎‚•๎‚™๎‚‰๎‚—๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚–๎‚‰ ๎‚”๎‚–๎‚“๎‚”๎‚‰๎‚–๎‚๎‚ ๎‚—๎‚˜๎‚“๎‚–๎‚‰๎‚ˆ๎‡€
RULE
164
RULE โ€” Sensitive data must be isolated
๎€…๎‚Œ๎‚‰๎‚‡๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚–๎‚‰๎‚… ๎‚—๎‚˜๎‚“๎‚–๎‚๎‚’๎‚‹ ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚…๎†ฝ i.e. ๎‚‘๎‚๎‚’๎‚๎‚‘๎‚๎‚—๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚
๎‚‰๎‚œ๎‚”๎‚“๎‚—๎‚™๎‚–๎‚‰๎†ฝ ๎‚‘๎‚๎‚’๎‚๎‚‘๎‚๎‚—๎‚‰ ๎‚‡๎‚“๎‚”๎‚๎‚๎‚’๎‚‹ ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚‰๎‚๎‚‰๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚‰๎‚…๎‡ฆ๎‚—๎‡ง ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚‰๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚…
๎‚…๎‚— ๎‚—๎‚“๎‚“๎‚’ ๎‚…๎‚— ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰๎‡€
๎ˆ๎ˆ“๎‡€ ๎€–๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚…๎‚–๎‚‰ ๎‚‘๎‚…๎‚’๎‚ ๎‚๎‚๎‚๎‚™๎‚—๎‚˜๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚—๎†ฟ ๎€๎‚‰๎‚๎‚˜๎‚ˆ๎‚“๎‚›๎‚’๎†ฝ ๎€•๎‚”๎‚‰๎‚‡๎‚˜๎‚–๎‚‰๎†ฝ ๎€œ๎‚“๎‚‘๎‚†๎‚๎‚‰๎€Ž๎‚“๎‚…๎‚ˆ๎†ฝ etc.
130 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚†๎‚‰๎‚๎‚“๎‚›๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚†๎‚™๎Šฎ๎‚‰๎‚– ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚—๎‚˜๎‚“๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‰๎‚ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‡‚
๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚š๎‚‰๎‚‡๎‚˜๎‚“๎‚–๎‡€
#define WORK_SIZE 32U
#include <stdlib .h>
void process( const uint8_t *key , uint16_t key_size , const uint8_t *init ,
uint16_t init_size) {
uint8_t buffer[WORK_SIZE ];
if ((NULL == key) || ( NULL == init)) {
/* error handling */
...
}
memcpy (buffer , key , min( key_size , WORK_SIZE));
print_key (buffer);
/* buffer contains 4 bytes of the initialisation vector and the last 12 bytes of
the key */
memcpy (buffer , init , min(init_size , WORK_SIZE ));
printIV (buffer);
...
/* no secure deletion */
}
Good example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚—๎‚Œ๎‚“๎‚›๎‚— ๎‚… ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‰๎‚ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚š๎‚‰๎‚‡๎‡‚
๎‚˜๎‚“๎‚–๎‡€
#define KEY_SIZE 16U
#define IV_SIZE 4U
#include <stdlib .h>
void process( const uint8_t *key , uint16_t key_size , const uint8_t *init ,
uint16_t init_size) {
uint8_t my_key[KEY_SIZE ];
uint8_t iv[IV_SIZE ];
if ((NULL == key) || ( NULL == init)) {
/* error handling */
...
}
memcpy (my_key , key , min( key_size , KEY_SIZE));
print_key (my_key);
memcpy (iv , init , min(init_size , IV_SIZE ));
printIV (iv);
...
/* deletion of the buffers , so that the data does not remain on the stack
ATTENTION: the compiler can optimise and delete these calls , which may be
considered unnecessary.
It is therefore necessary to consult the compiler documentation in order for the
calls to be retained. */
memset (my_key , 0, KEY_SIZE);
memset (iv , 0, IV_SIZE);
}
15.4.1 References
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 131
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€๎€•๎€…๎ˆ๎ˆ—๎‡‚๎€… ๎€„๎‚‰ ๎‚‡๎‚…๎‚–๎‚‰๎‚Š๎‚™๎‚ ๎‚›๎‚Œ๎‚๎‚๎‚‰ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚๎‚’๎‚‹ ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚…๎†ฝ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— ๎‚”๎‚…๎‚—๎‚—๎‚›๎‚“๎‚–๎‚ˆ๎‚—๎†ฝ ๎‚๎‚’ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€
132 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
16
Error management
16.1 Correct use of errno
๎€–๎‚Œ๎‚‰ errno ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎†ฝ ๎‚…๎‚‡๎‚˜๎‚๎‚š๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎†ฝ <errno.h>๎†ฝ ๎‚Œ๎‚…๎‚— ๎‚˜๎‚๎‚”๎‚‰ int๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚๎‚’
๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰ ๎‚๎‚˜๎‚— ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚”๎‚“๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚๎‚’ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‡€ ๎€‹๎‚˜ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚๎‚‘๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚’๎‚˜
๎‚˜๎‚“ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰ errno ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚…๎‚’๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚๎‚ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰๎‚— ๎‚๎‚˜๎‚— ๎‚š๎‚…๎‚๎‚™๎‚‰๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚๎‚˜
๎‚๎‚— ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚…๎‚๎‚—๎‚“ ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚˜๎‚“ ๎‚‡๎‚“๎‚’๎‚—๎‚™๎‚๎‚˜ ๎‚๎‚˜๎‚— ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚—๎‚™๎‚‡๎‚Œ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
RULE
165
RULE โ€” Initialise and view the value of errno before and after any exe-
cution of a standard library function that changes its value
Bad example
#include <stdlib .h>
void try1 (const unsigned char * len)
{
unsigned long res;
res = strtoul(len ,NULL ,5); /* conversion character string to unsigned long */
/* the function strtoul writes in errno */
if (res == ULONG_MAX)
{
/* problem management */
}
...
}
Good example
#include <stdlib .h>
#include <errno .h>
void try1 (const unsigned char * len)
{
unsigned long res;
errno = 0; /* init errno */
res = strtoul(len ,NULL ,5); /* conversion character string to unsigned long */
/* strtoul written in errno */
if (res == ULONG_MAX && errno != 0) /* errno reading */
{
/* problem management */
}
...
}
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 133
16.1.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‡๎€”๎€”๎ˆ’๎ˆ๎‡‚๎€… ๎€•๎‚‰๎‚˜ ๎‚‰๎‚–๎‚–๎‚’๎‚“ ๎‚˜๎‚“ ๎‚ž๎‚‰๎‚–๎‚“ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚‡๎‚…๎‚๎‚๎‚๎‚’๎‚‹ ๎‚… ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚’๎‚“๎‚›๎‚’ ๎‚˜๎‚“ ๎‚—๎‚‰๎‚˜ ๎‚‰๎‚–๎‚–๎‚’๎‚“ ๎‚…๎‚’๎‚ˆ
๎‚‡๎‚Œ๎‚‰๎‚‡๎‚ ๎‚‰๎‚–๎‚–๎‚’๎‚“ ๎‚“๎‚’๎‚๎‚ ๎‚…๎Šน๎‚‰๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚— ๎‚… ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚Š๎‚…๎‚๎‚๎‚™๎‚–๎‚‰๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€‡๎€”๎€”๎ˆ’๎ˆ‘๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚๎‚ ๎‚“๎‚’ ๎‚๎‚’๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚“๎‚Š ๎‚‰๎‚–๎‚–๎‚’๎‚“๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€‹๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚—๎‚‰๎‚˜๎‚˜๎‚๎‚’๎‚‹ ๎‚…๎‚’๎‚ˆ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚‰๎‚–๎‚–๎‚’๎‚“ ๎‡ฌ๎‚๎‚’๎‚š๎‚‰๎‚–๎‚–๎‚’๎‚“๎‡ญ๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ“๎ˆ”๎ˆ• ๎€๎‚๎‚—๎‚—๎‚๎‚’๎‚‹ ๎€‹๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‡€
16.2 Systematic consideration of errors returned by
standard library functions
๎€๎‚“๎‚—๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚˜๎‚“ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‡‚
๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚…๎‚๎‚—๎‚“ ๎‚…๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚‰๎‚ˆ๎‡€ ๎€ˆ๎‚…๎‚๎‚๎‚™๎‚–๎‚‰ ๎‚˜๎‚“ ๎‚˜๎‚‰๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚‘๎‚…๎‚ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“
๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚‰๎‚–๎‚–๎‚“๎‚’๎‚‰๎‚“๎‚™๎‚— ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
RULE
166
RULE โ€” All errors returned by standard library functions must be handled
๎€ƒ๎‚’๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚…๎‚ˆ ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚—๎‚‰๎‚˜ ๎‚™๎‚” ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚”๎‚”๎‚–๎‚“๎‚”๎‚–๎‚๎‚…๎‚˜๎‚‰ ๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚Š๎‚“๎‚๎‡‚
๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
๎€–๎‚Œ๎‚๎‚— ๎‚–๎‚™๎‚๎‚‰ ๎‚๎‚— ๎‚… ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‡ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ๎ˆ’๎‡€๎ˆ– ๎‚†๎‚™๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚…๎‚ ๎‚…๎‚˜๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚…๎‚๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‘๎‚…๎‚’๎‚…๎‚‹๎‚‰๎‚‘๎‚‰๎‚’๎‚˜
๎‚“๎‚Š ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
Bad example
#include <stdio .h>
#include <stdlib .h>
int main( void )
{
FILE *fp = fopen("myfile.txt", "w"); /* returned value not read */
fputs ("hello\n", fp);
...
}
Good example
#include <stdio .h>
#include <stdlib .h>
int main( void )
{
FILE *fp = fopen("myfile.txt", "w");
if (fp != NULL) {
fputs ("hello\n", fp);
...
}
}
134 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
16.2.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€†๎‚๎‚–๎‡€ ๎ˆ“๎‡€๎ˆ–๎†ฟ ๎€‹๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚— ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰
๎‚˜๎‚‰๎‚—๎‚˜๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€‡๎€š๎€’๎ˆ๎ˆ‘๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚๎‚‹๎‚’๎‚“๎‚–๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€‡๎€”๎€”๎ˆ’๎ˆ’๎‡‚๎€… ๎€†๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€ˆ๎€‹๎€‘๎ˆ’๎ˆ–๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚…๎‚—๎‚—๎‚™๎‚‘๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚Š๎‚‹๎‚‰๎‚˜๎‚—๎‡ฆ๎‡ง ๎‚“๎‚– ๎‚Š๎‚‹๎‚‰๎‚˜๎‚—๎‚›๎‡ฆ๎‡ง ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚— ๎‚… ๎‚’๎‚“๎‚’๎‚‰๎‚‘๎‚”๎‚˜๎‚ ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚—๎‚™๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚Š๎‚™๎‚๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ‘๎ˆ“๎ˆ ๎€‹๎‚‘๎‚”๎‚–๎‚“๎‚”๎‚‰๎‚– ๎€Š๎‚…๎‚’๎‚ˆ๎‚๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎€—๎‚’๎‚‰๎‚œ๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎€†๎‚…๎‚˜๎‚… ๎€–๎‚๎‚”๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ‘๎ˆ”๎ˆ‘ ๎€—๎‚’๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ ๎€”๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎€˜๎‚…๎‚๎‚™๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ‘๎ˆ”๎ˆ’ ๎€‹๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎€…๎‚Œ๎‚‰๎‚‡๎‚ ๎‚“๎‚Š ๎€ˆ๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎€”๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎€˜๎‚…๎‚๎‚™๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ’๎ˆ˜๎ˆ ๎€—๎‚’๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚‰๎‚ˆ ๎€‡๎‚–๎‚–๎‚“๎‚– ๎€…๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€ˆ๎‚…๎‚๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚‰ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‡ฌ๎‚๎‚๎‚†๎‚‰๎‚–๎‚–๎‡ญ๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€ˆ๎‚“๎‚–๎‚‘๎‚๎‚’๎‚‹ ๎‚๎‚’๎‚š๎‚…๎‚๎‚๎‚ˆ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚†๎‚ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‡ฌ๎‚๎‚๎‚†๎‚”๎‚˜๎‚–๎‡ญ๎‡€
16.3 Documentation and structuring of error codes
๎€‹๎‚’๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚˜๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚˜๎‚“๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚’ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚—๎†ฝ ๎‚‰๎‚—๎‚”๎‚‰๎‡‚
๎‚‡๎‚๎‚…๎‚๎‚๎‚ ๎‚๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‘๎‚…๎‚’๎‚…๎‚‹๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎†ฝ ๎‚๎‚Š ๎‚…๎‚๎‚ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚‘๎‚‰๎‚…๎‚’๎‚๎‚’๎‚‹๎‡€
๎€ƒ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚‰๎‚‘๎‚”๎‚๎‚…๎‚˜๎‚‰ ๎‚Š๎‚“๎‚– ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎†ฝ ๎‚Š๎‚“๎‚– ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’
๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚…๎‚’๎‚ˆ๎†ฝ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚š๎‚‰๎‚’๎‚˜ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚— ๎‚‘๎‚…๎‚ ๎‚“๎‚‡๎‚‡๎‚™๎‚– ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚˜๎‚๎‚‘๎‚‰๎†ฝ
๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚๎‚“๎‚–๎‚๎‚˜๎‚ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‘๎‚…๎‚’๎‚…๎‚‹๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€
RULE
167
RULE โ€” Error code documentation
๎€ƒ๎‚๎‚ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚— ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ๎‡€ ๎€‹๎‚Š ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚—
๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚˜๎‚๎‚‘๎‚‰ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰
๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚๎‚“๎‚–๎‚๎‚˜๎‚ ๎‚Š๎‚“๎‚– ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚—๎‡€
๎€‡๎‚–๎‚–๎‚“๎‚– ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€™๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚๎‚’๎‚‹๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‡‚
๎‚˜๎‚™๎‚–๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚— ๎‚“๎Šน๎‚‰๎‚’ ๎‚๎‚’๎‚—๎‚™๎Šฒ๎‚‡๎‚๎‚‰๎‚’๎‚˜๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚— ๎‚š๎‚๎‚… ๎‚‘๎‚…๎‚—๎‚๎‚— ๎‚๎‚— ๎‚“๎‚’๎‚‰ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚๎‚๎‚˜๎‚๎‡€ ๎€–๎‚Œ๎‚‰
๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚…๎‚๎‚—๎‚“ ๎‚†๎‚‰ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚… ๎‚›๎‚…๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚˜ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚‰๎‚ˆ ๎‚›๎‚Œ๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰
๎‚‡๎‚“๎‚‘๎‚‰๎‚— ๎‚Š๎‚–๎‚“๎‚‘ ๎‚… ๎‚’๎‚“๎‚–๎‚‘๎‚…๎‚ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚“๎‚– ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚…๎‚–๎‚ ๎‚›๎‚Œ๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚…๎‚’ ๎‚‰๎‚œ๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜
๎‚Œ๎‚…๎‚— ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚Š๎‚‰๎‚–๎‚‰๎‚ˆ ๎‡ฆ๎‚†๎‚™๎Šฎ๎‚‰๎‚– ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎†ฝ etc.๎‡ง๎‡€
RECO
168
RECOMMENDATION โ€” Structuring of return codes
๎€–๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚… ๎‚›๎‚…๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚‰๎‚—๎‚—
๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚๎‚๎‚‰๎‚ˆ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚“๎‚†๎‚˜๎‚…๎‚๎‚’๎‚‰๎‚ˆ ๎‚‰๎‚…๎‚—๎‚๎‚๎‚๎†ฟ
n
๎‚‰๎‚–๎‚–๎‚“๎‚–๎†พ
n
๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚˜๎‚๎‚”๎‚‰๎†พ
n
๎‚…๎‚๎‚…๎‚–๎‚‘๎†พ
n
๎‚…๎‚๎‚…๎‚–๎‚‘ ๎‚˜๎‚๎‚”๎‚‰๎†พ
n ๎‚“๎‚๎†พ
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 135
n etc.
16.3.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€‡๎€”๎€”๎ˆ๎ˆ๎‡‚๎€… ๎€ƒ๎‚ˆ๎‚“๎‚”๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚… ๎‚‡๎‚“๎‚’๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚’๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚”๎‚–๎‚‰๎‚Œ๎‚‰๎‚’๎‚—๎‚๎‚š๎‚‰ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‡‚๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚๎‚’๎‚‹ ๎‚”๎‚“๎‚๎‚๎‚‡๎‚๎‡€
16.4 Return code of a C program depending on whether it
executed successfully
๎€–๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚’๎‚…๎‚‹๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎‚‡๎‚…๎‚ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚“๎‚’๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘ ๎‚˜๎‚“
๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚“๎‚– ๎‚Š๎‚–๎‚“๎‚‘ ๎‚“๎‚’๎‚‰ ๎‚—๎‚Œ๎‚‰๎‚๎‚ ๎‚˜๎‚“ ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚–๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚‡๎‚…๎‚’ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚‡๎‚…๎‚™๎‚—๎‚‰ ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘๎‚— ๎‚“๎‚Š ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚๎‡€ ๎€ˆ๎‚–๎‚“๎‚‘
๎‚“๎‚’๎‚‰ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘ ๎‚˜๎‚“ ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚–๎†ฝ ๎‚“๎‚– ๎‚Š๎‚–๎‚“๎‚‘ ๎‚“๎‚’๎‚‰ ๎‚—๎‚Œ๎‚‰๎‚๎‚ ๎‚˜๎‚“ ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚™๎‚˜๎‚Œ๎‚“๎‚–๎‚๎‚—๎‚‰๎‚ˆ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚–๎‚…๎‚’๎‚‹๎‚‰ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰
๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚š๎‚…๎‚–๎‚๎‚‰๎‚—๎†ฟ
n
๎‚“๎‚’ ๎€™๎‚๎‚’๎‚ˆ๎‚“๎‚›๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚Œ๎‚‰๎‚๎‚ cmd.exe ๎‚…๎‚™๎‚˜๎‚Œ๎‚“๎‚–๎‚๎‚—๎‚‰๎‚— ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎ˆ’๎ˆ‘๎‡‚๎‚†๎‚๎‚˜ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚–๎‚— ๎‡ฆ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ERRORLEVEL
๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‡ง๎†พ
n ๎‚“๎‚’ ๎€Ž๎‚๎‚’๎‚™๎‚œ๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚Œ๎‚‰๎‚๎‚ ๎‚…๎‚™๎‚˜๎‚Œ๎‚“๎‚–๎‚๎‚—๎‚‰๎‚— ๎‚… ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎ˆ ๎‚…๎‚’๎‚ˆ ๎ˆ‘๎ˆ”๎ˆ” ๎‡ฆ๎‚‰๎‚š๎‚‰๎‚’ ๎‚๎‚Š ๎‚—๎‚“๎‚‘๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚–๎‚‰๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚–
๎‚—๎‚๎‚‹๎‚’๎‚…๎‚๎‚—๎†พ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚๎‚— ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚š๎‚๎‚… ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ $?๎‡ง๎‡€
๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎ˆ ๎‚…๎‚’๎‚ˆ ๎ˆ๎ˆ‘๎ˆ– ๎‚”๎‚–๎‚“๎‚˜๎‚‰๎‚‡๎‚˜๎‚— ๎‚…๎‚‹๎‚…๎‚๎‚’๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚๎‚—๎‚๎‚— ๎‚“๎‚Š ๎‚‘๎‚“๎‚ˆ๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‡ฆ๎‚†๎‚ ๎‚˜๎‚๎‚”๎‚‰
๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‡ง ๎‚“๎‚– ๎‚‘๎‚๎‚—๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚”๎‚–๎‚‰๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎†ฟ
n
๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎ˆ ๎‚…๎‚’๎‚ˆ ๎ˆ๎ˆ‘๎ˆ– ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚ˆ ๎‚“๎‚š๎‚‰๎‚– ๎ˆ– ๎‚†๎‚๎‚˜๎‚—๎†พ
n
๎‚˜๎‚Œ๎‚‰๎‚ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎‚›๎‚Œ๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚— ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚“๎‚– ๎‚™๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ๎‡€
RULE
169
RULE โ€” Return code of a C program according to the result of its execution
๎€–๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚Š ๎‚… ๎€… ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚… ๎‚‘๎‚‰๎‚…๎‚’๎‚๎‚’๎‚‹ ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰
๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚Œ๎‚…๎‚— ๎‚–๎‚™๎‚’ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚“๎‚– ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚Œ๎‚…๎‚— ๎‚“๎‚‡๎‚‡๎‚™๎‚–๎‚–๎‚‰๎‚ˆ๎†ฟ
n ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎ˆ ๎‚…๎‚’๎‚ˆ ๎ˆ๎ˆ‘๎ˆ–๎†พ
n
๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎ˆ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚›๎‚…๎‚— ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚—๎†พ
n
๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎ˆ‘ ๎‚๎‚— ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚๎‚๎‚ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎€—๎‚’๎‚๎‚œ ๎‚˜๎‚“ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰ ๎‚…๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚”๎‚…๎‚—๎‚—๎‚‰๎‚ˆ
๎‚…๎‚— ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‡€
๎€–๎‚Œ๎‚‰ ๎‚‘๎‚‰๎‚…๎‚’๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‡‹๎‚— ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚๎‚˜๎‚— ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
Bad example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚— ๎‚… ๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎€™๎‚๎‚’๎‚ˆ๎‚“๎‚›๎‚— ๎‚…๎‚’๎‚ˆ ๎€Ž๎‚๎‚’๎‚™๎‚œ๎‡€ ๎€–๎‚Œ๎‚‰
๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‡‚๎ˆ ๎‚๎‚— ๎‚๎‚’ ๎‚Š๎‚…๎‚‡๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎ˆ‘๎ˆ”๎ˆ” ๎‚“๎‚’ ๎€Ž๎‚๎‚’๎‚™๎‚œ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚…๎‚—๎‚Œ ๎‚—๎‚Œ๎‚‰๎‚๎‚๎‡€
int main(int argc , char * argv []) {
if (argc != 2) {
/* incorrect number of arguments */
return -1; /* the return code will not be interpreted correctly on Linux */
}
...
return 0;
136 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
}
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚— ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚”๎‚“๎‚—๎‚‰ ๎‚… ๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘๎‡€
#define RESULT_OK (0U)
#define ARG_ERROR (2U)
int main(int argc , char * argv []) {
if (argc != 2) {
/* incorrect number of arguments */
return ARG_ERROR;
}
...
return RESULT_OK;
}
16.5 Ending of a C program following an error
๎€™๎‚Œ๎‚‰๎‚’ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚‰๎‚œ๎‚๎‚˜ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚— ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚… ๎€… ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‘๎‚…๎‚๎‚‰๎‚— ๎‚๎‚˜ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚—๎‚‰๎‚˜ ๎‚™๎‚” ๎‚˜๎‚‰๎‚—๎‚˜๎‚— ๎‚Š๎‚“๎‚–
๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‚‰๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‡€ ๎€‡๎‚–๎‚–๎‚“๎‚– ๎‚‘๎‚…๎‚’๎‚…๎‚‹๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚…๎‚–๎‚–๎‚๎‚‰๎‚ˆ ๎‚“๎‚™๎‚˜ ๎‚™๎‚—๎‚๎‚’๎‚‹
๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚—๎‡€ ๎€‹๎‚Š ๎‚… ๎‚‡๎‚–๎‚๎‚˜๎‚๎‚‡๎‚…๎‚ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚๎‚— ๎‚‰๎‚’๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚–๎‚‰๎‚ˆ๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚‡๎‚…๎‚๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰
abort() ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ _Exit() ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‡ฆ๎€…๎ˆ˜๎ˆ˜๎‡ง ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚›๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚›๎‚…๎‚— ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ๎‡€ ๎€‹๎‚’ ๎‚Š๎‚…๎‚‡๎‚˜๎†ฝ
๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚˜๎‚›๎‚“ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ cleanly๎†ฝ i.e. ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚†๎‚๎‚”๎‚…๎‚—๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚“๎‚–๎‚‘๎‚…๎‚ ๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚๎‚“๎‚’
๎‚–๎‚“๎‚™๎‚˜๎‚๎‚’๎‚‰๎‚— ๎‡ฆ๎‚‡๎‚๎‚“๎‚—๎‚๎‚’๎‚‹ ๎Šฑ๎‚๎‚‰๎‚—๎†ฝ ๎‚ˆ๎‚‰๎‚๎‚‰๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚‰๎‚‘๎‚”๎‚“๎‚–๎‚…๎‚–๎‚ ๎Šฑ๎‚๎‚‰๎‚—๎†ฝ ๎‚›๎‚–๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚ˆ๎‚…๎‚˜๎‚…๎†ฝ etc.๎‡ง๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚˜๎‚–๎‚…๎‚‡๎‚‰๎‚ˆ ๎‚†๎‚
๎‚‘๎‚‰๎‚…๎‚’๎‚— ๎‚“๎‚Š ๎‚…๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚๎‚’ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ main()๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚›๎‚๎‚๎‚ ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‡€
RECO
170
RECOMMENDATION โ€” Give preference to error returns via return codes in
the main function
๎€ƒ ๎€… ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚… ๎‚‘๎‚๎‚’๎‚๎‚‘๎‚™๎‚‘ main() ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€‡๎‚–๎‚–๎‚“๎‚– ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚†๎‚ ๎‚…
๎‚ˆ๎‚‰๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‡ฆ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ๎‡ง ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
RULE
171
RULE โ€” Do not use the abort() or _Exit() functions
๎€–๎‚Œ๎‚‰ exit() ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎‚— ๎‚๎‚’ ๎‚… ๎‚’๎‚“๎‚–๎‚‘๎‚…๎‚ ๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚…๎‚’๎‚ˆ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜ ๎‚“๎‚’
๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚‰๎‚œ๎‚๎‚˜ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚‰๎‚œ๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚š๎‚‰๎‚๎‚ ๎‚Š๎‚–๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚—
๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚‡๎‚…๎‚’ ๎‚‘๎‚…๎‚๎‚‰ ๎‚๎‚˜ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‡€
RECO
172
RECOMMENDATION โ€” Limit calls to exit()
๎€…๎‚…๎‚๎‚๎‚— ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ exit() ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚“๎‚š๎‚‰๎‚–๎‚™๎‚—๎‚‰๎‚ˆ๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–
๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰๎‚‘ ๎‚›๎‚Œ๎‚‰๎‚–๎‚‰๎‚š๎‚‰๎‚– ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚…๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚๎‚’ ๎‚Š๎‚™๎‚’๎‚‡๎‡‚
๎‚˜๎‚๎‚“๎‚’๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 137
๎€ˆ๎‚๎‚’๎‚…๎‚๎‚๎‚๎†ฝ ๎‚˜๎‚Œ๎‚‰ setjmp() ๎‚…๎‚’๎‚ˆ longjump() ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ setjmp.h ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚‘๎‚…๎‚๎‚’๎‚๎‚ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚–
๎‚‰๎‚œ๎‚‡๎‚‰๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚๎‚’๎‚‹ ๎‚๎‚’ ๎€… ๎‚‡๎‚…๎‚’ ๎‚‰๎‚…๎‚—๎‚๎‚๎‚ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚…๎‚’๎‚ˆ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎€‹๎‚’ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚™๎‚—๎‚‰ ๎‚‡๎‚–๎‚‰๎‚…๎‚˜๎‚‰๎‚— ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚—๎‚๎‚‹๎‚’๎‚…๎‚ ๎‚‘๎‚…๎‚’๎‚…๎‚‹๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€
RULE
173
RULE โ€” Do not use the setjmp() and longjump() functions
Bad example
#include <stdlib .h>
#include <stdio .h>
int read_file( void)
{
FILE *f = fopen("C:\\myfile.txt", "w");
if (NULL == f)
{
/* problem when opening file */
_Exit (12); /* not authorized */
}
fprintf (f, "%s", " blablabla");
...
abort (); /* not authorized */
...
return 0;
}
int main( void )
{
int val = read_file();
...
return 1;
}
Good example
#include <stdlib .h>
#include <stdio .h>
int read_file( void)
{
FILE *f = fopen("C:\\myfile.txt", "w");
if (NULL == f)
{
/* problem when opening file */
return 12; /* error code documented for this problem */
}
fprintf (f, "%s", " blablabla");
...
return 10; /* other documented error code */
...
return 0; /* no problem */
}
int main( void )
{
int val = read_file();
if (val == 0)
{ /* no problem in the function */
...
return 0;
}
else
{ /* error handling according to the error code */
...
return 1;
138 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
}
}
16.5.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€•๎€‹๎€‰๎ˆ’๎ˆ๎‡‚๎€… ๎€…๎‚…๎‚๎‚ ๎‚“๎‚’๎‚๎‚ ๎‚…๎‚—๎‚๎‚’๎‚‡๎‚Œ๎‚–๎‚“๎‚’๎‚“๎‚™๎‚— ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚—๎‚๎‚‹๎‚’๎‚…๎‚ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚‰๎‚–๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€‡๎€”๎€”๎ˆ๎ˆ๎‡‚๎€… ๎€ƒ๎‚ˆ๎‚“๎‚”๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚… ๎‚‡๎‚“๎‚’๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚’๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚”๎‚–๎‚‰๎‚Œ๎‚‰๎‚’๎‚—๎‚๎‚š๎‚‰ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‡‚๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚๎‚’๎‚‹ ๎‚”๎‚“๎‚๎‚๎‚‡๎‚๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€‡๎€”๎€”๎ˆ๎ˆ“๎‡‚๎€… ๎€…๎‚Œ๎‚“๎‚“๎‚—๎‚‰ ๎‚…๎‚’ ๎‚…๎‚”๎‚”๎‚–๎‚“๎‚”๎‚–๎‚๎‚…๎‚˜๎‚‰ ๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚˜๎‚–๎‚…๎‚˜๎‚‰๎‚‹๎‚๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€‡๎€”๎€”๎ˆ๎ˆ•๎‡‚๎€… ๎€—๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚– ๎‚“๎‚Š ๎‚…๎‚—๎‚—๎‚‰๎‚–๎‚˜๎‡ฆ๎‡ง ๎‚…๎‚’๎‚ˆ ๎‚…๎‚†๎‚“๎‚–๎‚˜๎‡ฆ๎‡ง๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€…๎‚…๎‚๎‚๎‚๎‚’๎‚‹ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎€… ๎€•๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎€Ž๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚’ abort๎†ฝ _Exit ๎‚…๎‚’๎‚ˆ signal ๎‚Š๎‚–๎‚“๎‚‘
๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚… ๎‚—๎‚๎‚‹๎‚’๎‚…๎‚ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚‰๎‚– ๎‡ฌ๎‚…๎‚—๎‚๎‚’๎‚‡๎‚—๎‚๎‚‹๎‡ญ๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ๎€…๎‚…๎‚๎‚๎‚๎‚’๎‚‹ signal ๎‚Š๎‚–๎‚“๎‚‘ ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚–๎‚™๎‚”๎‚˜๎‚๎‚†๎‚๎‚‰ ๎‚—๎‚๎‚‹๎‚’๎‚…๎‚ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚‰๎‚–๎‚— ๎‡ฌ๎‚—๎‚๎‚‹๎‚‡๎‚…๎‚๎‚๎‡ญ๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ“๎ˆ–๎ˆ˜ ๎€•๎‚๎‚‹๎‚’๎‚…๎‚ ๎€Š๎‚…๎‚’๎‚ˆ๎‚๎‚‰๎‚– ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚… ๎€๎‚“๎‚’๎‡‚๎‚–๎‚‰๎‚‰๎‚’๎‚˜๎‚–๎‚…๎‚’๎‚˜ ๎€ˆ๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 139
17
Standard library
17.1 Prohibited standard library header ๎‚พles
๎€•๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚“๎‚’๎‚๎‚ ๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚…๎‚ˆ๎‚๎‚‡๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚™๎‚๎‚‰๎‚— ๎‚“๎‚–
๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚‹๎‚™๎‚๎‚ˆ๎‚‰๎†ฟ
n
setjmp.h๎†พ
n
stdarg.h๎‡€
๎€–๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚š๎‚๎‚“๎‚๎‚…๎‚˜๎‚‰ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚†๎‚“๎‚š๎‚‰ ๎‚–๎‚™๎‚๎‚‰๎‚—๎‡€
๎€–๎‚Œ๎‚‰ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ <stdarg.h>๎†ฝ ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎€…๎ˆ˜๎ˆ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎†ฝ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚— ๎‚… ๎‚˜๎‚๎‚”๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚— ๎ˆ’
๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚—๎†ฟ va_start๎†ฝ va_arg๎†ฝ va_end๎‡€ ๎€ƒ ๎‚’๎‚‰๎‚› ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‡ฆva_copy๎‡ง ๎‚๎‚— ๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎€…๎ˆ˜๎ˆ˜๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚”๎‚™๎‚–๎‚”๎‚“๎‚—๎‚‰
๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚๎‚— ๎‚˜๎‚“ ๎‚…๎‚๎‚๎‚“๎‚› ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎‡€
๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚Š๎‚‰๎‚…๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚‡๎‚…๎‚’๎†ฝ ๎‚๎‚’ ๎‚‘๎‚…๎‚’๎‚ ๎‚‡๎‚…๎‚—๎‚‰๎‚—๎†ฝ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€
๎€‹๎‚’๎‚‡๎‚“๎‚’๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚’๎‚˜ ๎‚˜๎‚๎‚”๎‚๎‚’๎‚‹ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚๎‚ ๎‚“๎‚Š ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚ˆ๎‚๎‚‡ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚’ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚™๎‚’๎‚‰๎‚œ๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚– ๎‚‰๎‚š๎‚‰๎‚’ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€
RULE
174
RULE โ€” Do not use the setjmp.h and stdarg.h standard libraries
17.1.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎ˆ–๎‡€๎ˆ ๎€–๎‚Œ๎‚‰ ๎‚Š๎‚‰๎‚…๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚“๎‚Š ๎Š”๎‚—๎‚˜๎‚ˆ๎‚…๎‚–๎‚‹๎‡€๎‚Œ๎Š• ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ๎ˆ๎‡‚๎€… ๎€๎‚…๎‚๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚…๎‚‡๎‚˜ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚›๎‚–๎‚๎‚˜๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚…๎‚๎‚๎‚‰๎‚– ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚ˆ๎‚๎‚‡ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€†๎€…๎€Ž๎ˆ๎ˆ๎‡‚๎€… ๎€—๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚—๎‚—๎‚™๎‚‰๎‚— ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚ˆ๎‚๎‚‡ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€๎€•๎€…๎ˆ’๎ˆ˜๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚…๎‚๎‚ ๎‚š๎‚…๎‡ ๎‚…๎‚–๎‚‹๎‡ฆ๎‡ง ๎‚“๎‚’ ๎‚… ๎‚š๎‚…๎‡ ๎‚๎‚๎‚—๎‚˜ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚Œ๎‚…๎‚— ๎‚…๎‚’ ๎‚๎‚’๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎ˆ‘๎ˆ๎‡€๎ˆ“ ๎€–๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎Š”๎‚—๎‚‰๎‚˜๎‚Ž๎‚‘๎‚”๎‡€๎‚Œ๎Š• ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€๎€•๎€…๎ˆ‘๎ˆ‘๎‡‚๎€… ๎€—๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚˜๎‚Ž๎‚‘๎‚”๎‡ฆ๎‡ง๎†ฝ ๎‚๎‚“๎‚’๎‚‹๎‚Ž๎‚‘๎‚”๎‡ฆ๎‡ง ๎‚Š๎‚…๎‚‡๎‚๎‚๎‚๎‚˜๎‚ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚‰๎‚๎‚๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€‡๎€”๎€”๎ˆ๎ˆ“๎‡‚๎€… ๎€…๎‚Œ๎‚“๎‚“๎‚—๎‚‰ ๎‚…๎‚’ ๎‚…๎‚”๎‚”๎‚–๎‚“๎‚”๎‚–๎‚๎‚…๎‚˜๎‚‰ ๎‚˜๎‚‰๎‚–๎‚‘๎‚๎‚’๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚˜๎‚–๎‚…๎‚˜๎‚‰๎‚‹๎‚๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€‡๎€”๎€”๎ˆ๎ˆ”๎‡‚๎€… ๎€ƒ๎‚”๎‚”๎‚๎‚๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚’๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚”๎‚–๎‚“๎‚š๎‚๎‚ˆ๎‚‰ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚ˆ๎‚๎‚‡๎‚˜๎‚…๎‚˜๎‚๎‚’๎‚‹
๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚๎‚’๎‚‹๎‡€
140 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
17.2 Not recommended standard libraries
๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‚‰๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚๎‚๎‚‘๎‚๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚–๎‚‰๎‚˜๎‚…๎‚๎‚’๎‚‰๎‚ˆ ๎‚“๎‚’๎‚๎‚ ๎‚๎‚Š ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚๎†ฟ float.h๎†ฝ complex.h๎†ฝ
fenv.h ๎‚…๎‚’๎‚ˆ math.h๎‡€
RECO
175
RECOMMENDATION โ€” Limit the use of standard libraries handling ๎‚ฟoating-
point numbers
๎€–๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‚‰๎‚— float.h๎†ฝ complex.h๎†ฝ fenv.h ๎‚…๎‚’๎‚ˆ math.h ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚“๎‚’๎‚๎‚ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ
๎‚๎‚Š ๎‚…๎‚†๎‚—๎‚“๎‚๎‚™๎‚˜๎‚‰๎‚๎‚ ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚…๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚ˆ๎‚๎‚‹๎‚๎‚˜๎‚…๎‚ ๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹๎‡€
17.2.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎ˆ‘๎ˆ๎‡€๎ˆ๎ˆ ๎€–๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎Š”๎‚˜๎‚‹๎‚‘๎‚…๎‚˜๎‚Œ๎‡€๎‚Œ๎Š• ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎ˆ‘๎ˆ๎‡€๎ˆ๎ˆ‘ ๎€–๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎Š”๎‚Š๎‚‰๎‚’๎‚š๎‡€๎‚Œ๎Š• ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€ˆ๎€Ž๎€’๎ˆ’๎ˆ‘๎‡‚๎€… ๎€’๎‚–๎‚‰๎‚š๎‚‰๎‚’๎‚˜ ๎‚“๎‚– ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜ ๎‚ˆ๎‚“๎‚‘๎‚…๎‚๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚–๎‚…๎‚’๎‚‹๎‚‰ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚๎‚’ ๎‚‘๎‚…๎‚˜๎‚Œ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€ˆ๎€Ž๎€’๎ˆ๎ˆ’๎‡‚๎€… ๎€†๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚‰ ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚—๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ—๎ˆ‘ ๎€‹๎‚’๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚‡๎‚…๎‚๎‚‡๎‚™๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
17.3 Prohibited standard library functions
๎€‘๎‚˜๎‚Œ๎‚‰๎‚– ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‚‰๎‚— ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚ˆ๎‚…๎‚’๎‚‹๎‚‰๎‚–๎‚“๎‚™๎‚— ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰ atoi()๎†ฝ atol()๎†ฝ atof() ๎‚…๎‚’๎‚ˆ atoll() ๎‚Š๎‚™๎‚’๎‚‡๎‡‚
๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š stdlib.h ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚‡๎‚…๎‚’๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ๎‡€
๎€–๎‚Œ๎‚‰ strto*() ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚”๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚–๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚…๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚๎‚—๎‚ ๎‚“๎‚Š
๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€
RULE
176
RULE โ€” Do not use the functions atoi(), atol(), atof()
and atoll() from the library stdlib.h
๎€‡๎‚•๎‚™๎‚๎‚š๎‚…๎‚๎‚‰๎‚’๎‚˜ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— strto*() ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎€–๎‚Œ๎‚‰ rand() ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Š๎‚“๎‚– ๎‚”๎‚—๎‚‰๎‚™๎‚ˆ๎‚“๎‡‚๎‚–๎‚…๎‚’๎‚ˆ๎‚“๎‚‘ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚‹๎‚๎‚š๎‚‰
๎‚…๎‚’๎‚ ๎‚‹๎‚™๎‚…๎‚–๎‚…๎‚’๎‚˜๎‚‰๎‚‰ ๎‚…๎‚— ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚•๎‚™๎‚…๎‚๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚–๎‚…๎‚’๎‚ˆ๎‚“๎‚‘๎‚’๎‚‰๎‚—๎‚—๎‡€
RULE
177
RULE โ€” Do not use the rand() function of the standard library
17.3.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€–๎‚Œ๎‚‰ atof๎†ฝ atoi๎†ฝ atol ๎‚…๎‚’๎‚ˆ atoll ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€‡๎€”๎€”๎ˆ๎ˆ–๎‡‚๎€… ๎€’๎‚–๎‚‰๎‚Š๎‚‰๎‚– ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚—๎‚™๎‚”๎‚”๎‚“๎‚–๎‚˜ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚๎‚’๎‚‹ ๎‚“๎‚š๎‚‰๎‚– ๎‚‰๎‚•๎‚™๎‚๎‚š๎‚…๎‚๎‚‰๎‚’๎‚˜ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚ˆ๎‚“๎‚’๎‡‹๎‚˜๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡ ๎€๎€•๎€…๎ˆ‘๎ˆ”๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚๎‚’๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚‰ ๎‚“๎‚– ๎‚›๎‚‰๎‚…๎‚ ๎‚‡๎‚–๎‚๎‚”๎‚˜๎‚“๎‚‹๎‚–๎‚…๎‚”๎‚Œ๎‚๎‚‡ ๎‚…๎‚๎‚‹๎‚“๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚—๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 141
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎€๎€•๎€…๎ˆ’๎ˆ๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ rand() ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚“๎‚– ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚”๎‚—๎‚‰๎‚™๎‚ˆ๎‚“๎‚–๎‚…๎‚’๎‚ˆ๎‚“๎‚‘ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚—๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ’๎ˆ‘๎ˆ– ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚… ๎€„๎‚–๎‚“๎‚๎‚‰๎‚’ ๎‚“๎‚– ๎€”๎‚๎‚—๎‚๎‚ ๎€…๎‚–๎‚๎‚”๎‚˜๎‚“๎‚‹๎‚–๎‚…๎‚”๎‚Œ๎‚๎‚‡ ๎€ƒ๎‚๎‚‹๎‚“๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ’๎ˆ’๎ˆ— ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎€…๎‚–๎‚๎‚”๎‚˜๎‚“๎‚‹๎‚–๎‚…๎‚”๎‚Œ๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎€™๎‚‰๎‚…๎‚ ๎€’๎‚—๎‚‰๎‚™๎‚ˆ๎‚“๎‡‚๎‚–๎‚…๎‚’๎‚ˆ๎‚“๎‚‘ ๎€๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎€‰๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎‡ฆ๎€’๎€”๎€๎€‰๎‡ง๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ–๎ˆ• ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚”๎‚“๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚๎‚๎‚ ๎‚ˆ๎‚…๎‚’๎‚‹๎‚‰๎‚–๎‚“๎‚™๎‚— ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
17.4 Choice between different versions of standard library
functions
๎€™๎‚Œ๎‚‰๎‚’ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚“๎Šฎ๎‚‰๎‚–๎‚— ๎‚… ๎‡Œ๎‚๎‚‰๎‚—๎‚— ๎‚ˆ๎‚…๎‚’๎‚‹๎‚‰๎‚–๎‚“๎‚™๎‚—๎‡ ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‡ก ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚’๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚˜
๎‚…๎‚ˆ๎‚ˆ๎‚— ๎‚‰๎‚œ๎‚˜๎‚–๎‚… ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚ ๎‡ก๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚Š๎‚…๎‚š๎‚“๎‚–๎‚‰๎‚ˆ๎‡€
Warning
๎€™๎‚‰ ๎‚–๎‚‰๎‚Š๎‚‰๎‚– ๎‚˜๎‚“ ๎‡Œ๎‚๎‚‰๎‚—๎‚— ๎‚ˆ๎‚…๎‚’๎‚‹๎‚‰๎‚–๎‚“๎‚™๎‚—๎‡ ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚†๎‚‰๎‚‡๎‚…๎‚™๎‚—๎‚‰ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚Š๎‚“๎‚– ๎‚๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚‡๎‚‰ ๎‚…๎‚ˆ๎‚ˆ ๎‚… ๎‚๎‚๎‚‘๎‚๎‚˜
๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚๎‚’๎‚”๎‚™๎‚˜ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚‡๎‚…๎‚’ ๎‚—๎‚˜๎‚๎‚๎‚ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚๎‚’ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚“๎‚– ๎‚™๎‚’๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ
๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‚—๎‡€
Information
๎€‹๎‚’ ๎‚๎‚…๎‚˜๎‚‰๎‚– ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‡ฆ๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚…๎‚๎‚๎‚ ๎‚Š๎‚“๎‚– ๎€…๎ˆ๎ˆ๎‡ง๎†ฝ ๎‚’๎‚‰๎‚› ๎‚…๎‚’๎‚ˆ ๎‚‹๎‚‰๎‚’๎‚™๎‚๎‚’๎‚‰๎‚๎‚ ๎‚‘๎‚“๎‚–๎‚‰
๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚‰ ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚”๎‚–๎‚“๎‚”๎‚“๎‚—๎‚‰๎‚ˆ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚…๎‚— strcpy_s()๎‡€
๎€•๎‚˜๎‚–๎‚๎‚’๎‚‹ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚๎‚’๎‚‹ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ strxx ๎‚›๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚•๎‚™๎‚๎‚š๎‚…๎‚๎‚‰๎‚’๎‚˜ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— strnxx
๎‚›๎‚Œ๎‚‰๎‚’ ๎‚๎‚˜ ๎‚๎‚— ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚๎‚๎‚‘๎‚๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‚— ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚‰๎‚ˆ๎‡€
RULE
178
RULE โ€” Use the โ€œmore secureโ€ versions for standard library functions
๎€™๎‚Œ๎‚‰๎‚’ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚‰๎‚œ๎‚๎‚—๎‚˜๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‡Œ๎‚‘๎‚“๎‚–๎‚‰ ๎‚—๎‚‰๎‡‚
๎‚‡๎‚™๎‚–๎‚‰๎‡ ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
๎€Ž๎‚๎‚๎‚‰๎‚›๎‚๎‚—๎‚‰๎†ฝ ๎‚…๎‚๎‚ ๎‚“๎‚†๎‚—๎‚“๎‚๎‚‰๎‚˜๎‚‰ ๎‚“๎‚– ๎‚“๎‚™๎‚˜๎‚ˆ๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚†๎‚‰๎‚—๎‚˜ ๎‚๎‚’๎‚“๎‚›๎‚’ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜
๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ gets() ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚ˆ๎‚‰๎‚”๎‚–๎‚‰๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚Œ๎‚๎‚–๎‚ˆ ๎‚˜๎‚‰๎‚‡๎‚Œ๎‚’๎‚๎‚‡๎‚…๎‚ ๎‚”๎‚…๎‚˜๎‚‡๎‚Œ ๎‚“๎‚Š ๎€…๎ˆ˜๎ˆ˜ ๎‡ฌ๎€ƒ๎‚’๎‚—๎‚๎€…๎ˆ˜๎ˆ˜๎‡ญ ๎‚…๎‚’๎‚ˆ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚›๎‚…๎‚—
๎‚–๎‚‰๎‚‘๎‚“๎‚š๎‚‰๎‚ˆ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚—๎‚™๎‚†๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚˜ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‚—๎‡€
RULE
179
RULE โ€” Do not use obsolete library functions or those which become ob-
solete in subsequent standards
RULE
180
RULE โ€” Do not use library functions that handle buffers without taking the
buffer size as an argument
142 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
17.4.1 References
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€’๎€”๎€‡๎ˆ๎ˆ˜๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚ˆ๎‚‰๎‚”๎‚–๎‚‰๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚– ๎‚“๎‚†๎‚—๎‚“๎‚๎‚‰๎‚—๎‚‡๎‚‰๎‚’๎‚˜ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ ๎€”๎‚‰๎‚‡๎‡€ ๎€๎€•๎€…๎ˆ‘๎ˆ“๎‡‚๎€… ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚ˆ๎‚‰๎‚”๎‚–๎‚‰๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚– ๎‚“๎‚†๎‚—๎‚“๎‚๎‚‰๎‚—๎‚‡๎‚‰๎‚’๎‚˜ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ‘๎ˆ ๎€‹๎‚’๎‚—๎‚™๎Šฒ๎‚‡๎‚๎‚‰๎‚’๎‚˜ ๎‚๎‚’๎‚”๎‚™๎‚˜ ๎‚š๎‚…๎‚๎‚๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ๎ˆ‘๎ˆ ๎€„๎‚™๎Šฎ๎‚‰๎‚– ๎€…๎‚“๎‚”๎‚ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚๎‚’๎‚‹ ๎€•๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎€‹๎‚’๎‚”๎‚™๎‚˜๎‡ฆ๎‡‹๎€…๎‚๎‚…๎‚—๎‚—๎‚๎‚‡ ๎€„๎‚™๎Šฎ๎‚‰๎‚– ๎€‘๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‡‹๎‡ง๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ–๎ˆ• ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚”๎‚“๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚๎‚๎‚ ๎‚ˆ๎‚…๎‚’๎‚‹๎‚‰๎‚–๎‚“๎‚™๎‚— ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ•๎ˆ—๎ˆ“ ๎€ˆ๎‚…๎‚๎‚๎‚™๎‚–๎‚‰ ๎‚˜๎‚“ ๎‚”๎‚–๎‚“๎‚š๎‚๎‚ˆ๎‚‰ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚…๎‚๎‚๎‚˜๎‚๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 143
18
Analysis, evaluation of the code
18.1 Proofreading of the code
๎€‹๎‚˜ ๎‚๎‚— ๎‚‹๎‚“๎‚“๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚…๎‚’๎‚ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–๎†ฝ ๎‚…๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚‹๎‚Œ ๎‚’๎‚“๎‚˜ ๎‚‘๎‚…๎‚’๎‚ˆ๎‚…๎‚˜๎‚“๎‚–๎‚๎†ฝ ๎‚˜๎‚“ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚–๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚Œ๎‚–๎‚“๎‚™๎‚‹๎‚Œ ๎‚…๎‚˜ ๎‚๎‚‰๎‚…๎‚—๎‚˜ ๎‚“๎‚’๎‚‡๎‚‰
๎‚†๎‚ ๎‚… ๎‚ˆ๎‚‰๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚”๎‚–๎‚“๎‚“๎‚Š๎‚–๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎‚“๎‚– ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚˜๎‚“ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚๎‚’๎‚˜๎‚…๎‚๎‚’๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚๎‚…๎‚–๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚๎‚–
๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€
GOOD
PRACTICE
181
GOOD PRACTICE โ€” All code should be proofread
18.2 Indentation of long expressions
๎€ˆ๎‚“๎‚– ๎‚๎‚“๎‚’๎‚‹ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚…๎‚ˆ๎‚‰๎‚•๎‚™๎‚…๎‚˜๎‚‰ ๎‚๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚š๎‚‰๎‚–๎‚ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰
๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‚— ๎‚Š๎‚“๎‚– ๎‚๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚—
๎‚…๎‚’๎‚ˆ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚‘๎‚“๎‚–๎‚‰ ๎Šท๎‚‰๎‚œ๎‚๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚Š๎‚“๎‚– ๎‚๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚…๎‚† ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‡€
RECO
182
RECOMMENDATION โ€” Indentation of long expressions
๎€™๎‚Œ๎‚‰๎‚’ ๎‚… ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚– ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚—๎‚”๎‚–๎‚‰๎‚…๎‚ˆ ๎‚“๎‚š๎‚‰๎‚– ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚๎‚๎‚’๎‚‰๎‚—๎†ฝ ๎‚๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚
๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚Š๎‚…๎‚‡๎‚๎‚๎‚๎‚˜๎‚…๎‚˜๎‚‰ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€ ๎‚‡๎‚“๎‚‘๎‚”๎‚–๎‚ณ๎‚Œ๎‚‰๎‚’๎‚—๎‚๎‚“๎‚’ ๎‚ˆ๎‚™ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€
Bad example
๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚–๎‚‰๎‡‚๎‚๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚‘๎‚…๎‚๎‚‰ ๎‚๎‚˜ ๎‚‰๎‚…๎‚—๎‚๎‚‰๎‚–
๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‡€
if (( OPT_1 == opt)
|| ((c >= a) && (0xdeafbeef == b)
&& (NULL == p_point )))
{
/* processing */
}
if (0 != a_function_name_really_to_extend_to_be_as_explicit_as_possible (
A_CONSTANT_ALWAYS_WITH_A_VERY_EXPLICIT_NAME ,
A_SECOND_CONSTANT_ALWAYS_WITH_A_NAME_TO_EXTEND , 5, 50))
{
/* processing */
}
144 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
Good example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚Œ๎‚“๎‚›๎‚— ๎‚… ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚“๎‚š๎‚‰๎‚– ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚๎‚๎‚’๎‚‰๎‚—๎‡€
if (( OPT_1 == opt)
|| ((c >= a)
&& (0 xdeafbeef == b)
&& (NULL == p_point )
))
{
/* processing */
}
if (0 != a_function_name_really_to_extend_to_be_as_explicit_as_possible (
A_CONSTANT , A_CONSTANT_ALWAYS_WITH_A_VERY_EXPLICIT_NAME ,
A_SECOND_CONSTANT_ALWAYS_WITH_A_NAME_TO_EXTEND ,
5,
50))
{
/* processing */
}
18.3 Identifying and removing any dead or unreachable
code
๎€–๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚ˆ๎‚‰๎‚…๎‚ˆ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚– ๎‚™๎‚’๎‚–๎‚‰๎‚…๎‚‡๎‚Œ๎‚…๎‚†๎‚๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚Œ๎‚๎‚’๎‚ˆ๎‚‰๎‚–๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚“๎‚Š๎‚–๎‚‰๎‚…๎‚ˆ๎‚๎‚’๎‚‹ ๎‚…๎‚’๎‚ˆ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚Š
๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€
Unreachable code
๎€…๎‚“๎‚ˆ๎‚‰ ๎‚๎‚— ๎‚‡๎‚“๎‚’๎‚—๎‚๎‚ˆ๎‚‰๎‚–๎‚‰๎‚ˆ ๎‚™๎‚’๎‚–๎‚‰๎‚…๎‚‡๎‚Œ๎‚…๎‚†๎‚๎‚‰ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚๎‚— ๎‚’๎‚“ ๎‚๎‚’๎‚”๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚˜๎‚Œ๎‚๎‚— ๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‡‚
๎‚‹๎‚–๎‚…๎‚‘ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚…๎‚‡๎‚Œ๎‚‰๎‚ˆ ๎‡ฆ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚๎‚’ ๎‚…๎‚’ ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚Š๎‚…๎‚๎‚—๎‚‰ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚๎†ฝ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ
๎‚…๎Šน๎‚‰๎‚– ๎‚… return ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎†ฝ etc.๎‡ง๎‡€
Dead code
๎‡Œ๎€†๎‚‰๎‚…๎‚ˆ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡ ๎‚๎‚— ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚“๎‚“๎‚ˆ ๎‚˜๎‚“ ๎‚‘๎‚‰๎‚…๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚Š๎‚“๎‚– ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚๎‚“๎‚’ ๎‚Œ๎‚…๎‚— ๎‚’๎‚“ ๎‚‰๎Šฎ๎‚‰๎‚‡๎‚˜ ๎‡ฆ๎‚’๎‚“
๎‚‘๎‚“๎‚ˆ๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎†ฝ ๎‚’๎‚“ ๎‚๎‚‘๎‚”๎‚…๎‚‡๎‚˜ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚ ๎Šท๎‚“๎‚›๎†ฝ etc.๎‡ง๎‡€
๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚–๎‚‘๎‚“๎‚–๎‚‰๎†ฝ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚… ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚ ๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚“๎‚Š ๎‚š๎‚๎‚‰๎‚›๎†ฝ ๎‚ˆ๎‚‰๎‚…๎‚ˆ ๎‚“๎‚– ๎‚™๎‚’๎‚–๎‚‰๎‚…๎‚‡๎‚Œ๎‚…๎‚†๎‚๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚ˆ๎‚™๎‚–๎‚๎‚’๎‚‹ ๎‚… ๎‚†๎‚๎‚”๎‚…๎‚—๎‚—
๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚˜๎‚–๎‚‰๎‚…๎‚‘๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚™๎‚’๎‚–๎‚‰๎‚…๎‚‡๎‚Œ๎‚…๎‚†๎‚๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‘๎‚…๎‚ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚†๎‚™๎‚‹๎‚‹๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚ˆ๎‚๎‚—๎‚…๎‚†๎‚๎‚๎‚’๎‚‹ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚—๎‡€
RULE
183
RULE โ€” Identify and remove any dead code
RULE
184
RULE โ€” The code must have no unreachable code other than defensive
code and interface code
๎€–๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚‰๎‚š๎‚‰๎‚– ๎‚†๎‚‰ ๎‚…๎‚’๎‚ ๎‚™๎‚’๎‚–๎‚‰๎‚…๎‚‡๎‚Œ๎‚…๎‚†๎‚๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚‰๎‚œ๎‚‡๎‚‰๎‚”๎‚˜ ๎‚Š๎‚“๎‚– ๎‚ˆ๎‚‰๎‚Š๎‚‰๎‚’๎‚—๎‚๎‚š๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚– ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚Š๎‚…๎‚‡๎‚‰
๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚๎‚’ ๎‚†๎‚“๎‚˜๎‚Œ ๎‚‡๎‚…๎‚—๎‚‰๎‚— ๎‚๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚… ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 145
18.3.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎‡€๎ˆ ๎€ƒ ๎‚”๎‚–๎‚“๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ unreachable ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ‘๎‡€๎ˆ‘ ๎€–๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚’๎‚“ ๎‚ˆ๎‚‰๎‚…๎‚ˆ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ”๎ˆ•๎ˆ ๎€†๎‚‰๎‚…๎‚ˆ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ”๎ˆ•๎ˆ’ ๎€ƒ๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚˜๎‚“ ๎€˜๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚™๎‚—๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ”๎ˆ–๎ˆ ๎€‡๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎€ˆ๎‚…๎‚๎‚—๎‚‰๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ ๎€…๎€™๎€‡๎‡‚๎ˆ”๎ˆ–๎ˆ ๎€‡๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎€–๎‚–๎‚™๎‚‰๎‡€
18.4 Tool-based evaluation of the source code to limit the
risk of execution errors
๎€†๎‚‰๎‚—๎‚”๎‚๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‡๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚‹๎‚“๎‚“๎‚ˆ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹ ๎‚”๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚‡๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚‰๎‚—๎‚˜ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚๎‚“๎‚’๎†ฝ
๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚Š๎‚–๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚˜๎‚๎‚ ๎‚–๎‚‰๎‚‘๎‚…๎‚๎‚’ ๎‚๎‚’ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰๎‡€ ๎€•๎‚“๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚–๎‚‰๎‚—๎‚๎‚ˆ๎‚™๎‚…๎‚ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚ˆ๎‚๎‚—๎‚‡๎‚“๎‚š๎‚‰๎‚–๎‚‰๎‚ˆ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰
๎‚…๎‚’๎‚…๎‚๎‚๎‚—๎‚๎‚— ๎‚˜๎‚“๎‚“๎‚๎‚—๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚…๎‚’๎‚…๎‚๎‚๎‚—๎‚‰๎‚– ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚‰๎‚—๎‚—๎‚‰๎‚—๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚๎‚๎‚‘๎‚๎‚˜๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‘๎‚”๎‚…๎‚‡๎‚˜
๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚“๎‚ˆ๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚†๎‚™๎‚˜ ๎‚…๎‚๎‚—๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚‘๎‚“๎‚ˆ๎‚๎Šฑ๎‚‡๎‚…๎‡‚
๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎€™๎‚Œ๎‚‰๎‚’ ๎‚–๎‚™๎‚’๎‚’๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚‰๎‚—๎‚˜๎‚—๎†ฝ ๎‚… ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡ ๎‚…๎‚’๎‚…๎‚๎‚๎‚—๎‚๎‚— ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎‚Š๎‚ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚๎‚‰๎‚…๎‚๎‚—๎‡€ ๎€…๎‚“๎‚ˆ๎‚‰
๎‚‡๎‚“๎‚š๎‚‰๎‚–๎‚…๎‚‹๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚…๎‚๎‚—๎‚“ ๎‚†๎‚‰ ๎‚‘๎‚‰๎‚…๎‚—๎‚™๎‚–๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎‚Š๎‚ ๎‚”๎‚…๎‚–๎‚˜๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰๎‚‰๎‚’
๎‚˜๎‚‰๎‚—๎‚˜๎‚‰๎‚ˆ๎‡€
RECO
185
RECOMMENDATION โ€” Tool-based evaluation of the source code to limit
the risk of execution errors
๎€–๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚…๎‚’๎‚…๎‚๎‚๎‚—๎‚‰๎‚ˆ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚…๎‚˜ ๎‚๎‚‰๎‚…๎‚—๎‚˜ ๎‚“๎‚’๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚…๎‚’๎‚…๎‚๎‚๎‚—๎‚๎‚—
๎‚˜๎‚“๎‚“๎‚๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜๎‚— ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚’๎‚…๎‚๎‚๎‚—๎‚๎‚— ๎‚˜๎‚“๎‚“๎‚ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚—๎‚˜๎‚™๎‚ˆ๎‚๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–
๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚๎‚’ ๎‚–๎‚‰๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘๎‚— ๎‚ˆ๎‚๎‚—๎‚‡๎‚“๎‚š๎‚‰๎‚–๎‚‰๎‚ˆ๎‡€
18.4.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ๎‡€๎ˆ’ ๎€–๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚’๎‚“ ๎‚“๎‚‡๎‚‡๎‚™๎‚–๎‚–๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚“๎‚– ๎‚‡๎‚–๎‚๎‚˜๎‚๎‚‡๎‚…๎‚ ๎‚™๎‚’๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€†๎‚๎‚–๎‡€ ๎ˆ“๎‡€๎ˆ๎†ฟ ๎€”๎‚™๎‚’๎‡‚๎‚˜๎‚๎‚‘๎‚‰ ๎‚Š๎‚…๎‚๎‚๎‚™๎‚–๎‚‰๎‚— ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚†๎‚‰ ๎‚‘๎‚๎‚’๎‚๎‚‘๎‚๎‚ž๎‚‰๎‚ˆ๎‡€
18.5 Limiting cyclomatic complexity
Cyclomatic complexity
๎€…๎‚๎‚‡๎‚๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ๎‚๎‚˜๎‚ ๎‚๎‚— ๎‚… ๎‚‘๎‚‰๎‚˜๎‚–๎‚๎‚‡ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‘๎‚‰๎‚…๎‚—๎‚™๎‚–๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚…๎‚ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚… ๎‚‡๎‚“๎‚‘๎‡‚
๎‚”๎‚™๎‚˜๎‚‰๎‚– ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‡ฆ๎‚‘๎‚“๎‚ˆ๎‚™๎‚๎‚‰๎†ฝ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡ง๎‡€ ๎€‹๎‚˜ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚‰๎‚œ๎‚๎‚—๎‚˜๎‚๎‚’๎‚‹ ๎‚”๎‚…๎‚˜๎‚Œ๎‚—๎‡€
146 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎€‹๎‚˜ ๎‚๎‚— ๎‚“๎Šน๎‚‰๎‚’ ๎‚“๎‚†๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚‹๎‚–๎‚‰๎‚…๎‚˜๎‚‰๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚๎‚‡๎‚๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ๎‚๎‚˜๎‚๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚ˆ๎‚๎Šฒ๎‚‡๎‚™๎‚๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚™๎‚˜๎‚‰๎‚–
๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚๎‚— ๎‚˜๎‚“ ๎‚˜๎‚‰๎‚—๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚…๎‚๎‚’๎‚˜๎‚…๎‚๎‚’๎‡€ ๎€Š๎‚๎‚‹๎‚Œ ๎‚‡๎‚๎‚‡๎‚๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ๎‚๎‚˜๎‚ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚— ๎‚… ๎‚Œ๎‚๎‚‹๎‚Œ ๎‚”๎‚–๎‚“๎‚†๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚๎‚’๎‚˜๎‚–๎‚“๎‡‚
๎‚ˆ๎‚™๎‚‡๎‚๎‚’๎‚‹ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚ˆ๎‚™๎‚–๎‚๎‚’๎‚‹ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚– ๎‚‘๎‚…๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚…๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‡€
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚š๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚—๎‚๎‚‹๎‚’๎‚๎Šฑ๎‚‡๎‚…๎‚’๎‚˜ ๎‚‡๎‚๎‚‡๎‚๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ๎‚๎‚˜๎‚๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚“๎‚–๎‚‹๎‚…๎‚’๎‚๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚—๎‚๎‚‘๎‡‚
๎‚”๎‚๎‚๎‚Š๎‚ ๎‚๎‚˜๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚ˆ๎‚“๎‚’๎‚‰๎†ฝ ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚†๎‚ ๎‚›๎‚–๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
RECO
186
RECOMMENDATION โ€” Limitation of cyclomatic complexity
๎€–๎‚Œ๎‚‰ ๎‚‡๎‚๎‚‡๎‚๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚๎‚๎‚‘๎‚๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚Š๎‚…๎‚– ๎‚…๎‚— ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰๎‡€
18.6 Limiting the length of functions
๎€‹๎‚’ ๎‚๎‚๎‚’๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚š๎‚๎‚“๎‚™๎‚— ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚… ๎‚‡๎‚๎‚‰๎‚…๎‚– ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚…๎‚‡๎‡‚
๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€–๎‚“๎‚“ ๎‚“๎Šน๎‚‰๎‚’๎†ฝ ๎€… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚‡๎‚˜๎‚™๎‚…๎‚๎‚๎‚ ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚…๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡š๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚‰๎‚— ๎‚…๎‚˜ ๎‚“๎‚’๎‚‡๎‚‰๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚—
๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚…๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚๎‚˜๎‚— ๎‚™๎‚”๎‚ˆ๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚…๎‚’๎‚ˆ ๎‚‘๎‚…๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚…๎‚’๎‚‡๎‚‰๎‡€ ๎€ƒ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚— ๎‚˜๎‚“๎‚“ ๎‚๎‚“๎‚’๎‚‹๎†ฝ ๎‚๎‚’ ๎‚˜๎‚‰๎‚–๎‚‘๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚๎‚๎‚’๎‚‰๎‚— ๎‚“๎‚Š ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚๎‚— ๎‚“๎Šน๎‚‰๎‚’ ๎‚… ๎‚—๎‚๎‚‹๎‚’ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚— ๎‚˜๎‚“๎‚“ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ ๎‚›๎‚๎‚˜๎‚Œ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚…๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚…๎‚’๎‚ˆ
๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚‡๎‚“๎‚™๎‚๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚†๎‚‰ ๎‚—๎‚”๎‚๎‚๎‚˜ ๎‚๎‚’๎‚˜๎‚“ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚—๎‚™๎‚†๎‡‚๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€ ๎€‹๎‚’ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚‡๎‚…๎‚—๎‚‰๎‚—๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ
๎‚†๎‚‰ ๎‚–๎‚‰๎‚“๎‚–๎‚‹๎‚…๎‚’๎‚๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚—๎‚๎‚‘๎‚”๎‚๎‚๎‚Š๎‚ ๎‚๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚–๎‚‰๎‚“๎‚–๎‚‹๎‚…๎‚’๎‚๎‚—๎‚‰ ๎‚๎‚˜ ๎‚๎‚’๎‚˜๎‚“ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚—๎‚‘๎‚…๎‚๎‚๎‚‰๎‚– ๎‚—๎‚๎‚ž๎‚‰๎‚—๎†ฝ
๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚‰ ๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹๎‡€
RECO
187
RECOMMENDATION โ€” Limitation of the length and complexity of a func-
tion
๎€ƒ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚๎‚ˆ๎‚‰๎‚…๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚—๎‚๎‚’๎‚‹๎‚๎‚‰ ๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚— ๎‚…๎‚’๎‚ˆ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰
๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ ๎‚˜๎‚“ ๎‚… ๎‚–๎‚‰๎‚…๎‚—๎‚“๎‚’๎‚…๎‚†๎‚๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚๎‚๎‚’๎‚‰๎‚— ๎‚“๎‚Š ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€
18.7 Do not use C++ keywords
๎€… ๎‚…๎‚’๎‚ˆ ๎€…๎ŠŠ๎ŠŠ ๎‚…๎‚–๎‚‰ ๎‚˜๎‚›๎‚“ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹ ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰๎‚—๎†ฝ ๎‚…๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚‹๎‚Œ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚‘๎‚…๎‚’๎‚ ๎‚—๎‚๎‚‘๎‚๎‚๎‚…๎‚–๎‚๎‚˜๎‚๎‚‰๎‚—๎†ฝ ๎‚…๎‚’๎‚ˆ
๎€…๎ŠŠ๎ŠŠ ๎‚๎‚’๎‚‡๎‚“๎‚–๎‚”๎‚“๎‚–๎‚…๎‚˜๎‚‰๎‚— ๎‚‘๎‚“๎‚—๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚‰๎‚…๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰๎‡€
๎€ƒ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚‘๎‚…๎‚ ๎‚๎‚’๎‚…๎‚ˆ๎‚š๎‚‰๎‚–๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚ ๎‚™๎‚—๎‚‰ ๎€…๎ŠŠ๎ŠŠ ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ๎‚— ๎‡ฆ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฟ class๎†ฝ new๎†ฝ private๎†ฝ public๎†ฝ
delete๎†ฝ etc.๎‡ง ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚… ๎€… ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚›๎‚Œ๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚“๎‚– ๎‚—๎‚“๎‚‘๎‚‰๎‚˜๎‚Œ๎‚๎‚’๎‚‹ ๎‚‰๎‚๎‚—๎‚‰๎‡€ ๎€Š๎‚“๎‚›๎‡‚
๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚Œ๎‚๎‚’๎‚ˆ๎‚‰๎‚–๎‚— ๎‚”๎‚–๎‚“๎‚“๎‚Š๎‚–๎‚‰๎‚…๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚–๎‚๎‚—๎‚๎‚— ๎‚‡๎‚“๎‚’๎‚Š๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚’๎‚…๎‚๎‚๎‚—๎‚๎‚— ๎‚˜๎‚“๎‚“๎‚๎‚—๎‡€ ๎€๎‚“๎‚–๎‚‰๎‚“๎‚š๎‚‰๎‚–๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‡๎‚…๎‚’
๎‚Œ๎‚…๎‚‘๎‚”๎‚‰๎‚– ๎‚‘๎‚…๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚…๎‚’๎‚‡๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚…๎‚’ ๎‚‡๎‚…๎‚™๎‚—๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚–๎‚“๎‚†๎‚๎‚‰๎‚‘๎‚— ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚…๎‚๎‚—๎‚“ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚— ๎€…๎ŠŠ๎ŠŠ๎‡€ ๎€ƒ
๎‚—๎‚‰๎‚…๎‚–๎‚‡๎‚Œ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰๎‚— ๎‚“๎‚Š ๎‚… ๎€… ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚‰๎‚…๎‚—๎‚๎‚๎‚ ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚‰๎‚ˆ๎‡€ ๎€™๎‚Œ๎‚‰๎‚’ ๎‚“๎‚’๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ๎‚— ๎‚๎‚— ๎‚Š๎‚“๎‚™๎‚’๎‚ˆ๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎†ฝ ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚– ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰๎‚ˆ๎‡€
๎€ƒ๎‚”๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚œ ๎€… ๎‚”๎‚–๎‚“๎‚š๎‚๎‚ˆ๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚๎‚—๎‚˜ ๎‚“๎‚Š ๎€…๎ŠŠ๎ŠŠ ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ๎‚—๎‡€
RULE
188
RULE โ€” Do not use C++ keywords
๎€๎‚“ ๎€…๎ŠŠ๎ŠŠ ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚Š ๎‚… ๎€… ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 147
Bad example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚†๎‚‰๎‚๎‚“๎‚›๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ new ๎‚…๎‚’๎‚ˆ delete ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰๎‚ˆ ๎‚˜๎‚“
new_point ๎‚…๎‚’๎‚ˆ delete_point ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎‡€
/* point .h */
typedef struct
{
float x;
float y;
} point_t;
point_t *new ();
void delete( point_t *p);
Good example
๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚’๎‚“ ๎€…๎ŠŠ๎ŠŠ ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ๎‚— ๎‚…๎‚–๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€
/* point .h */
typedef struct
{
float x;
float y;
} point_t;
point_t *new_point ();
void delete_point(point_t *p);
148 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
19
Miscellaneous
19.1 Comment format
๎€…๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚…๎‚‡๎‚‡๎‚‰๎‚”๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚‡๎‚‡๎‚“๎‚–๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎€…๎ˆ˜๎ˆ ๎‚‡๎‚…๎‚’ ๎‚“๎‚’๎‚๎‚ ๎‚˜๎‚…๎‚๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚–๎‚‘๎†ฟ
/* comments that can be over several lines */
๎€‹๎‚’ ๎€…๎ˆ˜๎ˆ˜๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚“๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚’ ๎‚… ๎‚๎‚๎‚’๎‚‰ ๎‚๎‚— ๎‚‰๎‚œ๎‚˜๎‚‰๎‚’๎‚ˆ๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎†ฟ
// comments on a single line
๎€–๎‚Œ๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰๎‚— /* ๎‚…๎‚’๎‚ˆ // ๎‚…๎‚–๎‚‰ ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚…๎‚๎‚ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚๎‚’๎‚‰ ๎‚—๎‚”๎‚๎‚๎‚‡๎‚๎‚’๎‚‹ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–
\ ๎‚๎‚— ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚… ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚†๎‚ // ๎‚†๎‚‰๎‚‡๎‚…๎‚™๎‚—๎‚‰ ๎‚๎‚˜ ๎‚๎‚‰๎‚…๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎‡€
RULE
189
RULE โ€” Prohibited character sequences in comments
๎€–๎‚Œ๎‚‰ /* ๎‚…๎‚’๎‚ˆ // ๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚…๎‚๎‚ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎‡€ ๎€ƒ๎‚’๎‚ˆ ๎‚… ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚’ ๎‚… ๎‚๎‚๎‚’๎‚‰
๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚†๎‚ // ๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚… ๎‚๎‚๎‚’๎‚‰ ๎‚—๎‚”๎‚๎‚๎‚‡๎‚๎‚’๎‚‹ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– \๎‡€
19.1.1 References
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ’๎‡€๎ˆ ๎€–๎‚Œ๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‚— ๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰๎‚— ๎‡š๎‡ธ ๎‚…๎‚’๎‚ˆ ๎‡š๎‡š ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚… ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ ๎€”๎‚™๎‚๎‚‰ ๎ˆ’๎‡€๎ˆ‘ ๎€Ž๎‚๎‚’๎‚‰๎‡‚๎‚—๎‚”๎‚๎‚๎‚‡๎‚๎‚’๎‚‹ ๎‚—๎‚Œ๎‚…๎‚๎‚ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‡š๎‡š ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎‡€
19.2 Implementation of a โ€œcanaryโ€ mechanism
๎€ƒ๎‚๎‚–๎‚‰๎‚…๎‚ˆ๎‚ ๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚—๎‚™๎‚†๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ”๎‡€๎ˆ’๎‡€๎ˆ”๎†ฝ ๎‡Œ๎‚‡๎‚…๎‚’๎‚…๎‚–๎‚๎‚‰๎‚—๎‡ ๎‚”๎‚–๎‚“๎‚š๎‚๎‚ˆ๎‚‰ ๎‚… ๎‚”๎‚–๎‚“๎‚˜๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚‹๎‚…๎‚๎‚’๎‚—๎‚˜ ๎‚—๎‚“๎‚‘๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹
๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‡๎‚“๎‚™๎‚๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚‡๎‚‰ ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚ ๎Šท๎‚“๎‚› ๎‚Œ๎‚๎‚Ž๎‚…๎‚‡๎‚๎‚๎‚’๎‚‹ ๎‚†๎‚ ๎‚“๎‚š๎‚‰๎‚–๎‚›๎‚–๎‚๎‚˜๎‚๎‚’๎‚‹ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚—
๎‚—๎‚…๎‚š๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚‡๎‚๎‡€
๎€‹๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚“๎‚“๎‚๎‚‡๎‚Œ๎‚…๎‚๎‚’ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚—๎‚™๎‚”๎‚”๎‚“๎‚–๎‚˜ ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚๎‚’๎‚—๎‚‰๎‚–๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‡๎‚…๎‚’๎‚…๎‚–๎‚๎‚‰๎‚—๎†ฝ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚… ๎‚‘๎‚‰๎‚‡๎‚Œ๎‚…๎‚’๎‚๎‚—๎‚‘ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰
๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚– ๎‚Œ๎‚๎‚‘๎‚—๎‚‰๎‚๎‚Š๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚…๎‚‡๎‚Œ๎‚๎‚‰๎‚š๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚”๎‚…๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚…๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜
๎‚˜๎‚“ ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚‡๎‚–๎‚๎‚˜๎‚๎‚‡๎‚…๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚š๎‚‰๎‚–๎‚๎‚Š๎‚๎‚๎‚’๎‚‹ ๎‚๎‚˜๎‚— ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚‰๎‚‹๎‚๎‚’๎‚’๎‚๎‚’๎‚‹ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚…๎‚—
๎‚๎‚๎‚๎‚™๎‚—๎‚˜๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚Œ๎‚‰๎‚–๎‚‰๎‚๎‚’๎‚†๎‚‰๎‚๎‚“๎‚›๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 149
RULE
190
RULE โ€” Manually implement a โ€œcanaryโ€ mechanism when not already sup-
ported by the toolchain
๎€–๎‚Œ๎‚๎‚— ๎‚‘๎‚‰๎‚‡๎‚Œ๎‚…๎‚’๎‚๎‚—๎‚‘ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚…๎‚˜ ๎‚๎‚‰๎‚…๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚‡๎‚–๎‚๎‚˜๎‚๎‚‡๎‚…๎‚ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎€™๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚๎‚— ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚Š๎‚‰๎‚…๎‚—๎‚๎‚†๎‚๎‚‰๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚—๎‚˜๎‚๎‚๎‚ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚˜๎‚…๎‚๎‚‰ ๎‚… ๎‚˜๎‚Œ๎‚“๎‚–๎‚“๎‚™๎‚‹๎‚Œ ๎‚…๎‚’๎‚…๎‚๎‚๎‚—๎‚๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚’
๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“๎†ฝ ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚‹๎‚™๎‚…๎‚–๎‚…๎‚’๎‚˜๎‚‰๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚’๎‚“ ๎‚๎‚“๎‚‡๎‚…๎‚ ๎‚…๎‚–๎‚–๎‚…๎‚๎‚— ๎‚…๎‚–๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎†ฝ ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚…๎‚’๎‚ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚ ๎Šท๎‚“๎‚› ๎‚Œ๎‚๎‚Ž๎‚…๎‚‡๎‚๎‚๎‚’๎‚‹
๎‚ˆ๎‚™๎‚‰ ๎‚˜๎‚“ ๎‚… ๎‚—๎‚˜๎‚…๎‚‡๎‚ ๎‚†๎‚™๎Šฎ๎‚‰๎‚– ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‡€
Warning
๎€’๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚‹๎‚๎‚š๎‚‰๎‚’ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‡Œ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‡ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚…๎‚’๎‚…๎‚–๎‚๎‚‰๎‚— ๎‚†๎‚ ๎‚‘๎‚‰๎‚…๎‚’๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎‚˜๎‚“๎‚“๎‚๎‚‡๎‚Œ๎‚…๎‚๎‚’๎‡€ ๎€‹๎‚’๎‚ˆ๎‚‰๎‚‰๎‚ˆ๎†ฝ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚๎‚’๎‚‹ ๎‚… ๎‚‡๎‚…๎‚’๎‚…๎‚–๎‚ ๎‚‘๎‚‰๎‚‡๎‚Œ๎‚…๎‚’๎‚๎‚—๎‚‘ ๎‚–๎‚‰๎‚‘๎‚…๎‚๎‚’๎‚— ๎‚… ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚…๎‚—๎‚๎†ฝ ๎‚“๎‚Š๎‡‚
๎‚˜๎‚‰๎‚’ ๎‚”๎‚–๎‚“๎‚’๎‚‰ ๎‚˜๎‚“ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚“๎‚– ๎‚‰๎‚š๎‚‰๎‚’ ๎‚š๎‚™๎‚๎‚’๎‚‰๎‚–๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚๎‚‰๎‚—๎‡€
Good example
๎€–๎‚Œ๎‚‰ ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ volatile ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚”๎‚–๎‚‰๎‚š๎‚‰๎‚’๎‚˜ ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚Š๎‚“๎‚–
๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ canary ๎‚…๎‚’๎‚ˆ canaryRef ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎‡€ ๎€‹๎‚’ ๎‚Š๎‚…๎‚‡๎‚˜๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚˜๎‚“
๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚‹๎‚“ ๎‚…๎‚’๎‚ˆ ๎‚–๎‚‰๎‚…๎‚ˆ ๎‚˜๎‚Œ๎‚‰ canary ๎‚…๎‚’๎‚ˆ canaryRef ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚๎‚’ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚๎‡€
typedef volatile uint32_t fid_t ;
#ifdef ACTIVATE_CANARIES
static inline void verifcanari (fid_t canari , fid_t canariRef) {
uint8_t res = !!( canari != canariRef);
if (0 != res)
{
/* context -specific processing */
}
}
#else /* ifdef ACTIVATE_CANARIES */
static inline void verifcanari (fid_t canari , fid_t canariRef) { }
#endif /* ifdef ACTIVATE_CANARIES */
void foo( fid_t canari) {
/* checking of the canary parameter at the beginning of the function */
verifcanari (canari , FID_FOO);
/* body of the function ... */
/* checking of the canary parameter at the end of the function */
verifcanari (canari , FID_FOO);
}
19.3 Assertions of development and assertions of integrity
๎€–๎‚›๎‚“ ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚“๎‚Š ๎‚…๎‚—๎‚—๎‚‰๎‚–๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚ˆ๎‚๎‚—๎‚˜๎‚๎‚’๎‚‹๎‚™๎‚๎‚—๎‚Œ๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰๎†ฟ
n ๎‚…๎‚—๎‚—๎‚‰๎‚–๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚™๎‚–๎‚”๎‚“๎‚—๎‚‰ ๎‚“๎‚Š ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜๎‡€ ๎€–๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚…๎‚–๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚ˆ๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚‘๎‚“๎‚š๎‚‰๎‚ˆ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰
๎‚“๎‚’๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚Œ๎‚…๎‚—๎‚‰ ๎‚๎‚— ๎‚“๎‚š๎‚‰๎‚– ๎‡ฆ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎†ฝ ๎‚˜๎‚“ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚’๎‚™๎‚๎‚๎‡ง๎†พ
n
๎‚…๎‚—๎‚—๎‚‰๎‚–๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚ˆ๎‚‰๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚–๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚™๎‚–๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚๎‚“๎‚’๎†ฟ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚…๎‚–๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚’๎‚ˆ๎‚‰๎‚ˆ
๎‚˜๎‚“ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚–๎‚™๎‚’๎‚— ๎‚’๎‚“๎‚–๎‚‘๎‚…๎‚๎‚๎‚ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜ ๎‚… ๎‚Œ๎‚…๎‚–๎‚ˆ๎‚›๎‚…๎‚–๎‚‰ ๎‚Š๎‚…๎‚๎‚๎‚™๎‚–๎‚‰ ๎‚“๎‚– ๎‚…๎‚’ ๎‚…๎‚˜๎‚˜๎‚‰๎‚‘๎‚”๎‚˜ ๎‚˜๎‚“
๎‚‘๎‚“๎‚ˆ๎‚๎‚Š๎‚ ๎‚๎‚˜ ๎‚‰๎‚œ๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚๎‚๎‚ ๎‡ฆ๎‚‰๎‡€๎‚‹๎‡€ ๎‚… ๎‚Š๎‚…๎‚™๎‚๎‚˜ ๎‚…๎‚˜๎‚˜๎‚…๎‚‡๎‚๎‡ง๎‡€
๎€ƒ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚–๎‚๎‚˜๎‚ ๎‚…๎‚—๎‚—๎‚‰๎‚–๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚›๎‚–๎‚๎‚˜๎‚˜๎‚‰๎‚’ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ assert()๎‡€ ๎€‹๎‚’๎‚ˆ๎‚‰๎‚‰๎‚ˆ๎†ฝ ๎‚˜๎‚Œ๎‚๎‚— ๎‚‘๎‚…๎‚‡๎‚–๎‚“
๎‚๎‚— ๎‚ˆ๎‚‰๎‚๎‚‰๎‚˜๎‚‰๎‚ˆ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚’ ๎‚–๎‚‰๎‚๎‚‰๎‚…๎‚—๎‚‰ ๎‚‘๎‚“๎‚ˆ๎‚‰๎‡€ ๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚–๎‚‘๎‚“๎‚–๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚…๎‚—๎‚—๎‚‰๎‚–๎‚˜๎‚๎‚“๎‚’๎‚—
150 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚“๎‚’๎‚๎‚ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚ˆ๎‚‰๎‚†๎‚™๎‚‹๎‚‹๎‚๎‚’๎‚‹ ๎‚”๎‚™๎‚–๎‚”๎‚“๎‚—๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚…๎‚–๎‚‰ ๎‚๎‚’ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚– ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚š๎‚‰๎‚–๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’
๎‚”๎‚™๎‚–๎‚”๎‚“๎‚—๎‚‰๎‚—๎†ฝ ๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚…๎‚๎‚๎‚ ๎‚ˆ๎‚™๎‚‰ ๎‚˜๎‚“ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚‡๎‚˜๎‚๎‚š๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚ˆ๎‚‰๎‚†๎‚™๎‚‹ ๎‚‘๎‚“๎‚ˆ๎‚‰ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚›๎‚๎‚๎‚ ๎‚’๎‚“ ๎‚๎‚“๎‚’๎‚‹๎‚‰๎‚– ๎‚†๎‚‰ ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜
๎‚“๎‚™๎‚˜๎‚—๎‚๎‚ˆ๎‚‰ ๎‚“๎‚Š ๎‚ˆ๎‚‰๎‚†๎‚™๎‚‹ ๎‚‘๎‚“๎‚ˆ๎‚‰๎‡€
๎€‹๎‚˜ ๎‚‘๎‚…๎‚ ๎‚†๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚… ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚–๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚… ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚๎‚— ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚™๎‚’๎‚–๎‚‰๎‚…๎‚‡๎‚Œ๎‚…๎‚†๎‚๎‚‰
๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚“๎‚– ๎‚… ๎‚—๎‚˜๎‚…๎‚˜๎‚๎‚‡ ๎‚…๎‚’๎‚…๎‚๎‚๎‚—๎‚๎‚— ๎‚˜๎‚“๎‚“๎‚ ๎‡ฆ๎‚๎‚’ ๎‚Š๎‚…๎‚‡๎‚˜๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‡๎‚…๎‚’ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚ ๎‚… ๎‚—๎‚‰๎‚˜ ๎‚“๎‚Š ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‡๎‚…๎‚’๎‚’๎‚“๎‚˜
๎‚“๎‚‡๎‚‡๎‚™๎‚– ๎‚ˆ๎‚™๎‚–๎‚๎‚’๎‚‹ ๎‚’๎‚“๎‚–๎‚‘๎‚…๎‚ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚๎‚“๎‚’๎‡ง๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚”๎‚™๎‚–๎‚”๎‚“๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚๎‚‰๎‚…๎‚–๎‚๎‚ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ๎†ฝ
๎‚…๎‚’๎‚ˆ ๎‚๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‰๎‚’๎‚—๎‚™๎‚–๎‚‰๎‚ˆ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚๎‚‰๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚๎‚— ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰
๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚๎‚’๎‚…๎‚–๎‚๎‡€
RULE
191
RULE โ€” No development assertion on a code in production
๎€†๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚…๎‚—๎‚—๎‚‰๎‚–๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜ ๎‚๎‚’ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€
RECO
192
RECOMMENDATION โ€” Management of integrity assertions should include
emergency data deletion
๎€‹๎‚’๎‚˜๎‚‰๎‚‹๎‚–๎‚๎‚˜๎‚ ๎‚…๎‚—๎‚—๎‚‰๎‚–๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚…๎‚”๎‚”๎‚‰๎‚…๎‚– ๎‚๎‚’ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€‹๎‚Š ๎‚…๎‚’ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚–๎‚๎‚˜๎‚ ๎‚…๎‚—๎‚—๎‚‰๎‚–๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚˜๎‚–๎‚๎‚‹๎‚‹๎‚‰๎‚–๎‚‰๎‚ˆ๎†ฝ
๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚‘๎‚‰๎‚–๎‚‹๎‚‰๎‚’๎‚‡๎‚ ๎‚ˆ๎‚‰๎‚๎‚‰๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚…๎‡€
19.4 Last line of a non-empty ๎‚พle must end with a line break
๎€–๎‚Œ๎‚‰ ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚๎‚๎‚’๎‚‰ ๎‚†๎‚–๎‚‰๎‚…๎‚ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚… ๎‚’๎‚“๎‚’๎‡‚๎‚‰๎‚‘๎‚”๎‚˜๎‚ ๎Šฑ๎‚๎‚‰ ๎‚๎‚‰๎‚…๎‚ˆ๎‚— ๎‚˜๎‚“ ๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚– ๎‚…๎‚‡๎‚‡๎‚“๎‚–๎‚ˆ๎‚๎‚’๎‚‹
๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎€…๎ˆ˜๎ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎ˆ˜๎ˆ˜ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‚—๎‡€
Warning
๎€–๎‚Œ๎‚‰ ๎‚š๎‚…๎‚—๎‚˜ ๎‚‘๎‚…๎‚Ž๎‚“๎‚–๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚”๎‚™๎‚†๎‚๎‚๎‚—๎‚Œ๎‚‰๎‚–๎‚—๎†ฝ ๎‚”๎‚…๎‚–๎‚˜๎‚๎‚‡๎‚™๎‚๎‚…๎‚–๎‚๎‚ ๎‚๎‚’ ๎‚… ๎€Ž๎‚๎‚’๎‚™๎‚œ ๎‚‰๎‚’๎‚š๎‚๎‚–๎‚“๎‚’๎‚‘๎‚‰๎‚’๎‚˜๎†ฝ ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚
๎‚…๎‚’๎‚ˆ ๎‚๎‚’๎‚š๎‚๎‚—๎‚๎‚†๎‚๎‚ ๎‚…๎‚ˆ๎‚ˆ ๎‚˜๎‚Œ๎‚๎‚— ๎‚๎‚๎‚’๎‚‰ ๎‚†๎‚–๎‚‰๎‚…๎‚ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚‡๎‚๎‚“๎‚—๎‚๎‚’๎‚‹ ๎Šฑ๎‚๎‚‰๎‚—๎‡€
๎€‹๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚…๎‚๎‚ ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚๎‚“๎‚—๎‚‰๎‚ˆ๎‡€
RULE
193
RULE โ€” All non-empty ๎‚พles mustend with a line break and thepreprocessor
directives and comments must be closed
๎€ƒ ๎‚’๎‚“๎‚’๎‡‚๎‚‰๎‚‘๎‚”๎‚˜๎‚ ๎Šฑ๎‚๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚“๎‚˜ ๎‚‰๎‚’๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚๎‚ˆ๎‚ˆ๎‚๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚– ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 151
Appendix A
Acronyms
๎€ƒ๎€๎€•๎€‹ ๎€ƒ๎‚‘๎‚‰๎‚–๎‚๎‚‡๎‚…๎‚’ ๎€๎‚…๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎€•๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‚— ๎€‹๎‚’๎‚—๎‚˜๎‚๎‚˜๎‚™๎‚˜๎‚‰
๎€ƒ๎€’๎€‹ ๎€ƒ๎‚”๎‚”๎‚๎‚๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎€’๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹ ๎€‹๎‚’๎‚˜๎‚‰๎‚–๎‚Š๎‚…๎‚‡๎‚‰
๎€ƒ๎€•๎€Ž๎€” ๎€ƒ๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚๎‚…๎‚๎‚“๎‚™๎‚˜ ๎‚–๎‚…๎‚’๎‚ˆ๎‚“๎‚‘๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’
๎€ˆ๎€ƒ๎€ ๎€ˆ๎‚๎‚‰๎‚œ๎‚๎‚†๎‚๎‚‰ ๎€ƒ๎‚–๎‚–๎‚…๎‚ ๎€๎‚‰๎‚‘๎‚†๎‚‰๎‚–
๎€‹๎€†๎€‡ ๎€‹๎‚’๎‚˜๎‚‰๎‚‹๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎€†๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎€‡๎‚’๎‚š๎‚๎‚–๎‚“๎‚’๎‚‘๎‚‰๎‚’๎‚˜
๎€‹๎€•๎€‘ ๎€‹๎‚’๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎€•๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‚— ๎€‘๎‚–๎‚‹๎‚…๎‚’๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’
๎€๎€‹๎€•๎€”๎€ƒ ๎€๎‚“๎‚˜๎‚“๎‚– ๎€‹๎‚’๎‚ˆ๎‚™๎‚—๎‚˜๎‚–๎‚ ๎€•๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎€”๎‚‰๎‚๎‚๎‚…๎‚†๎‚๎‚๎‚๎‚˜๎‚ ๎€ƒ๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚๎‚“๎‚’
๎€˜๎€Ž๎€ƒ ๎€˜๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎€Ž๎‚‰๎‚’๎‚‹๎‚˜๎‚Œ ๎€ƒ๎‚–๎‚–๎‚…๎‚
152 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
Appendix B
Further information on gcc and Clang
options
๎€‹๎‚’๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‹๎‚๎‚š๎‚‰๎‚’ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚๎‚— ๎‚…๎‚”๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚œ ๎‚“๎‚–๎‚๎‚‹๎‚๎‚’๎‚…๎‚˜๎‚‰๎‚— ๎‚Š๎‚–๎‚“๎‚‘ ๎‡ฌ๎€‰๎‚‡๎‚‡๎€”๎‚‰๎‚Š๎‡ญ ๎‚…๎‚’๎‚ˆ ๎‡ฌ๎€…๎‚๎‚…๎‚’๎‚‹๎€”๎‚‰๎‚Š๎‡ญ๎†ฝ ๎‚Š๎‚“๎‚– ๎„Œ๎„ˆ๎„ˆ ๎ˆ๎ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ
๎ˆ๎ˆ’ ๎‚–๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚๎‚๎‡€
B.1 De๎‚พnition of the C language standard in use
๎€‘๎‚”๎‚˜๎‚๎‚“๎‚’ -std ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚Š๎‚“๎‚– ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚Š๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‡ก ๎‚“๎‚– ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎€‰๎€๎€—
๎‚ˆ๎‚๎‚…๎‚๎‚‰๎‚‡๎‚˜ ๎‡ก ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‡€ ๎€™๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚˜๎‚Œ๎‚๎‚— ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎€‰๎€๎€— ๎‚ˆ๎‚๎‚…๎‚๎‚‰๎‚‡๎‚˜ ๎‚“๎‚Š ๎€‹๎€•๎€‘ ๎€…๎ˆ๎ˆ– ๎‚๎‚— ๎‚—๎‚‰๎‚๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜๎‡€
Information
๎€‘๎‚”๎‚˜๎‚๎‚“๎‚’ -ansi ๎‚๎‚— ๎‚‰๎‚•๎‚™๎‚๎‚š๎‚…๎‚๎‚‰๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ -std=c90๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚๎‚— ๎‚๎‚˜๎‚—๎‚‰๎‚๎‚Š ๎‚‰๎‚•๎‚™๎‚๎‚š๎‚…๎‚๎‚‰๎‚’๎‚˜ ๎‚˜๎‚“ -std=c89
๎‚…๎‚’๎‚ˆ -std=iso9899:1990๎‡€
Information
๎€‘๎‚”๎‚˜๎‚๎‚“๎‚’ -std=iso9899:199409 ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚— ๎‚˜๎‚“ ๎€‹๎€•๎€‘ ๎€…๎ˆ˜๎ˆ ๎‚…๎‚— ๎‚‘๎‚“๎‚ˆ๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚…๎‚‘๎‚‰๎‚’๎‚ˆ๎‚‘๎‚‰๎‚’๎‚˜ ๎ˆ
๎‚๎‚’ ๎ˆ๎ˆ˜๎ˆ˜๎ˆ”๎‡€
Information
๎€‘๎‚”๎‚˜๎‚๎‚“๎‚’ -std=iso9899:1999 ๎‚๎‚— ๎‚‰๎‚•๎‚™๎‚๎‚š๎‚…๎‚๎‚‰๎‚’๎‚˜ ๎‚˜๎‚“ -std=c99๎‡€
B.2 Additional warnings
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚’๎‚‰๎‚๎‚˜๎‚Œ๎‚‰๎‚– ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚ˆ ๎‚๎‚’ -Wall ๎‚’๎‚“๎‚– -Wextra ๎‚’๎‚“๎‚– -Wpedantic ๎‚…๎‚’๎‚ˆ ๎‚›๎‚‰๎‚–๎‚‰ ๎‚’๎‚“๎‚˜
๎‚‘๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚‡๎‚Œ๎‚…๎‚”๎‚˜๎‚‰๎‚– ๎ˆ”๎†ฝ ๎‚†๎‚™๎‚˜ ๎‚‘๎‚…๎‚ ๎‚’๎‚“๎‚’๎‚‰๎‚˜๎‚Œ๎‚‰๎‚๎‚‰๎‚—๎‚— ๎‚”๎‚–๎‚“๎‚š๎‚‰ ๎‚™๎‚—๎‚‰๎‚Š๎‚™๎‚
๎ˆ๎ˆ”
๎†ฟ
n
-Wbad-function-cast
n -Wcast-align
n -Wcast-qual ๎‡ฆ๎‚›๎‚…๎‚–๎‚’๎‚— ๎‚›๎‚Œ๎‚‰๎‚’๎‚‰๎‚š๎‚‰๎‚– ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚๎‚— ๎‚‡๎‚…๎‚—๎‚˜ ๎‚—๎‚“ ๎‚…๎‚— ๎‚˜๎‚“ ๎‚–๎‚‰๎‚‘๎‚“๎‚š๎‚‰ ๎‡ก ๎‚“๎‚– ๎‚๎‚’๎‚˜๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰ ๎‚๎‚’ ๎‚…๎‚’ ๎‚™๎‚’๎‚—๎‚…๎‚Š๎‚‰
๎‚›๎‚…๎‚ ๎‡ก ๎‚… ๎‚˜๎‚๎‚”๎‚‰ ๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚– ๎‚๎‚๎‚๎‚‰ const๎‡ง
๎ˆ๎ˆ”๎‡€ ๎€‘๎‚’๎‚๎‚ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚›๎‚Œ๎‚“๎‚—๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚‰๎‚‘๎‚‰๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚‘๎‚‰๎‚…๎‚’๎‚๎‚’๎‚‹๎‚Š๎‚™๎‚ ๎‚‰๎‚’๎‚“๎‚™๎‚‹๎‚Œ ๎‚…๎‚–๎‚‰ ๎‚‰๎‚œ๎‚”๎‚๎‚…๎‚๎‚’๎‚‰๎‚ˆ๎‡€ ๎€‹๎‚’ ๎‚…๎‚’๎‚ ๎‚‡๎‚…๎‚—๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎‚๎‚— ๎‚‰๎‚’๎‚‡๎‚“๎‚™๎‚–๎‚…๎‚‹๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚–๎‚‰๎‚Š๎‚‰๎‚– ๎‚˜๎‚“
๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚’๎‚ˆ ๎€…๎„‘๎„†๎„“๎„Œ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‚‘๎‚…๎‚’๎‚™๎‚…๎‚๎‚— ๎‚Š๎‚“๎‚– ๎‚‘๎‚“๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚˜๎‚…๎‚๎‚๎‚‰๎‚ˆ ๎‚‰๎‚œ๎‚”๎‚๎‚…๎‚’๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 153
n
-Wconversion ๎‡ฆ๎‚›๎‚…๎‚–๎‚’๎‚— ๎‚Š๎‚“๎‚– ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‘๎‚…๎‚ ๎‚…๎‚๎‚˜๎‚‰๎‚– ๎‚… ๎‚š๎‚…๎‚๎‚™๎‚‰๎†ฝ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚—
๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚™๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ๎‡ง
n
-Wfloat-equal
n -Wnull-dereference
n
-Wshadow ๎‡ฆ๎‚›๎‚…๎‚–๎‚’๎‚— ๎‚›๎‚Œ๎‚‰๎‚’๎‚‰๎‚š๎‚‰๎‚– ๎‚… ๎‚๎‚“๎‚‡๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚“๎‚– ๎‚˜๎‚๎‚”๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚–๎‚‰๎‚™๎‚—๎‚‰๎‚— ๎‚…๎‚’ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚— ๎‚…๎‚๎‚–๎‚‰๎‚…๎‚ˆ๎‚
๎‚†๎‚“๎‚™๎‚’๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎†ฝ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚“๎‚– ๎‚˜๎‚๎‚”๎‚‰๎‡ง
n
-Wstack-protector ๎‡ฆ๎‚›๎‚…๎‚–๎‚’๎‚— ๎‚…๎‚†๎‚“๎‚™๎‚˜ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚’๎‚“๎‚˜ ๎‚๎‚’๎‚—๎‚˜๎‚–๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚—๎‚˜๎‚…๎‚‡๎‚ ๎‚‡๎‚…๎‚’๎‚…๎‚–๎‚๎‡ง
n
-Wstrict-prototypes
n
-Wswitch-enum ๎‡ฆ๎‚›๎‚…๎‚–๎‚’๎‚— ๎‚›๎‚Œ๎‚‰๎‚’๎‚‰๎‚š๎‚‰๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚๎‚๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… switch ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚— ๎‚“๎‚Š ๎‚…๎‚’ ๎‚‰๎‚’๎‚™๎‡‚
๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚๎‚”๎‚‰ ๎‚†๎‚™๎‚˜ ๎‚๎‚…๎‚‡๎‚๎‚— ๎‚… case ๎‚Š๎‚“๎‚– ๎‚“๎‚’๎‚‰ ๎‚“๎‚– ๎‚‘๎‚“๎‚–๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰๎‚ˆ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚๎‚— ๎‚˜๎‚๎‚”๎‚‰๎‡ง
n
-Wmissing-prototypes
n
-Wundef
n
-Wvla
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‡ ๎‚˜๎‚“ ๎„Œ๎„ˆ๎„ˆ๎†ฟ
n
-Wduplicated-branches
n
-Wduplicated-cond
n
-Wformat-signedness
n
-Wjump-misses-init
n
-Wlogical-op ๎‡ฆ๎‚›๎‚…๎‚–๎‚’๎‚— ๎‚…๎‚†๎‚“๎‚™๎‚˜ ๎‚—๎‚™๎‚—๎‚”๎‚๎‚‡๎‚๎‚“๎‚™๎‚— ๎‚™๎‚—๎‚‰๎‚— ๎‚“๎‚Š ๎‚๎‚“๎‚‹๎‚๎‚‡๎‚…๎‚ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚—๎‡ง
n
-Wnested-externs ๎‡ฆ๎‚›๎‚…๎‚–๎‚’๎‚— ๎‚“๎‚’ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ extern ๎‚—๎‚˜๎‚“๎‚–๎‚…๎‚‹๎‚‰ ๎‚‡๎‚๎‚…๎‚—๎‚— ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚– ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚…
๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡ง
n
-Wnormalized ๎‡ฆ๎‚›๎‚…๎‚–๎‚’๎‚— ๎‚…๎‚†๎‚“๎‚™๎‚˜ ๎‚…๎‚’๎‚ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚๎‚’ ๎‚’๎‚“๎‚–๎‚‘๎‚…๎‚๎‚๎‚ž๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚–๎‚‘๎‡ง
n -Wold-style-definition
n
-Wshift-negative-value
n
-Wshift-overflow=2
n
-Wstrict-overflow=3 ๎‡ฆ๎‚›๎‚…๎‚–๎‚’๎‚— ๎‚…๎‚†๎‚“๎‚™๎‚˜ ๎‚… ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚‡๎‚…๎‚—๎‚‰๎‚— ๎‚›๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚ž๎‚‰๎‚— ๎‚†๎‚…๎‚—๎‚‰๎‚ˆ ๎‚“๎‚’
๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚™๎‚‘๎‚”๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚› ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚“๎‚‡๎‚‡๎‚™๎‚–๎‡ง
n
-Wsuggest-attribute=format ๎‡ฆ๎‚›๎‚…๎‚–๎‚’๎‚— ๎‚Š๎‚“๎‚– ๎‚‡๎‚…๎‚—๎‚‰๎‚— ๎‚›๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚’๎‚‹ ๎‚… format ๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚˜๎‚˜๎‚–๎‚๎‚†๎‚™๎‚˜๎‚‰ ๎‚‘๎‚…๎‚ ๎‚†๎‚‰
๎‚†๎‚‰๎‚’๎‚‰๎Šฑ๎‚‡๎‚๎‚…๎‚๎‡ง
n
-Wsuggest-attribute=malloc ๎‡ฆ๎‚›๎‚…๎‚–๎‚’๎‚— ๎‚Š๎‚“๎‚– ๎‚‡๎‚…๎‚—๎‚‰๎‚— ๎‚›๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚’๎‚‹ ๎‚… malloc ๎„Œ๎„ˆ๎„ˆ ๎‚…๎‚˜๎‚˜๎‚–๎‚๎‚†๎‚™๎‚˜๎‚‰ ๎‚‘๎‚…๎‚ ๎‚†๎‚‰
๎‚†๎‚‰๎‚’๎‚‰๎Šฑ๎‚‡๎‚๎‚…๎‚๎‡ง
n -Wswitch-default ๎‡ฆ๎‚›๎‚…๎‚–๎‚’๎‚— ๎‚›๎‚Œ๎‚‰๎‚’๎‚‰๎‚š๎‚‰๎‚– ๎‚… switch ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚… ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜ ๎‚‡๎‚…๎‚—๎‚‰๎‡ง
n
-Wtraditional-conversion ๎‡ฆ๎‚›๎‚…๎‚–๎‚’๎‚— ๎‚๎‚Š ๎‚… ๎‚”๎‚–๎‚“๎‚˜๎‚“๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚…๎‚™๎‚—๎‚‰๎‚— ๎‚… ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚— ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚Š๎‚–๎‚“๎‚‘
๎‚›๎‚Œ๎‚…๎‚˜ ๎‚›๎‚“๎‚™๎‚๎‚ˆ ๎‚Œ๎‚…๎‚”๎‚”๎‚‰๎‚’ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚”๎‚–๎‚“๎‚˜๎‚“๎‚˜๎‚๎‚”๎‚‰๎‡ง
154 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
n
-Wtrampolines ๎‡ฆ๎‚›๎‚…๎‚–๎‚’๎‚— ๎‚๎‚Š ๎‚˜๎‚–๎‚…๎‚‘๎‚”๎‚“๎‚๎‚๎‚’๎‚‰๎‚—๎†ฝ ๎‚‘๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚… ๎‚Š๎‚“๎‚“๎‚˜๎‚’๎‚“๎‚˜๎‚‰ ๎‚๎‚’ ๎‚—๎‚™๎‚†๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ”๎‡€๎ˆ’๎‡€๎ˆ”๎†ฝ ๎‚…๎‚–๎‚‰ ๎‚‹๎‚‰๎‚’๎‚‰๎‚–๎‡‚
๎‚…๎‚˜๎‚‰๎‚ˆ๎‡ง
n
-Wwrite-strings ๎‡ฆ๎‚…๎‚ˆ๎‚ˆ๎‚— ๎‚˜๎‚๎‚”๎‚‰ ๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚– const ๎‚˜๎‚“ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹๎‚— ๎‚—๎‚“ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‡๎‚“๎‚”๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚“๎‚Š
๎‚“๎‚’๎‚‰ ๎‚๎‚’๎‚˜๎‚“ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚“ ๎‚… ๎‚’๎‚“๎‚’๎‡‚const๎‡‚๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚˜๎‚๎‚”๎‚‰ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚— ๎‚… ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎†พ ๎‚˜๎‚Œ๎‚๎‚— ๎‚Œ๎‚‰๎‚๎‚”๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–
๎‚˜๎‚“ ๎Šฑ๎‚’๎‚ˆ ๎‚…๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‡‚๎‚˜๎‚๎‚‘๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚–๎‚๎‚‰๎‚— ๎‚˜๎‚“ ๎‚›๎‚–๎‚๎‚˜๎‚‰ ๎‚๎‚’๎‚˜๎‚“ ๎‚… ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‡ก ๎‚”๎‚–๎‚“๎‚š๎‚๎‚ˆ๎‚‰๎‚ˆ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ const
๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ ๎‚Œ๎‚…๎‚— ๎‚๎‚’๎‚ˆ๎‚‰๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚‰๎‚’ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ ๎‚”๎‚–๎‚“๎‚˜๎‚“๎‚˜๎‚๎‚”๎‚‰๎‚—๎†ฝ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚–๎‚›๎‚๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚๎‚— ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹ ๎‚†๎‚‰๎‚‡๎‚“๎‚‘๎‚‰๎‚—
๎‚™๎‚’๎‚Œ๎‚‰๎‚๎‚”๎‚Š๎‚™๎‚๎‚๎‚ ๎‚š๎‚‰๎‚–๎‚ ๎‚’๎‚“๎‚๎‚—๎‚๎‡ง
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‡ ๎‚˜๎‚“ ๎€…๎„‘๎„†๎„“๎„Œ๎†ฟ
n
-Warray-bounds-pointer-arithmetic
n
-Wassign-enum ๎‡ฆ๎‚›๎‚…๎‚–๎‚’๎‚— ๎‚›๎‚Œ๎‚‰๎‚’๎‚‰๎‚š๎‚‰๎‚– ๎‚…๎‚’ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚‰๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ
๎‚˜๎‚๎‚”๎‚‰ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰๎‚๎‚“๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚…๎‚’๎‚‹๎‚‰ ๎‚“๎‚Š ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚๎‚— ๎‚˜๎‚๎‚”๎‚‰๎‡ง
n
-Wcast-function-type
n
-Wcomma
n -Wcovered-switch-default
n -Wduplicate-enum
n
-Widiomatic-parentheses ๎‡ฆ๎‚›๎‚…๎‚–๎‚’๎‚— ๎‚›๎‚Œ๎‚‰๎‚’๎‚‰๎‚š๎‚‰๎‚– ๎‚…๎‚’ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚… ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’
๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚†๎‚‰๎‚๎‚’๎‚‹ ๎‚‰๎‚’๎‚‡๎‚๎‚“๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚—๎‡ง
n
-Wloop-analysis
n
-Wformat-non-iso
n
-Wformat-pedantic
n
-Wformat-type-confusion
n
-Wfour-char-constants
n -Wimplicit-fallthrough
n -Wpointer-arith
n
-Wpragmas
n
-Wreserved-identifier
n
-Wshift-sign-overflow
n
-Wsigned-enum-bitfield
n
-Wstatic-in-inline
n
-Wtautological-constant-in-range-compare
n
-Wthread-safety
n -Wunreachable-code
n -Wunreachable-code-aggressive
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 155
n
-Wunused-macros
n
-Wused-but-marked-unused
n
-Wvariadic-macros
n
-Wzero-as-null-pointer-constant
Information
๎€™๎‚๎‚˜๎‚Œ ๎€…๎„‘๎„†๎„“๎„Œ๎†ฝ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ -Wall ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ -Wmost๎†ฝ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚๎‚˜๎‚—๎‚‰๎‚๎‚Š ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰๎‚—
๎‚‘๎‚…๎‚’๎‚ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚—๎‡€ ๎€–๎‚Œ๎‚‰๎‚–๎‚‰๎‚Š๎‚“๎‚–๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚…๎‚˜๎‚˜๎‚‰๎‚– ๎‚…๎‚–๎‚‰
๎‚’๎‚“๎‚˜ ๎‚๎‚๎‚—๎‚˜๎‚‰๎‚ˆ ๎‚…๎‚†๎‚“๎‚š๎‚‰๎‡€
B.3 Clang and the -Weverything option
๎€…๎„‘๎„†๎„“๎„Œ ๎‚Š๎‚‰๎‚…๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚… -Weverything ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’
๎ˆ๎ˆ•
๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚‰๎‚— all ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚—๎‚™๎‚”๎‚”๎‚“๎‚–๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎€…๎„‘๎„†๎„“๎„Œ
๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚‰๎‚œ๎‚‡๎‚‰๎‚”๎‚˜๎‚๎‚“๎‚’๎‡€
๎€—๎‚—๎‚๎‚’๎‚‹ -Weverything ๎‚‘๎‚…๎‚ ๎‚†๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚‰๎‚—๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚ˆ๎‚๎‚—๎‚‡๎‚“๎‚š๎‚‰๎‚– ๎‚’๎‚‰๎‚› ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚—๎‚™๎‚”๎‚”๎‚“๎‚–๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚“๎‚–
๎‚๎‚’ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚Œ๎‚๎‚‹๎‚Œ๎‚‰๎‚—๎‚˜ ๎‚๎‚‰๎‚š๎‚‰๎‚ ๎‚“๎‚Š ๎‚–๎‚‰๎‚•๎‚™๎‚๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚’ ๎‚… ๎‚‹๎‚๎‚š๎‚‰๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚†๎‚…๎‚—๎‚‰๎‡€ ๎€‹๎‚˜ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚
๎‚˜๎‚Œ๎‚“๎‚™๎‚‹๎‚Œ๎†ฝ ๎‚—๎‚๎‚’๎‚‡๎‚‰ ๎‚๎‚˜ ๎‚‘๎‚๎‚‹๎‚Œ๎‚˜ ๎‚Š๎‚“๎‚– ๎‚๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚‡๎‚‰ ๎‚‡๎‚…๎‚™๎‚—๎‚‰ ๎‚˜๎‚–๎‚“๎‚™๎‚†๎‚๎‚‰ ๎‚Š๎‚“๎‚– ๎‚”๎‚–๎‚“๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚†๎‚™๎‚๎‚๎‚ˆ๎‚— ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚… ๎‚˜๎‚“๎‚“๎‚ ๎‚™๎‚”๎‚ˆ๎‚…๎‚˜๎‚‰๎‡€
๎ˆ๎ˆ•๎‡€ ๎€๎‚“๎‚˜ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚’๎‚Š๎‚™๎‚—๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ -Wall๎†ฝ -Wextra ๎‚…๎‚’๎‚ˆ -Wmost๎‡€
156 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
Appendix C
C++ reserved words
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚๎‚๎‚—๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚— ๎‚–๎‚‰๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ ๎‚›๎‚“๎‚–๎‚ˆ๎‚— ๎‚Š๎‚–๎‚“๎‚‘ ๎€…๎ŠŠ๎ŠŠ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰๎‚๎‚“๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰๎‡€ ๎€™๎‚“๎‚–๎‚ˆ๎‚—
๎‚—๎‚™๎Šฒ๎‚œ๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚…๎‚’ ๎‚…๎‚—๎‚˜๎‚‰๎‚–๎‚๎‚—๎‚ ๎‚…๎‚–๎‚‰ ๎‚–๎‚‰๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ ๎‚›๎‚“๎‚–๎‚ˆ๎‚— ๎‚…๎‚ˆ๎‚ˆ๎‚‰๎‚ˆ ๎‚๎‚’ ๎€…๎ŠŠ๎ŠŠ๎ˆ๎ˆ๎‡€ ๎€ƒ๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚—๎‚‰๎‚‘๎‚…๎‚’๎‚˜๎‚๎‚‡๎‚— ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚†๎‚‰๎‚‰๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚‰๎‚ˆ
๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚ˆ ๎‚›๎‚“๎‚–๎‚ˆ ๎‚ˆ๎‚‰๎‚๎‚‰๎‚˜๎‚‰ ๎‚๎‚’ ๎€…๎ŠŠ๎ŠŠ๎ˆ๎ˆ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚๎‚’๎‚‹ ๎‚… ๎‚‡๎‚๎‚…๎‚—๎‚—๎‡€
๎‚…๎‚๎‚๎‚‹๎‚’๎‚…๎‚— ๎‡ธ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‡ ๎‚‡๎‚…๎‚—๎‚˜ ๎‚’๎‚“๎‚˜๎‡ ๎‚‰๎‚• ๎‚˜๎‚Œ๎‚๎‚—
๎‚…๎‚๎‚๎‚‹๎‚’๎‚“๎‚Š ๎‡ธ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚˜๎‚๎‚”๎‚‰ ๎‡ธ ๎‚’๎‚™๎‚๎‚๎‚”๎‚˜๎‚– ๎‡ธ ๎‚˜๎‚Œ๎‚–๎‚“๎‚›
๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚‰๎‚๎‚‰๎‚˜๎‚‰ ๎‡ธ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–
๎‚…๎‚’๎‚ˆ๎‡ ๎‚‰๎‚• ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡๎‡ ๎‚‡๎‚…๎‚—๎‚˜ ๎‚“๎‚– ๎‚˜๎‚–๎‚
๎‚…๎‚—๎‚‘ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚“๎‚–๎‡ ๎‚‰๎‚• ๎‚˜๎‚๎‚”๎‚‰๎‚๎‚ˆ
๎‚˜๎‚Œ๎‚–๎‚‰๎‚…๎‚ˆ๎‡ ๎‚๎‚“๎‚‡๎‚…๎‚ ๎‡ธ ๎‚‰๎‚œ๎‚”๎‚“๎‚–๎‚˜ ๎‚“๎‚š๎‚‰๎‚–๎‚–๎‚๎‚ˆ๎‚‰ ๎‡ธ ๎‚˜๎‚๎‚”๎‚‰๎‚’๎‚…๎‚‘๎‚‰
๎‚†๎‚๎‚˜๎‚…๎‚’๎‚ˆ ๎Šฑ๎‚’๎‚…๎‚๎‡ธ ๎‚”๎‚–๎‚๎‚š๎‚…๎‚˜๎‚‰ ๎‚™๎‚—๎‚๎‚’๎‚‹
๎‚†๎‚๎‚˜๎‚“๎‚– ๎‚Š๎‚–๎‚๎‚‰๎‚’๎‚ˆ ๎‚”๎‚–๎‚“๎‚˜๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚š๎‚๎‚–๎‚˜๎‚™๎‚…๎‚
๎‚‡๎‚Œ๎‚…๎‚–๎ˆ๎ˆ•๎‡ ๎‚˜ ๎‡ธ ๎‚‘๎‚™๎‚˜๎‚…๎‚†๎‚๎‚‰ ๎‚”๎‚™๎‚†๎‚๎‚๎‚‡ ๎‚œ๎‚“๎‚–
๎‚‡๎‚Œ๎‚…๎‚–๎ˆ’๎ˆ‘๎‡ ๎‚˜ ๎‡ธ ๎‚’๎‚…๎‚‘๎‚‰๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚–๎‚‰๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚”๎‚–๎‚‰๎‚˜๎‡ ๎‚‡๎‚…๎‚—๎‚˜ ๎‚œ๎‚“๎‚–๎‡ ๎‚‰๎‚•
๎‚‡๎‚…๎‚˜๎‚‡๎‚Œ ๎‚’๎‚‰๎‚› ๎‚—๎‚˜๎‚…๎‚˜๎‚๎‚‡๎‡ ๎‚…๎‚—๎‚—๎‚‰๎‚–๎‚˜ ๎‡ธ
๎‚‡๎‚๎‚…๎‚—๎‚— ๎‚’๎‚“๎‚‰๎‚œ๎‚‡๎‚‰๎‚”๎‚˜ ๎‡ธ ๎‚—๎‚˜๎‚…๎‚˜๎‚๎‚‡๎‡ ๎‚‡๎‚…๎‚—๎‚˜
๎‚‡๎‚“๎‚‘๎‚”๎‚ ๎‚˜๎‚‰๎‚‘๎‚”๎‚๎‚…๎‚˜๎‚‰
๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚‰๎‚œ๎‚”๎‚– ๎‡ธ
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 157
Appendix D
Operator priority
๎€–๎‚Œ๎‚‰ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚…๎‚ˆ๎‚“๎‚”๎‚˜๎‚‰๎‚ˆ ๎‚๎‚— ๎‚๎‚’ ๎‚ˆ๎‚‰๎‚—๎‚‡๎‚‰๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚“๎‚Š ๎‚”๎‚–๎‚๎‚“๎‚–๎‚๎‚˜๎‚๎‡€ ๎€‘๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚‡๎‚‰๎‚๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎‚˜๎‚…๎‚†๎‚๎‚‰ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚”๎‚–๎‚๎‚“๎‚–๎‚๎‚˜๎‚ ๎‚๎‚‰๎‚š๎‚‰๎‚๎†ฝ ๎‚‰๎‚š๎‚‰๎‚’ ๎‚๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚…๎‚–๎‚‰ ๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚… ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚–๎‚“๎‚› ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚‰๎‚๎‚๎‡€
๎€Ž๎‡€ ๎‚˜๎‚“ ๎€”๎‡€ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚— ๎‚Š๎‚“๎‚– ๎‡Œ๎‚๎‚‰๎Šน๎‡‚๎‚˜๎‚“๎‡‚๎‚–๎‚๎‚‹๎‚Œ๎‚˜ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚๎‚š๎‚๎‚˜๎‚๎‡๎†ฝ ๎‚…๎‚’๎‚ˆ ๎€”๎‡€ ๎‚˜๎‚“ ๎€Ž๎‡€ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚— ๎‚Š๎‚“๎‚– ๎‡Œ๎‚–๎‚๎‚‹๎‚Œ๎‚˜๎‡‚๎‚˜๎‚“๎‡‚๎‚๎‚‰๎Šน ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚๎‚š๎‚๎‚˜๎‚๎‡๎‡€
๎€…๎‚…๎‚˜๎‚‰๎‚‹๎‚“๎‚–๎‚ ๎€‘๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎€๎‚…๎‚‘๎‚‰ ๎€ƒ๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚๎‚š๎‚๎‚˜๎‚
๎€”๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰ ๎‡ฆ๎‡ง ๎€ˆ๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚๎‚ ๎€Ž๎‡€ ๎‚˜๎‚“ ๎€”๎‡€
๎‡ฌ๎‡ญ ๎€ƒ๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚˜๎‚“ ๎‚…๎‚’ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚’ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚
๎‡‚๎Š• ๎€ƒ๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚˜๎‚“ ๎‚… ๎Šฑ๎‚‰๎‚๎‚ˆ ๎‚“๎‚Š ๎‚… ๎‚‹๎‚๎‚š๎‚‰๎‚’ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚๎‚’ ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‡‚
๎‚˜๎‚™๎‚–๎‚‰
๎‡€ ๎€ƒ๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚˜๎‚“ ๎‚… ๎Šฑ๎‚‰๎‚๎‚ˆ ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰
๎€—๎‚’๎‚…๎‚–๎‚ ๎ŠŠ ๎€‹๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎‚˜๎‚ ๎€”๎‡€ ๎‚˜๎‚“ ๎€Ž๎‡€
๎‡‚ ๎€‘๎‚”๎‚”๎‚“๎‚—๎‚๎‚˜๎‚‰
๎ŠŠ๎ŠŠ ๎€‹๎‚’๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜
๎‡‚๎‡‚ ๎€†๎‚‰๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜
๎‡„ ๎€Ž๎‚“๎‚‹๎‚๎‚‡ ๎‚’๎‚‰๎‚‹๎‚…๎‚˜๎‚๎‚“๎‚’
๎Š‘ ๎€‹๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚…๎‚๎‚ ๎‚†๎‚๎‚˜๎‚—
๎Šจ ๎€’๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚๎‚’๎‚‹
๎€’๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚ˆ๎‚‰๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚๎‚’๎‚‹
๎‡ฆ๎‚‡๎‚…๎‚—๎‚˜๎‡ง ๎€–๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’
๎‚—๎‚๎‚ž๎‚‰๎‚“๎‚Š ๎€•๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜
๎€ƒ๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‡ธ ๎€’๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚˜ ๎€Ž๎‡€ ๎‚˜๎‚“ ๎€”๎‡€
๎‡š ๎€†๎‚๎‚š๎‚๎‚—๎‚๎‚“๎‚’
๎Š† ๎€๎‚“๎‚ˆ๎‚™๎‚๎‚“
๎ŠŠ ๎€•๎‚™๎‚‘ ๎‚“๎‚Š ๎‚˜๎‚›๎‚“ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚— ๎‚“๎‚– ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚… ๎‚’๎‚™๎‚‘๎‡‚
๎‚†๎‚‰๎‚–
๎‡‚ ๎€•๎‚™๎‚†๎‚—๎‚˜๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚›๎‚“ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚–๎‚— ๎‚“๎‚– ๎‚˜๎‚›๎‚“ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚—
๎€•๎‚Œ๎‚๎Šน ๎Š”๎Š” ๎€„๎‚๎‚’๎‚…๎‚–๎‚ ๎‚—๎‚Œ๎‚๎Šน ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‰๎Šน ๎€Ž๎‡€ ๎‚˜๎‚“ ๎€”๎‡€
๎Š•๎Š• ๎€„๎‚๎‚’๎‚…๎‚–๎‚ ๎‚—๎‚Œ๎‚๎Šน ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚๎‚‹๎‚Œ๎‚˜
๎€…๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’ ๎Š” ๎Š”๎Š ๎€•๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‚๎‚ ๎‚๎‚‰๎‚—๎‚— ๎‚˜๎‚Œ๎‚…๎‚’๎†ฝ ๎‚๎‚‰๎‚—๎‚— ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚“๎‚– ๎‚‰๎‚•๎‚™๎‚…๎‚ ๎‚˜๎‚“ ๎€Ž๎‡€ ๎‚˜๎‚“ ๎€”๎‡€
๎Š• ๎Š•๎Š ๎€•๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‚๎‚ ๎‚‹๎‚–๎‚‰๎‚…๎‚˜๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚’๎†ฝ ๎‚‹๎‚–๎‚‰๎‚…๎‚˜๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚“๎‚– ๎‚‰๎‚•๎‚™๎‚…๎‚ ๎‚˜๎‚“
๎Š๎Š ๎€‡๎‚•๎‚™๎‚…๎‚ ๎‚Ÿ
๎‡„๎Š ๎€†๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚Š๎‚–๎‚“๎‚‘
๎€„๎‚๎‚˜ ๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹ ๎Šจ ๎€„๎‚๎‚˜๎‚›๎‚๎‚—๎‚‰ ๎€ƒ๎‚’๎‚ˆ ๎€Ž๎‡€ ๎‚˜๎‚“ ๎€”๎‡€
๎Š“ ๎€„๎‚๎‚˜๎‚›๎‚๎‚—๎‚‰ ๎‚‰๎‚œ๎‚‡๎‚๎‚™๎‚—๎‚๎‚š๎‚‰ ๎€‘๎‚–
๎‡œ ๎€„๎‚๎‚˜๎‚›๎‚๎‚—๎‚‰ ๎€‘๎‚–
๎€Ž๎‚“๎‚‹๎‚๎‚‡ ๎Šจ๎Šจ ๎€Ž๎‚“๎‚‹๎‚๎‚‡ ๎€ƒ๎‚’๎‚ˆ ๎€Ž๎‡€ ๎‚˜๎‚“ ๎€”๎‡€
๎‡œ๎‡œ ๎€Ž๎‚“๎‚‹๎‚๎‚‡ ๎€‘๎‚–
158 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎€…๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‡‡๎†ฟ ๎€–๎‚‰๎‚–๎‚’๎‚…๎‚–๎‚ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎€”๎‡€ ๎‚˜๎‚“ ๎€Ž๎‡€
๎€ƒ๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎Š ๎€ƒ๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎€”๎‡€ ๎‚˜๎‚“ ๎€Ž๎‡€
๎ŠŠ๎Š ๎‡‚๎Š ๎‡ธ๎Š ๎‡š๎Š ๎€‹๎‚’๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎†ฝ ๎‚ˆ๎‚‰๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎†ฝ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚˜๎†ฝ ๎‚ˆ๎‚๎‚š๎‚๎‚—๎‚๎‚“๎‚’
๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜
๎Š†๎Š ๎Šจ๎Š ๎Š“๎Š
๎‡œ๎Š ๎Š”๎Š”๎Š ๎Š•๎Š•๎Š
๎€๎‚“๎‚ˆ๎‚™๎‚๎‚“๎†ฝ ๎‚๎‚“๎‚‹๎‚๎‚‡ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚—๎‚Œ๎‚๎Šน ๎‚˜๎‚Œ๎‚‰๎‚’ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‡‚
๎‚‘๎‚‰๎‚’๎‚˜
๎€•๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰ ๎†ฝ ๎€ƒ๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚– ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚—๎‚‰๎‚”๎‚…๎‚–๎‚…๎‚˜๎‚“๎‚– ๎€Ž๎‡€ ๎‚˜๎‚“ ๎€”๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 159
Appendix E
Example of development conventions
๎€ƒ๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚‰๎‚‹๎‚๎‚’๎‚’๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚…๎‚’ ๎€‹๎€– ๎‚”๎‚–๎‚“๎‚Ž๎‚‰๎‚‡๎‚˜๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚˜๎‚‰๎‚…๎‚‘ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚…๎‚‹๎‚–๎‚‰๎‚‰ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚’๎‡‚
๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚…๎‚”๎‚”๎‚๎‚๎‚‰๎‚ˆ๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚…๎‚๎‚‘ ๎‚๎‚— ๎‚˜๎‚“ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰ ๎‚… ๎‚‡๎‚“๎‚Œ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€ ๎€ˆ๎‚™๎‚–๎‚˜๎‚Œ๎‚‰๎‚–๎‚‘๎‚“๎‚–๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚๎‚‹๎‚Œ๎‚˜
๎‚‡๎‚Œ๎‚“๎‚๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚Œ๎‚‰๎‚๎‚”๎‚— ๎‚˜๎‚“ ๎‚–๎‚‰๎‚ˆ๎‚™๎‚‡๎‚‰ ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘๎‚‘๎‚๎‚’๎‚‹ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚—๎‡€
Information
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚— ๎‚…๎‚–๎‚‰ ๎‚…๎‚’ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€ ๎€•๎‚“๎‚‘๎‚‰ ๎‚‡๎‚Œ๎‚“๎‚๎‚‡๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚…๎‚–๎‚†๎‚๎‡‚
๎‚˜๎‚–๎‚…๎‚–๎‚ ๎‚…๎‚’๎‚ˆ ๎‚“๎‚”๎‚‰๎‚’ ๎‚Š๎‚“๎‚– ๎‚ˆ๎‚๎‚—๎‚‡๎‚™๎‚—๎‚—๎‚๎‚“๎‚’๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚“๎‚– ๎‚˜๎‚…๎‚๎‚‰๎‚’ ๎‚…๎‚—
๎‚… ๎‚Š๎‚“๎‚™๎‚’๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚๎‚Š ๎‚’๎‚“ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’ ๎‚Œ๎‚…๎‚— ๎‚†๎‚‰๎‚‰๎‚’ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚“ ๎‚†๎‚‰
๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ๎‡€ ๎€†๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚˜๎‚“๎‚“๎‚๎‚— ๎‚“๎‚– ๎‚…๎‚ˆ๎‚š๎‚…๎‚’๎‚‡๎‚‰๎‚ˆ ๎‚‰๎‚ˆ๎‚๎‚˜๎‚“๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚…๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜
๎‚—๎‚“๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎€™๎‚Œ๎‚‰๎‚–๎‚‰ ๎‚…๎‚‹๎‚–๎‚‰๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚†๎‚‰๎‚‰๎‚’ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚‰๎‚œ๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚”๎‚–๎‚“๎‚Ž๎‚‰๎‚‡๎‚˜๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‡‚
๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‡๎‚๎‚‰๎‚…๎‚–๎‚๎‚ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚Š๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚…๎‚‡๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚’๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚Ž๎‚‰๎‚‡๎‚˜ ๎‚๎‚’ ๎‚•๎‚™๎‚‰๎‚—๎‚˜๎‚๎‚“๎‚’๎‡€
E.1 Files encoding
๎€–๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰๎‚— ๎‚…๎‚–๎‚‰ ๎‚‰๎‚’๎‚‡๎‚“๎‚ˆ๎‚‰๎‚ˆ ๎‚๎‚’ ๎€—๎€–๎€ˆ๎ˆ— ๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‡€
๎€–๎‚Œ๎‚‰ ๎‚๎‚๎‚’๎‚‰ ๎‚Š๎‚‰๎‚‰๎‚ˆ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‚๎‚— \n ๎‡ฆ๎‡Œ๎‚๎‚๎‚’๎‚‰ ๎‚Š๎‚‰๎‚‰๎‚ˆ๎‡ ๎‚๎‚’ ๎€—๎‚’๎‚๎‚œ ๎‚Š๎‚“๎‚–๎‚‘๎‚…๎‚˜๎‡ง๎‡€
E.2 Code layout and indentation
E.2.1 Maximum lengths
๎€ƒ ๎‚๎‚๎‚’๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚– ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚‰๎‚œ๎‚‡๎‚‰๎‚‰๎‚ˆ ๎ˆ๎ˆ๎ˆ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‚—๎‡€
๎€ƒ ๎‚๎‚๎‚’๎‚‰ ๎‚“๎‚Š ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚‰๎‚œ๎‚‡๎‚‰๎‚‰๎‚ˆ ๎ˆ๎ˆ๎ˆ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‚—๎‡€
๎€ƒ ๎Šฑ๎‚๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚‰๎‚œ๎‚‡๎‚‰๎‚‰๎‚ˆ ๎ˆ“๎ˆ๎ˆ๎ˆ ๎‚๎‚๎‚’๎‚‰๎‚— ๎‡ฆ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚๎‚’๎‚‹ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎‡ง๎‡€
๎€ƒ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚‰๎‚œ๎‚‡๎‚‰๎‚‰๎‚ˆ ๎ˆ”๎ˆ๎ˆ ๎‚๎‚๎‚’๎‚‰๎‚—๎‡€
E.2.2 Code indentation
๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚— ๎‚๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚—๎‚”๎‚…๎‚‡๎‚‰๎‚—๎†ฟ ๎‚“๎‚’๎‚‰ ๎‚๎‚‰๎‚š๎‚‰๎‚ ๎‚“๎‚Š ๎‚๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚—๎‚”๎‚“๎‚’๎‚ˆ๎‚— ๎‚˜๎‚“ ๎ˆ“ ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‚—๎‡€ ๎€–๎‚Œ๎‚‰
๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚…๎‚† ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‚…๎‚— ๎‚…๎‚’ ๎‚๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‚๎‚— ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ๎‡€
160 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‚—๎‡€
๎€ƒ ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‚๎‚— ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚๎‚‰๎Šน ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚… ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚”๎‚‰๎‚’๎‚๎‚’๎‚‹ ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚—
๎‚๎‚˜๎‡€
๎€–๎‚Œ๎‚‰ ๎‚“๎‚”๎‚‰๎‚’๎‚๎‚’๎‚‹ ๎‚†๎‚–๎‚…๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚†๎‚๎‚“๎‚‡๎‚ ๎‚๎‚— ๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚… ๎‚’๎‚‰๎‚› ๎‚๎‚๎‚’๎‚‰๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚†๎‚๎‚“๎‚‡๎‚ ๎‚‡๎‚๎‚“๎‚—๎‚๎‚’๎‚‹ ๎‚†๎‚–๎‚…๎‚‡๎‚‰ ๎‚๎‚— ๎‚…๎‚๎‚—๎‚“ ๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚…
๎‚’๎‚‰๎‚› ๎‚๎‚๎‚’๎‚‰๎‡€
๎€ƒ ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‚๎‚— ๎‚๎‚‰๎Šน ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚…๎Šน๎‚‰๎‚– ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‡€
๎€ƒ ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‚๎‚— ๎‚๎‚‰๎Šน ๎‚…๎Šน๎‚‰๎‚– ๎‚… ๎‚‡๎‚“๎‚‘๎‚‘๎‚…๎‡€
๎€–๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚‘๎‚๎‚‡๎‚“๎‚๎‚“๎‚’ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚๎‚— ๎‚—๎‚˜๎‚™๎‚‡๎‚ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚…๎‚—๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚’๎‚ˆ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‡€
๎€ˆ๎‚“๎‚– ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚๎‚ ๎‚›๎‚๎‚˜๎‚Œ ๎‚‘๎‚…๎‚’๎‚ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚—๎†ฝ ๎‚๎‚Š ๎‚๎‚˜ ๎‚๎‚— ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚˜๎‚“ ๎‚”๎‚๎‚…๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚’ ๎‚—๎‚‰๎‚š๎‚‰๎‚–๎‚…๎‚ ๎‚๎‚๎‚’๎‚‰๎‚—๎†ฝ
๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚…๎‚–๎‚‰ ๎‚๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚”๎‚“๎‚—๎‚๎‚˜๎‚๎‚“๎‚’๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚”๎‚‰๎‚’๎‚๎‚’๎‚‹ ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚…๎‚๎‚๎‡€
Good example
...
uint32_t processing_function ( linked_list_t *p_param1 , uint32_t ui32_param2 ,
const unsigned char *s_param3 )
{
uint32_t ui32_result = 0;
element_t *pp_out_param4 = NULL;
if ((NULL == p_param1) || (NULL == s_param3 ))
{
ui32_result = 0;
goto End;
}
ui32_result = function_with_many_params (p_param1 , ui32_param2 , s_param3 ,
pp_out_param4 );
if (1 == ui32_result)
{
...
}
End:
return ui32_result;
}
E.3 Standard types
๎€‹๎‚Š ๎‚˜๎‚Œ๎‚‰ stdint.h ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎‚๎‚— ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎†ฝ ๎‚๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚†๎‚‰๎‚’๎‚‰๎Šฑ๎‚˜ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰๎‚—
๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚˜ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚—๎‡€ ๎€‹๎‚’ ๎‚๎‚˜๎‚— ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚…๎‚— ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚—๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ–๎‡€
๎€‹๎‚Š ๎‚˜๎‚Œ๎‚‰ stdbool.h ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎‚๎‚— ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎†ฝ ๎‚๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚†๎‚‰๎‚’๎‚‰๎Šฑ๎‚˜ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚˜
๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚—๎‡€ ๎€‹๎‚’ ๎‚๎‚˜๎‚— ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ bool ๎‚˜๎‚๎‚”๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚…๎‚— ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‡ฆ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚–
๎Šฑ๎‚๎‚‰ ๎‚Š๎‚–๎‚“๎‚‘ ๎„Œ๎„ˆ๎„ˆ ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎ˆ“๎‡€๎ˆ—๎‡€๎ˆ‘๎‡ง๎‡€ ๎€–๎‚Œ๎‚‰ _Bool ๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚— ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚˜๎‚๎‚†๎‚๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ
๎€…๎ˆ˜๎ˆ˜ ๎‚…๎‚’๎‚ˆ ๎‚—๎‚™๎‚†๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚˜ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‚—๎‡€
/* Copyright (C) 1998 -2013 Free Software Foundation , Inc . */
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 161
/*
* ISO C Standard: 7.16 Boolean type and values < stdbool.h>
*/
#ifndef _STDBOOL_H
#define _STDBOOL_H
#ifndef __cplusplus
#define bool _Bool
#define true 1
#define false 0
#else // __cplusplus
/* Supporting <stdbool.h> in C++ is a GCC extension . */
#define _Bool bool
#define bool bool
#define false false
#define true true
#endif // __cplusplus
/* Signal that all the definitions are present . */
#define __bool_true_false_are_defined 1
#endif // stdbool .h
E.4 Naming
E.4.1 Language for implementation
๎€–๎‚Œ๎‚‰ ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚’๎‚…๎‚‘๎‚๎‚’๎‚‹ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‚‰๎‚—๎†ฝ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚—๎†ฝ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰๎‚—๎†ฝ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚—๎†ฝ ๎‚˜๎‚๎‚”๎‚‰๎‚—๎†ฝ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚Š๎‚™๎‚’๎‚‡๎‡‚
๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎€‡๎‚’๎‚‹๎‚๎‚๎‚—๎‚Œ๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎€‡๎‚’๎‚‹๎‚๎‚๎‚—๎‚Œ ๎‚…๎‚š๎‚“๎‚๎‚ˆ๎‚— ๎‚‘๎‚๎‚œ๎‚๎‚’๎‚‹ ๎‚›๎‚“๎‚–๎‚ˆ๎‚— ๎‚๎‚’ ๎€ˆ๎‚–๎‚‰๎‚’๎‚‡๎‚Œ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎€… ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚…๎‚–๎‚‰ ๎‚๎‚’ ๎€‡๎‚’๎‚‹๎‚๎‚๎‚—๎‚Œ ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚‰๎‚’๎‚˜๎‚๎‚–๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚๎‚— ๎‚˜๎‚Œ๎‚™๎‚— ๎‚‘๎‚“๎‚–๎‚‰
๎‚‡๎‚“๎‚Œ๎‚‰๎‚–๎‚‰๎‚’๎‚˜๎‡€
๎€–๎‚Œ๎‚‰ ๎‚๎‚…๎‚’๎‚‹๎‚™๎‚…๎‚‹๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎€‡๎‚’๎‚‹๎‚๎‚๎‚—๎‚Œ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚‰๎‚‹๎‚๎‚’๎‚’๎‚๎‚’๎‚‹ ๎‚“๎‚Š
๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚…๎‚๎‚ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚—๎‡€
E.4.2 Naming of source ๎‚พle directories
๎€–๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚“๎‚–๎‚‹๎‚…๎‚’๎‚๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‚‰๎‚—๎‡€ ๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚๎‚…๎‚–๎‚‹๎‚‰ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚‰๎‚ˆ ๎‚˜๎‚“
๎‚‡๎‚–๎‚‰๎‚…๎‚˜๎‚‰ ๎‚… ๎‚˜๎‚–๎‚‰๎‚‰ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚˜๎‚“ ๎‚“๎‚–๎‚‹๎‚…๎‚’๎‚๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰๎‚—๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚˜๎‚“๎‚”๎‡‚๎‚๎‚‰๎‚š๎‚‰๎‚ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚“๎‚–๎‚ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰
๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‡€ ๎€•๎‚™๎‚†๎‡‚๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚“๎‚–๎‚๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚… ๎‚›๎‚…๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚–๎‚‰๎Šท๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚–๎‚๎‚˜๎‚‰๎‚–๎‚๎‚… ๎‚Š๎‚“๎‚–
๎‚‹๎‚–๎‚“๎‚™๎‚”๎‚๎‚’๎‚‹ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰๎‚—๎‡€
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚—๎‚Œ๎‚“๎‚›๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚–๎‚‹๎‚…๎‚’๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚“๎‚–๎‚๎‚‰๎‚— ๎‚Š๎‚“๎‚– ๎‚… ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚๎‚’๎‚‹ ๎‚™๎‚˜๎‚๎‚๎‚๎‚˜๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‡‚
๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฟ
162 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎€–๎‚–๎‚‰๎‚‰ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎€…๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜
๎‚™๎‚˜๎‚๎‚๎‚— ๎€„๎‚…๎‚—๎‚๎‚‡ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚“๎‚–๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚
๎‚™๎‚˜๎‚๎‚๎‚—๎‡š๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚— ๎€†๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚“๎‚–๎‚ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚๎‚’๎‚‹ ๎‚…๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‡ฆ๎€ƒ๎€’๎€‹๎‡ง
๎‚™๎‚˜๎‚๎‚๎‚—๎‡š๎‚‡๎‚“๎‚๎‚๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎€†๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚“๎‚–๎‚ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚…๎‚๎‚ ๎‚‡๎‚“๎‚๎‚๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚๎‚”๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚…
๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‡ฆ๎‚๎‚๎‚—๎‚˜๎‚—๎†ฝ ๎‚—๎‚˜๎‚…๎‚‡๎‚๎†ฝ ๎‚…๎‚–๎‚–๎‚…๎‚๎†ฝ ๎‚Œ๎‚…๎‚—๎‚Œ ๎‚˜๎‚…๎‚†๎‚๎‚‰๎†ฝ etc.๎‡ง
๎‚™๎‚˜๎‚๎‚๎‚—๎‡š๎‚‡๎‚“๎‚’๎‚‡๎‚™๎‚–๎‚–๎‚‰๎‚’๎‚‡๎‚ ๎€†๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚“๎‚–๎‚ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‘๎‚™๎‚˜๎‚‰๎‚œ๎†ฝ ๎‚—๎‚‰๎‚‘๎‚…๎‚”๎‚Œ๎‚“๎‚–๎‚‰๎‚—๎†ฝ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‡‚
๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—
๎‚™๎‚˜๎‚๎‚๎‚—๎‡š๎‚˜๎‚Œ๎‚–๎‚‰๎‚…๎‚ˆ๎‚— ๎€†๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚“๎‚–๎‚ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚–๎‚‰๎‚…๎‚ˆ๎‚—
๎‡ ๎‡
E.4.3 Naming of header ๎‚พles and implementation ๎‚พles
๎€Š๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚”๎‚–๎‚‰๎Šฑ๎‚œ๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚˜๎‚“ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚˜๎‚Œ๎‚‰๎‚ ๎‚†๎‚‰๎‚๎‚“๎‚’๎‚‹๎‡€
๎€‹๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚๎‚— ๎‚๎‚“๎‚’๎‚‹๎†ฝ ๎‚๎‚˜ ๎‚๎‚— ๎‚…๎‚ˆ๎‚š๎‚๎‚—๎‚…๎‚†๎‚๎‚‰ ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰ ๎‚…๎‚’ ๎‚…๎‚†๎‚†๎‚–๎‚‰๎‚š๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚— ๎‚… ๎‚”๎‚–๎‚‰๎Šฑ๎‚œ๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚…๎‚†๎‚†๎‚–๎‚‰๎‚š๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜
๎‚†๎‚‰ ๎‚‡๎‚Œ๎‚“๎‚—๎‚‰๎‚’ ๎‚๎‚’ ๎‚—๎‚™๎‚‡๎‚Œ ๎‚… ๎‚›๎‚…๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚˜ ๎‚ˆ๎‚“๎‚‰๎‚— ๎‚’๎‚“๎‚˜ ๎‚‡๎‚“๎‚’๎Šท๎‚๎‚‡๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ ๎‚…๎‚’ ๎‚…๎‚๎‚–๎‚‰๎‚…๎‚ˆ๎‚ ๎‚‰๎‚œ๎‚๎‚—๎‚˜๎‚๎‚’๎‚‹ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‡ฆ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‚‰๎‚—๎†ฝ
๎‚˜๎‚Œ๎‚๎‚–๎‚ˆ ๎‚”๎‚…๎‚–๎‚˜๎‚ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‚‰๎‚—๎†ฝ etc.๎‡ง๎‡€
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚๎‚๎‚—๎‚˜ ๎‚‹๎‚๎‚š๎‚‰๎‚— ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰๎‚— ๎‚“๎‚Š ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰๎‚—๎†ฟ utils_linked_list.h๎†ฝ
utils_linked_list.c๎†ฝ utils_mutex.h๎†ฝ utils_mutex.c๎†ฝ utils_thread.h๎†ฝ utils_thread.c ๎‡
E.4.4 Naming of macros
๎€’๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚™๎‚”๎‚”๎‚‰๎‚– ๎‚‡๎‚…๎‚—๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰๎‚—๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚›๎‚“๎‚–๎‚ˆ๎‚— ๎‚‘๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚™๎‚” ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰
๎‚—๎‚‰๎‚”๎‚…๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚‡๎‚“๎‚–๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚‘๎‚…๎‚˜๎‚‡๎‚Œ ๎‚…๎‚’ ๎‚…๎‚๎‚–๎‚‰๎‚…๎‚ˆ๎‚ ๎‚‰๎‚œ๎‚๎‚—๎‚˜๎‚๎‚’๎‚‹
๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎†ฟ ๎‚Š๎‚“๎‚– ๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚†๎‚‰๎‚๎‚“๎‚’๎‚‹๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚… ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‡€ ๎€–๎‚Œ๎‚‰
๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚Š ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚–๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚’๎‚…๎‚‘๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‡€
Good example
#define LOG_DEBUG(sMessage) write_log_message(sMessage )
๎€–๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎†ฝ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚…๎‚š๎‚“๎‚๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎†ฝ ๎‚™๎‚—๎‚‰๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰
๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚๎‚’ ๎‚™๎‚”๎‚”๎‚‰๎‚– ๎‚‡๎‚…๎‚—๎‚‰๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚๎‚ ๎‚—๎‚˜๎‚“๎‚” ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‚๎‚— ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚…๎‚’ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚‡๎‚“๎‚–๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‡€
Good example
#define UTILS_LINKED_LIST_H
E.4.5 Naming of types
๎€–๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚˜๎‚๎‚”๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ typedef ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚›๎‚–๎‚๎‚˜๎‚˜๎‚‰๎‚’ ๎‚๎‚’ ๎‚๎‚“๎‚›๎‚‰๎‚– ๎‚‡๎‚…๎‚—๎‚‰๎†ฝ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰
๎‚—๎‚™๎Šฒ๎‚œ _t๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚›๎‚“๎‚–๎‚ˆ๎‚— ๎‚‘๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚™๎‚” ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚—๎‚‰๎‚”๎‚…๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚‡๎‚“๎‚–๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‡€
๎€™๎‚Œ๎‚‰๎‚’ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚’๎‚‹ ๎‚… ๎‚˜๎‚๎‚”๎‚‰ ๎‚Š๎‚“๎‚– ๎‚…๎‚’ ๎‚‰๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚– ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ enum ๎‚“๎‚–
struct ๎‚‘๎‚™๎‚—๎‚˜ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚™๎Šฒ๎‚œ _tag๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚…๎Šน๎‚‰๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚๎‚“๎‚—๎‚๎‚’๎‚‹ ๎‚†๎‚–๎‚…๎‚‡๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰
๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰๎†ฝ ๎‚›๎‚๎‚˜๎‚Œ _tag ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚ˆ ๎‚†๎‚ _t๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 163
Good example
typedef enum status_tag {
...
} status_t;
typedef signed long sint32_t;
typedef struct linked_list_tag
{
...
} linked_list_t;
E.4.6 Naming of functions
๎€–๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚”๎‚–๎‚‰๎Šฑ๎‚œ๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‡ฆ๎‚“๎‚– ๎‚…๎‚†๎‚†๎‚–๎‚‰๎‚š๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰๎‡ง ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚
๎‚˜๎‚“ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚๎‚˜ ๎‚†๎‚‰๎‚๎‚“๎‚’๎‚‹๎‚—๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚›๎‚“๎‚–๎‚ˆ๎‚— ๎‚‘๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚™๎‚” ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚—๎‚‰๎‚”๎‚…๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰
๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚‡๎‚“๎‚–๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚›๎‚–๎‚๎‚˜๎‚˜๎‚‰๎‚’ ๎‚๎‚’ ๎‚๎‚“๎‚›๎‚‰๎‚– ๎‚‡๎‚…๎‚—๎‚‰๎‡€
Good example
status_t utils_create_linked_list(linked_list_t ** pp_list);
status_t utils_delete_linked_list(linked_list_t *pp_list);
E.4.7 Naming of variables
๎€˜๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚–๎‚— ๎‚›๎‚๎‚๎‚ ๎‚‡๎‚“๎‚’๎‚—๎‚๎‚—๎‚˜ ๎‚“๎‚Š ๎‚›๎‚“๎‚–๎‚ˆ๎‚— ๎‚—๎‚‰๎‚”๎‚…๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚‡๎‚“๎‚–๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎†ฝ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚—๎‚”๎‚…๎‚‡๎‚‰๎‚— ๎‚“๎‚–
๎‚™๎‚”๎‚”๎‚‰๎‚– ๎‚‡๎‚…๎‚—๎‚‰ ๎‚๎‚‰๎‚˜๎‚˜๎‚‰๎‚–๎‚—๎‡€ ๎€‡๎‚…๎‚‡๎‚Œ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎Šฑ๎‚‰๎‚– ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚Š๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‡ฆ๎‚˜๎‚๎‚”๎‚‰๎†ฝ
๎‚—๎‚๎‚‹๎‚’๎†ฝ ๎‚—๎‚๎‚ž๎‚‰๎†ฝ ๎‚–๎‚“๎‚๎‚‰๎†ฝ etc.๎‡ง๎‡€
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚˜๎‚…๎‚†๎‚๎‚‰ ๎‚—๎‚Œ๎‚“๎‚›๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎Šฑ๎‚œ๎‚‰๎‚— ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰๎‚— ๎‚…๎‚‡๎‚‡๎‚“๎‚–๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚˜๎‚๎‚”๎‚‰๎†ฝ ๎‚…๎‚— ๎‚›๎‚‰๎‚๎‚ ๎‚…๎‚— ๎‚…๎‚’
๎‚‰๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰ ๎‚Š๎‚“๎‚– ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎†ฟ
๎€’๎‚–๎‚‰๎Šฑ๎‚œ ๎€˜๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎€‡๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰
๎‚๎ˆ— ๎€•๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎ˆ—๎‡‚๎‚†๎‚๎‚˜ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚๎‚’๎‚˜๎ˆ—๎‡ ๎‚˜ ๎‚๎ˆ—๎‡ ๎‚†๎‚๎‚˜๎‚‰ ๎Š ๎ˆ๎†พ
๎‚™๎‚๎ˆ— ๎€—๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎ˆ—๎‡‚๎‚†๎‚๎‚˜ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚™๎‚๎‚’๎‚˜๎ˆ—๎‡ ๎‚˜ ๎‚™๎‚๎ˆ—๎‡ ๎‚†๎‚๎‚˜๎‚‰ ๎Š ๎ˆ๎€—๎†พ
๎‚๎ˆ๎ˆ• ๎€•๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎ˆ๎ˆ•๎‡‚๎‚†๎‚๎‚˜ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚๎‚’๎‚˜๎ˆ๎ˆ•๎‡ ๎‚˜ ๎‚๎ˆ๎ˆ•๎‡ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’ ๎Š ๎ˆ๎†พ
๎‚™๎‚๎ˆ๎ˆ• ๎€—๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎ˆ๎ˆ•๎‡‚๎‚†๎‚๎‚˜ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚™๎‚๎‚’๎‚˜๎ˆ๎ˆ•๎‡ ๎‚˜ ๎‚™๎‚๎ˆ๎ˆ•๎‡ ๎‚”๎‚“๎‚–๎‚˜ ๎Š ๎ˆ๎€—๎†พ
๎‚๎ˆ’๎ˆ‘ ๎€•๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎ˆ’๎ˆ‘๎‡‚๎‚†๎‚๎‚˜ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚๎‚’๎‚˜๎ˆ’๎ˆ‘๎‡ ๎‚˜ ๎‚๎ˆ’๎ˆ‘๎‡ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎Š ๎ˆ๎€Ž๎†พ
๎‚™๎‚๎ˆ’๎ˆ‘ ๎€—๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎ˆ’๎ˆ‘๎‡‚๎‚†๎‚๎‚˜ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚™๎‚๎‚’๎‚˜๎ˆ’๎ˆ‘๎‡ ๎‚˜ ๎‚™๎‚๎ˆ’๎ˆ‘๎‡ ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚– ๎Š ๎ˆ๎€—๎€Ž๎†พ
๎‚๎ˆ•๎ˆ“ ๎€•๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎ˆ•๎ˆ“๎‡‚๎‚†๎‚๎‚˜ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚๎‚’๎‚˜๎ˆ•๎ˆ“๎‡ ๎‚˜ ๎‚๎ˆ•๎ˆ“๎‡ ๎‚†๎‚๎‚‹๎‡ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎Š ๎ˆ๎€Ž๎€Ž๎†พ
๎‚™๎‚๎ˆ•๎ˆ“ ๎€—๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎ˆ•๎ˆ“๎‡‚๎‚†๎‚๎‚˜ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚™๎‚๎‚’๎‚˜๎ˆ•๎ˆ“๎‡ ๎‚˜ ๎‚™๎‚๎ˆ•๎ˆ“๎‡ ๎‚†๎‚๎‚‹๎‡ ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚– ๎Š ๎ˆ๎€—๎€Ž๎€Ž๎†พ
๎‚† ๎€„๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚†๎‚“๎‚“๎‚ ๎‚†๎‡ ๎‚๎‚—๎‡ ๎‚—๎‚‰๎‚˜ ๎Š ๎‚Š๎‚…๎‚๎‚—๎‚‰๎†พ
๎‚‡ ๎€…๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‚‡๎‚Œ๎‚…๎‚– ๎‚‡๎‡ ๎‚๎‚‰๎‚˜๎‚˜๎‚‰๎‚– ๎Š ๎‡‹๎‡›๎ˆ๎‡‹๎†พ
๎‚Š ๎€ˆ๎‚๎‚“๎‚…๎‚˜ ๎Šท๎‚“๎‚…๎‚˜ ๎‚Š๎‡ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎Š ๎ˆ๎‡€๎ˆ๎‚Š๎†พ
๎‚ˆ ๎€†๎‚“๎‚™๎‚†๎‚๎‚‰ ๎‚ˆ๎‚“๎‚™๎‚†๎‚๎‚‰ ๎‚ˆ๎‡ ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚‰๎‚ˆ๎‡ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎Š ๎ˆ๎‡€๎ˆ๎‚ˆ๎†พ
๎‚—๎‚ž ๎€–๎‚๎‚”๎‚‰ ๎‚—๎‚๎‚ž๎‚‰๎‡ ๎‚˜ ๎‚—๎‚๎‚ž๎‚‰๎‡ ๎‚˜ ๎‚—๎‚ž๎‡ ๎‚—๎‚˜๎‚–๎‚๎‚’๎‚‹๎‡ ๎‚๎‚‰๎‚’๎‚‹๎‚˜๎‚Œ ๎Š ๎ˆ๎€—๎†พ
๎‚‰ ๎€‡๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚๎‚”๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚™๎‚—๎‡ ๎‚˜ ๎‚‰๎‡ ๎‚—๎‚˜๎‚…๎‚˜๎‚™๎‚—๎‡ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎Š ๎€•๎€–๎€ƒ๎€–๎€—๎€•๎‡ ๎€‡๎€”๎€”๎†พ
๎‚—๎‚˜ ๎€•๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚๎‚๎‚’๎‚๎‚‰๎‚ˆ๎‡ ๎‚๎‚๎‚—๎‚˜๎‡ ๎‚˜ ๎‚—๎‚˜๎‡ ๎‚๎‚๎‚—๎‚˜๎†พ
๎‚… ๎€ƒ๎‚–๎‚–๎‚…๎‚ ๎‚™๎‚๎‚’๎‚˜๎ˆ’๎ˆ‘๎‡ ๎‚˜ ๎‚…๎‡ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚—๎‡ฌ๎ˆ๎ˆ๎‡ญ๎†พ
๎‚” ๎€’๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚๎‚๎‚’๎‚๎‚‰๎‚ˆ๎‡ ๎‚๎‚๎‚—๎‚˜๎‡ ๎‚˜๎‡ธ ๎‚”๎‡ ๎‚๎‚๎‚—๎‚˜ ๎Š ๎€๎€—๎€Ž๎€Ž๎†พ
164 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
๎€’๎‚–๎‚‰๎Šฑ๎‚œ ๎€˜๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎€‡๎‚œ๎‚…๎‚‘๎‚”๎‚๎‚‰
๎‚”๎‚” ๎€’๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚“๎‚Š ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚๎‚๎‚’๎‚๎‚‰๎‚ˆ๎‡ ๎‚๎‚๎‚—๎‚˜๎‡ ๎‚˜๎‡ธ๎‡ธ ๎‚”๎‚”๎‡ ๎‚๎‚๎‚—๎‚˜ ๎Š ๎€๎€—๎€Ž๎€Ž๎†พ
๎‚— ๎€•๎‚˜๎‚–๎‚๎‚’๎‚‹ ๎‚˜๎‚๎‚”๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚–๎‡ธ ๎‚—๎‡ ๎‚‘๎‚‰๎‚—๎‚—๎‚…๎‚‹๎‚‰ ๎Š ๎€๎€—๎€Ž๎€Ž๎†พ
๎‚›๎‚— ๎€•๎‚˜๎‚–๎‚๎‚’๎‚‹ ๎‚˜๎‚๎‚”๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚๎‚’ ๎‚™๎‚’๎‚๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚›๎‚‡๎‚Œ๎‚…๎‚–๎‡ ๎‚˜๎‡ธ ๎‚›๎‚—๎‡ ๎‚‘๎‚‰๎‚—๎‚—๎‚…๎‚‹๎‚‰ ๎Š ๎€๎€—๎€Ž๎€Ž๎†พ
E.5 Documentation
E.5.1 Format of tags for documentation
๎€–๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ Doxygen ๎‚˜๎‚“๎‚“๎‚๎‡‹๎‚— ๎‚˜๎‚…๎‚‹ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‡€ Doxygen
๎‚˜๎‚…๎‚‹๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚…๎‚๎‚ ๎‚†๎‚‰๎‚‹๎‚๎‚’ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ @ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‡€ ๎€–๎‚Œ๎‚‰ Doxygen ๎‚˜๎‚“๎‚“๎‚ ๎‚…๎‚๎‚—๎‚“ ๎‚…๎‚๎‚๎‚“๎‚›๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚…๎‚‡๎‚๎‚—๎‚๎‚…๎‚—๎‚Œ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‡€
๎€Š๎‚“๎‚›๎‚‰๎‚š๎‚‰๎‚–๎†ฝ ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚™๎‚’๎‚๎‚Š๎‚“๎‚–๎‚‘๎‚๎‚˜๎‚ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚˜๎‡‚๎‚—๎‚๎‚‹๎‚’ ๎‚”๎‚–๎‚‰๎Šฑ๎‚œ
๎‚Š๎‚“๎‚– Doxygen ๎‚‡๎‚“๎‚‘๎‚‘๎‚…๎‚’๎‚ˆ๎‚— ๎‚๎‚— ๎‚๎‚‘๎‚”๎‚“๎‚—๎‚‰๎‚ˆ๎‡€
๎€ƒ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜ ๎‚†๎‚‰๎‚‹๎‚๎‚’๎‚— ๎‚›๎‚๎‚˜๎‚Œ /*! ๎‚…๎‚’๎‚ˆ ๎‚‰๎‚’๎‚ˆ๎‚— ๎‚›๎‚๎‚˜๎‚Œ */๎‡€
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚— ๎‚“๎‚™๎‚˜๎‚๎‚๎‚’๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚๎‚’๎‚๎‚‘๎‚™๎‚‘ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜ ๎‚๎‚’ ๎‚… ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‡€
E.5.2 File header title block
๎€ƒ๎‚๎‚ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚…๎‚๎‚ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰๎‚‹๎‚๎‚’ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚๎‚˜๎‚๎‚‰ ๎‚†๎‚๎‚“๎‚‡๎‚ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎‚Š๎‚๎†ฟ
n
๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‡š ๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚˜๎‚“ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚–๎‡š๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚†๎‚‰๎‚๎‚“๎‚’๎‚‹๎‚—๎†พ
n
๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚’๎‚ ๎‡ฆ๎‚…๎‚’๎‚ˆ ๎‚๎‚Š ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚™๎‚˜๎‚Œ๎‚“๎‚–๎‡ง ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚”๎‚๎‚–๎‚๎‚‹๎‚Œ๎‚˜ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚๎‚‰๎†พ
n
๎‚˜๎‚Œ๎‚‰ Doxygen @file ๎‚˜๎‚…๎‚‹๎‡€ ๎€–๎‚Œ๎‚‰ @file ๎‚˜๎‚…๎‚‹ ๎‚‡๎‚…๎‚’ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚…๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰๎‡€ ๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰
๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚๎‚— ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚ˆ๎‚‰๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚๎‚’ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚˜๎‚Œ๎‚‰
@file ๎‚˜๎‚…๎‚‹ ๎‚๎‚— ๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ๎‡€
๎€‹๎‚˜ ๎‚๎‚— ๎‚‰๎‚—๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚ ๎‚˜๎‚“ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ @file ๎‚˜๎‚…๎‚‹ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰๎‚—๎‡€ ๎€‹๎‚’ ๎‚๎‚˜๎‚— ๎‚…๎‚†๎‚—๎‚‰๎‚’๎‚‡๎‚‰ ๎‚˜๎‚Œ๎‚‰
๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚’ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚—๎†ฝ ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎†ฝ ๎‚˜๎‚๎‚”๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚’๎‚ˆ ๎‚‰๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰
๎Šฑ๎‚๎‚‰ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ Doxygen ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ๎‡€
๎€‹๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚๎‚— ๎‚”๎‚…๎‚–๎‚˜ ๎‚“๎‚Š ๎‚… ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚‘๎‚…๎‚’๎‚ˆ @addtogroup <label> [title] ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€ ๎€–๎‚Œ๎‚๎‚—
๎‚—๎‚‰๎‚–๎‚š๎‚‰๎‚— ๎‚˜๎‚“ ๎‚‹๎‚–๎‚“๎‚™๎‚” ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚…๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚… ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚›๎‚๎‚˜๎‚Œ๎‚๎‚’ ๎‚… ๎‚‘๎‚“๎‚ˆ๎‚™๎‚๎‚‰ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‡‚
๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰๎‚ˆ๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚๎‚…๎‚†๎‚‰๎‚ ๎‚๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‹๎‚–๎‚“๎‚™๎‚” ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚…๎‚๎‚ ๎Šฑ๎‚๎‚‰๎‚— ๎‚†๎‚‰๎‚๎‚“๎‚’๎‚‹๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰
๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚˜๎‚๎‚˜๎‚๎‚‰ ๎‚๎‚— ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚…๎‚๎‡€ ๎€‹๎‚˜ ๎‚๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚‹๎‚–๎‚“๎‚™๎‚” ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
๎€–๎‚Œ๎‚‰ @addtogroup ๎‚‡๎‚“๎‚‘๎‚‘๎‚…๎‚’๎‚ˆ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚—๎‚™๎‚”๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ @{ ๎‚…๎‚’๎‚ˆ @} ๎‚˜๎‚…๎‚‹ ๎‚”๎‚…๎‚๎‚– ๎‚๎‚’ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚˜๎‚“ ๎‚ˆ๎‚‰๎‚๎‚๎‚‘๎‚๎‚˜
๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚†๎‚‰๎‚๎‚“๎‚’๎‚‹๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚‹๎‚–๎‚“๎‚™๎‚”๎‡€
E.5.3 Documentation of a structure
๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜ ๎‚”๎‚–๎‚‰๎‚‡๎‚‰๎‚ˆ๎‚๎‚’๎‚‹ ๎‚๎‚˜๎‚— ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€–๎‚Œ๎‚๎‚—
๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚“๎‚๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‡€ ๎€‡๎‚…๎‚‡๎‚Œ ๎Šฑ๎‚‰๎‚๎‚ˆ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ๎‡€
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 165
E.5.4 Documentation of an enumeration
๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚‰๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜ ๎‚”๎‚–๎‚‰๎‚‡๎‚‰๎‚ˆ๎‚๎‚’๎‚‹ ๎‚๎‚˜๎‚— ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’๎‡€
๎€–๎‚Œ๎‚๎‚— ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰ ๎‚๎‚’ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚Š๎‚–๎‚…๎‚‘๎‚‰๎‚›๎‚“๎‚–๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ๎‡€ ๎€‡๎‚…๎‚‡๎‚Œ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰
๎‚‰๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ๎‡€
E.5.5 Documentation of a global variable
๎€ƒ ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜ ๎‚”๎‚–๎‚‰๎‚‡๎‚‰๎‚ˆ๎‚๎‚’๎‚‹ ๎‚๎‚˜๎‚— ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€–๎‚Œ๎‚๎‚— ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜
๎‚‘๎‚™๎‚—๎‚˜ ๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚“๎‚๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎†ฝ ๎‚๎‚˜๎‚— ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚š๎‚…๎‚๎‚™๎‚‰๎†ฝ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚’๎‚ ๎‚๎‚’๎‚š๎‚…๎‚–๎‚๎‚…๎‚’๎‚˜๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰
๎‚–๎‚‰๎‚—๎‚”๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ๎‡€
E.5.6 Documentation of a function
๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚”๎‚–๎‚‰๎‚‡๎‚‰๎‚ˆ๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚–๎‚“๎‚˜๎‚“๎‚˜๎‚๎‚”๎‚‰ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰
๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‡€ ๎€–๎‚Œ๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‡๎‚“๎‚’๎‚—๎‚๎‚—๎‚˜๎‚— ๎‚“๎‚Š๎†ฟ
n
๎‚… ๎‚†๎‚–๎‚๎‚‰๎‚Š ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎†พ
n
๎‚… ๎‚ˆ๎‚‰๎‚˜๎‚…๎‚๎‚๎‚‰๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‰๎‚œ๎‚”๎‚๎‚…๎‚๎‚’๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚‰๎‚…๎‚˜๎‚™๎‚–๎‚‰ ๎‚“๎Šฎ๎‚‰๎‚–๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†พ
n
๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎†ฝ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚Š๎‚๎‚๎‚’๎‚‹ ๎‚›๎‚Œ๎‚‰๎‚˜๎‚Œ๎‚‰๎‚– ๎‚๎‚˜ ๎‚๎‚— ๎‚…๎‚’ ๎‚๎‚’๎‚”๎‚™๎‚˜๎†ฝ ๎‚“๎‚™๎‚˜๎‚”๎‚™๎‚˜ ๎‚“๎‚– ๎‚†๎‚“๎‚˜๎‚Œ ๎‚๎‚’๎‚”๎‚™๎‚˜ ๎‚…๎‚’๎‚ˆ
๎‚“๎‚™๎‚˜๎‚”๎‚™๎‚˜ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎†พ
n
๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡€ ๎€‹๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚™๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚‡๎‚…๎‚—๎‚‰๎‡ฆ๎‚—๎‡ง ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰
๎‚๎‚’๎‚ˆ๎‚๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ๎†ฝ ๎‚…๎‚๎‚“๎‚’๎‚‹ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚”๎‚–๎‚๎‚“๎‚–๎‚๎‚˜๎‚๎†พ
n
๎‚… ๎‚”๎‚–๎‚‰๎‡‚๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚๎‚Š ๎‚…๎‚’๎‚๎†ฝ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚‡๎‚…๎‚๎‚๎‚‰๎‚ˆ๎†พ
n
๎‚… ๎‚”๎‚“๎‚—๎‚˜๎‡‚๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚๎‚Š ๎‚…๎‚’๎‚๎†ฝ ๎‚…๎Šน๎‚‰๎‚– ๎‚‡๎‚…๎‚๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†พ
n ๎‚…๎‚’๎‚ ๎‚…๎‚ˆ๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎‚–๎‚‰๎‚‘๎‚…๎‚–๎‚๎‚— ๎‚“๎‚– ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚—๎‡€
Good example
๎€–๎‚Œ๎‚‰ ๎‚Š๎‚“๎‚๎‚๎‚“๎‚›๎‚๎‚’๎‚‹ ๎‚๎‚๎‚’๎‚‰๎‚— ๎‚—๎‚Œ๎‚“๎‚› ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚๎‚’๎‚๎‚‘๎‚™๎‚‘ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚“๎‚– ๎‚… ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‡€
#ifndef UTILS_LINKED_LIST_H
#define UTILS_LINKED_LIST_H
/*!
* @file linked_list .h
* @author DEV 1
*
* @brief Linked List
*
* Function declarations for the manipulation of linked list.
*
* @addtogroup utils Library Utils
* @{
*/
/*!
* @brief Enumeration of status codes
*
* Status codes to indicate the success or the failure of functions
*/
typedef enum status_tag {
STATUS_SUCCESS = 0, //!< success
166 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
STATUS_GENERIC_ERROR , //!< generic error
STATUS_MEMORY_ERROR , //!< memory allocation error
STATUS_INVALID_PARAM //!< invalid parameter
} status_t;
/*!
* @brief Element of the linked list
*/
typedef struct linked_list_element_tag
{
struct linked_list_element_tag * pNext; //!< next element
struct linked_list_element_tag * pPrevious ; //!< previous element
void* pData ; //!< data of the element
} linked_list_element_t ;
/*!
* @brief Double linked list
*
* Structure to define a double linked list . The type data of the list is void.
*/
typedef struct linked_list_tag
{
linked_list_element_t *pHead; //!< first element
linked_list_element_t *pTail; //!< last element
} linked_list_t;
/*!
* @brief New linked list
*
* Creation of a new linked list by allocating the memory for the structure and by
initializing the list.
* The new list is empty.
*
* @param[out] ppList is the new list
* @return # STATUS_SUCCESS the creation and the initialization are done with
success
* @return # STATUS_INVALID_PARAM if ppList is NULL or
* if (* ppList) != NULL
* @return # STATUS_MEMORY_ERROR fail of the memory allocation
* @pre ppList != NULL and (*ppList) == NULL
* @note the created list has to be deleted
* by calling # utils_delete_linked_list
*/
status_t utils_create_linked_list(linked_list_t **ppList);
/*!
* @brief Deletion of the list
*
* All the elements of the list are deleted and the used memory is freed.
* @warning The memory used by the data in the list is not freed..
*
* @param[in , out] ppList the list to delete.
* @return # STATUS_SUCCESS if the deletion of the list is a success
* @return # STATUS_INVALID_PARAM if ppList is NULL
* or if (* ppList ) is NULL
* @pre ppList != NULL and (*ppList) != NULL
* @post (* ppList) == NULL
*/
status_t utils_delete_linked_list(linked_list_t **ppList);
...
/*! @} */
#endif // UTILS_LINKED_LIST_H
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 167
Index
๎‡‚๎Š•๎†ฝ ๎ˆ–๎ˆ’
๎ˆ‡๎†ฝ ๎ˆ๎ˆ—
FAM๎†ฝ Flexible Array Member๎†ฝ ๎ˆ—๎ˆ
VLA๎†ฝ variable length array๎†ฝ ๎ˆ•๎ˆ•
bit-๎Šฑeld๎†ฝ ๎ˆ–๎ˆ˜
compound literals๎†ฝ ๎ˆ“๎ˆ–
dangling pointer๎†ฝ ๎ˆ–๎ˆ
debug๎†ฝ ๎ˆ’๎ˆ•
debug ๎‚‘๎‚“๎‚ˆ๎‚‰๎†ฝ ๎ˆ’๎ˆ•
release๎†ฝ ๎ˆ’๎ˆ•
release ๎‚‘๎‚“๎‚ˆ๎‚‰๎†ฝ ๎ˆ’๎ˆ•
use-a๎Šนer-free๎†ฝ ๎ˆ–๎ˆ
++๎†ฝ ๎ˆ๎ˆ‘๎ˆ‘
,๎†ฝ ๎ˆ๎ˆ‘๎ˆ
--๎†ฝ ๎ˆ๎ˆ‘๎ˆ‘
?:๎†ฝ ๎ˆ๎ˆ‘๎ˆ’
##๎†ฝ ๎ˆ๎ˆ—
#define๎†ฝ ๎ˆ“๎ˆ
#pragma๎†ฝ ๎ˆ’๎ˆ
#undef๎†ฝ ๎ˆ‘๎ˆ”
#๎†ฝ ๎ˆ๎ˆ—
_Bool๎†ฝ ๎ˆ—๎ˆ—
bool๎†ฝ ๎ˆ—๎ˆ—
complex๎†ฝ ๎ˆ˜๎ˆ•
const๎†ฝ ๎ˆ“๎ˆ๎†ฝ ๎ˆ๎ˆ๎ˆ
errno๎†ฝ ๎ˆ๎ˆ’๎ˆ’
float, double๎†ฝ ๎ˆ˜๎ˆ“
for๎†ฝ ๎ˆ๎ˆ๎ˆ
goto๎†ฝ ๎ˆ๎ˆ๎ˆ“๎†ฝ ๎ˆ๎ˆ๎ˆ”
inline๎†ฝ ๎ˆ๎ˆ๎ˆ‘
int๎†ฝ ๎ˆ”๎ˆ–
realloc๎†ฝ ๎ˆ๎ˆ‘๎ˆ•
restrict๎†ฝ ๎ˆ–๎ˆ
sizeof๎†ฝ ๎ˆ๎ˆ‘๎ˆ–
static๎†ฝ ๎ˆ๎ˆ“๎†ฝ ๎ˆ“๎ˆ“๎†ฝ ๎ˆ๎ˆ๎ˆ‘
switch-case๎†ฝ ๎ˆ˜๎ˆ—
typedef๎†ฝ ๎ˆ“๎ˆ•๎†ฝ ๎ˆ”๎ˆ—
volatile๎†ฝ ๎ˆ“๎ˆ”
/*๎†ฝ ๎ˆ๎ˆ“๎ˆ˜
//๎†ฝ ๎ˆ๎ˆ“๎ˆ˜
๎‚…๎‚๎‚๎‚…๎‚—๎†ฝ ๎ˆ–๎ˆ
๎‚…๎‚’๎‚…๎‚๎‚๎‚—๎‚๎‚—๎†ฝ ๎ˆ๎ˆ“๎ˆ“
๎‚…๎‚–๎‚–๎‚…๎‚๎†ฝ ๎ˆ•๎ˆ“
๎‚†๎‚“๎‚“๎‚๎‚‰๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎†ฝ ๎ˆ—๎ˆ•
๎€…๎ŠŠ๎ŠŠ๎†ฝ ๎ˆ๎ˆ“๎ˆ–๎†ฝ ๎ˆ๎ˆ”๎ˆ–
๎‚‡๎‚…๎‚’๎‚…๎‚–๎‚๎†ฝ ๎ˆ’๎ˆ“๎†ฝ ๎ˆ๎ˆ“๎ˆ˜
๎‚‡๎‚…๎‚—๎‚˜๎†ฝ ๎ˆ”๎ˆ–
๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎†ฝ ๎ˆ๎ˆ“๎ˆ˜
๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎ˆ‘๎ˆ–
๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚…๎‚๎†ฝ ๎ˆ˜๎ˆ–
๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎†ฝ ๎ˆ“๎ˆ
๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’ ๎‚ˆ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚…๎‚‹๎‚‰๎†ฝ ๎ˆ—
๎‚‡๎‚๎‚‡๎‚๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ๎‚๎‚˜๎‚๎†ฝ ๎ˆ๎ˆ“๎ˆ•
๎‚ˆ๎‚‰๎‚…๎‚ˆ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎ˆ๎ˆ“๎ˆ”
๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎ˆ๎ˆ๎ˆ–
๎‚ˆ๎‚™๎‚–๎‚‡๎‚๎‚—๎‚—๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎†ฝ ๎ˆ’๎ˆ
๎‚‰๎‚–๎‚–๎‚“๎‚–๎†ฝ ๎ˆ๎ˆ’๎ˆ’
๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎†ฝ ๎ˆ•๎ˆ”
๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎ˆ๎ˆ๎ˆ–
๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎ˆ๎ˆ๎ˆ–
๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚–๎‚“๎‚˜๎‚“๎‚˜๎‚๎‚”๎‚‰๎†ฝ ๎ˆ๎ˆ๎ˆ–
๎‚‹๎‚“๎‚“๎‚ˆ ๎‚”๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚‡๎‚‰๎†ฝ ๎ˆ–
๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎‡‚๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ๎†ฝ ๎ˆ’๎ˆ
๎‚๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎ˆ๎ˆ“๎ˆ“
๎‚Ž๎‚™๎‚‘๎‚”๎†ฝ ๎ˆ๎ˆ๎ˆ“
๎‚๎‚๎‚˜๎‚‰๎‚–๎‚…๎‚๎†ฝ ๎ˆ“๎ˆ
๎€Ž๎‚š๎‚…๎‚๎‚™๎‚‰๎†ฝ ๎ˆ•๎ˆ”
๎‚‘๎‚ณ๎‚‘๎‚“๎‚๎‚–๎‚‰๎†ฝ ๎ˆ๎ˆ‘๎ˆ”
๎‚“๎‚”๎‚ณ๎‚–๎‚…๎‚˜๎‚‰๎‚™๎‚– ๎‚ˆ๎‚‰ stringi๎Šฑcation๎†ฝ ๎ˆ๎ˆ—
๎‚“๎‚”๎‚ณ๎‚–๎‚…๎‚˜๎‚‰๎‚™๎‚– ๎‚ˆ๎‚‰ ๎‚‡๎‚“๎‚’๎‚‡๎‚…๎‚˜๎‚ณ๎‚’๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎ˆ๎ˆ—
๎‚”๎‚…๎‚ˆ๎‚ˆ๎‚๎‚’๎‚‹๎†ฝ ๎ˆ–๎ˆ—
๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚”๎‚…๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚†๎‚ ๎‚‡๎‚“๎‚”๎‚๎†ฝ ๎ˆ๎ˆ๎ˆ˜
๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚”๎‚…๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚†๎‚ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎†ฝ ๎ˆ๎ˆ๎ˆ˜
๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚”๎‚…๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚†๎‚ ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰๎†ฝ ๎ˆ๎ˆ๎ˆ˜
๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚”๎‚…๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚†๎‚ ๎‚š๎‚…๎‚๎‚™๎‚‰๎†ฝ ๎ˆ๎ˆ๎ˆ˜
๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎†ฝ ๎ˆ•๎ˆ“
๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡๎†ฝ ๎ˆ–๎ˆ“
๎‚”๎‚–๎‚“๎‚“๎‚Š๎‚–๎‚‰๎‚…๎‚ˆ๎‚๎‚’๎‚‹๎†ฝ ๎ˆ๎ˆ“๎ˆ“
๎ˆ๎ˆ•๎ˆ—
๎‚”๎‚–๎‚ณ๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚‰๎‚™๎‚–๎†ฝ ๎ˆ๎ˆ
๎‚–๎‚‰๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚ˆ๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎ˆ•
๎‚–๎‚™๎‚๎‚‰๎†ฝ ๎ˆ•
๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎†ฝ ๎ˆ๎ˆ“๎ˆ
๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎†ฝ ๎ˆ–๎ˆ–
๎‚˜๎‚–๎‚๎‚‹๎‚–๎‚…๎‚”๎‚Œ๎†ฝ ๎ˆ‘๎ˆ”
๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎†ฝ ๎ˆ”๎ˆ–
๎‚˜๎‚๎‚”๎‚‰๎‚ˆ๎‚‰๎‚Š๎†ฝ ๎ˆ“๎ˆ•
๎‚™๎‚’๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚™๎‚–๎†ฝ ๎ˆ˜
๎‚™๎‚’๎‚๎‚“๎‚’๎†ฝ ๎ˆ–๎ˆ–๎†ฝ ๎ˆ—๎ˆ
๎‚™๎‚’๎‚–๎‚‰๎‚…๎‚‡๎‚Œ๎‚…๎‚†๎‚๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰๎†ฝ ๎ˆ๎ˆ“๎ˆ”
๎‚™๎‚’๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚†๎‚‰๎‚Œ๎‚…๎‚š๎‚๎‚“๎‚–๎†ฝ ๎ˆ˜
๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚—๎†ฝ ๎ˆ’๎ˆ—
๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎ˆ’๎ˆ—
๎‚š๎‚…๎‚–๎‚๎‚…๎‚ˆ๎‚๎‚‡ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎ˆ๎ˆ๎ˆ˜
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 169
List of rules, recommendations and good
practices
1 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€ƒ๎‚”๎‚”๎‚๎‚๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‡๎‚๎‚‰๎‚…๎‚– ๎‚…๎‚’๎‚ˆ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚‡๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ—
2 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€‘๎‚’๎‚๎‚ ๎€… ๎‚‡๎‚“๎‚ˆ๎‚๎‚’๎‚‹ ๎‚๎‚’ ๎‚…๎‚‡๎‚‡๎‚“๎‚–๎‚ˆ๎‚…๎‚’๎‚‡๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚— ๎‚…๎‚™๎‚˜๎‚Œ๎‚“๎‚–๎‚๎‚—๎‚‰๎‚ˆ ๎ˆ˜
3 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€Ž๎‚๎‚‘๎‚๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚Ž๎‚™๎‚—๎‚˜๎‚๎‚Š๎‚ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’๎‚— ๎‚๎‚’ ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎ˆ๎ˆ
4 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€‘๎‚’๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰๎‚ˆ ๎ˆ๎ˆ
5 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€—๎‚—๎‚‰ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰ ๎‚‹๎‚™๎‚…๎‚–๎‚ˆ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚— ๎‚Š๎‚“๎‚– ๎‚… ๎Šฑ๎‚๎‚‰ ๎ˆ๎ˆ‘
6 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€Š๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚‹๎‚–๎‚“๎‚™๎‚”๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚‰๎‚‹๎‚๎‚’๎‚’๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚๎‚‰ ๎ˆ๎ˆ‘
7 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€•๎‚๎‚—๎‚˜๎‚‰๎‚‘ ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’๎‚— ๎‚…๎‚–๎‚‰ ๎‚‘๎‚…๎‚ˆ๎‚‰ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚™๎‚—๎‚‰๎‚– ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰
๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’๎‚— ๎ˆ๎ˆ‘
8 ๎€‰๎€‘๎€‘๎€† ๎€’๎€”๎€ƒ๎€…๎€–๎€‹๎€…๎€‡ ๎‡ก ๎€—๎‚—๎‚‰ ๎‚…๎‚๎‚”๎‚Œ๎‚…๎‚†๎‚‰๎‚˜๎‚๎‚‡๎‚…๎‚ ๎‚“๎‚–๎‚ˆ๎‚‰๎‚– ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚Š ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰๎‚— ๎ˆ๎ˆ‘
9 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰ ๎‚… ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰ ๎‚๎‚’ ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎Šฑ๎‚๎‚‰ ๎ˆ๎ˆ“
10 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€ˆ๎‚๎‚๎‚‰ ๎‚”๎‚…๎‚˜๎‚Œ๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚”๎‚“๎‚–๎‚˜๎‚…๎‚†๎‚๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎ˆ๎ˆ”
11 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€–๎‚Œ๎‚‰ ๎‚’๎‚…๎‚‘๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚Œ๎‚‰๎‚…๎‚ˆ๎‚‰๎‚– ๎Šฑ๎‚๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚‡๎‚‰๎‚–๎‚˜๎‚…๎‚๎‚’ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚– ๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰๎‚— ๎‚“๎‚Š
๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚–๎‚— ๎ˆ๎ˆ•
12 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€’๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚†๎‚๎‚“๎‚‡๎‚๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚’ ๎ˆ๎ˆ•
13 ๎€‰๎€‘๎€‘๎€† ๎€’๎€”๎€ƒ๎€…๎€–๎€‹๎€…๎€‡ ๎‡ก ๎€†๎‚“๎‚™๎‚†๎‚๎‚‰ ๎‚’๎‚‰๎‚‹๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚†๎‚๎‚“๎‚‡๎‚ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚—
๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚…๎‚š๎‚“๎‚๎‚ˆ๎‚‰๎‚ˆ ๎ˆ๎ˆ•
14 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚†๎‚๎‚“๎‚‡๎‚ ๎‚๎‚’ ๎‚… ๎‚—๎‚๎‚’๎‚‹๎‚๎‚‰ ๎Šฑ๎‚๎‚‰ ๎ˆ๎ˆ–
15 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€’๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚
๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ ๎ˆ๎ˆ–
16 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚‘๎‚“๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚“๎‚’๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎ˆ‡ ๎‚…๎‚’๎‚ˆ ๎ˆ‡๎ˆ‡ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰
๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎ˆ๎ˆ—
17 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€—๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎ˆ‡ ๎‚…๎‚’๎‚ˆ
๎ˆ‡๎ˆ‡ ๎ˆ๎ˆ—
18 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€๎‚…๎‚‡๎‚–๎‚“๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‡๎‚…๎‚๎‚๎‚ ๎‚’๎‚…๎‚‘๎‚‰๎‚ˆ ๎ˆ‘๎ˆ
19 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‰๎‚’๎‚ˆ ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚—๎‚‰๎‚‘๎‚๎‚‡๎‚“๎‚๎‚“๎‚’ ๎ˆ‘๎ˆ
20 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€—๎‚—๎‚‰ static inline ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚๎‚’๎‚—๎‚˜๎‚‰๎‚…๎‚ˆ ๎‚“๎‚Š ๎‚‘๎‚™๎‚๎‚˜๎‚๎‡‚๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚— ๎ˆ‘๎ˆ‘
21 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€–๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚”๎‚๎‚…๎‚‡๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚… ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‰๎‚–๎‡‚๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚–๎‚‰๎‚…๎‚˜๎‚‰ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ‘๎ˆ‘
22 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€๎‚…๎‚‡๎‚–๎‚“๎‚— ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’๎‚๎‚’๎‚‹ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚… do { ๎‡€๎‡€๎‡€ } while(0) ๎‚๎‚“๎‚“๎‚” ๎‚Š๎‚“๎‚–
๎‚˜๎‚Œ๎‚‰๎‚๎‚– ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎ˆ‘๎ˆ’
23 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€๎‚…๎‚’๎‚ˆ๎‚…๎‚˜๎‚“๎‚–๎‚ ๎‚”๎‚…๎‚–๎‚‰๎‚’๎‚˜๎‚Œ๎‚‰๎‚—๎‚‰๎‚— ๎‚…๎‚–๎‚“๎‚™๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚“๎‚ˆ๎‚ ๎‚“๎‚Š ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎ˆ‘๎ˆ“
24 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€ƒ๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚‡๎‚…๎‚–๎‚–๎‚๎‚๎‚’๎‚‹ ๎‚“๎‚™๎‚˜ ๎‚…๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰
๎‚…๎‚š๎‚“๎‚๎‚ˆ๎‚‰๎‚ˆ ๎ˆ‘๎ˆ“
25 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€ƒ๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚๎‚’ ๎‚… ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚—๎‚๎‚ˆ๎‚‰ ๎‚‰๎Šฎ๎‚‰๎‚‡๎‚˜๎‚— ๎ˆ‘๎ˆ“
26 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚— ๎‚๎‚’ ๎‚‘๎‚…๎‚‡๎‚–๎‚“ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎ˆ‘๎ˆ“
27 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€–๎‚Œ๎‚‰ #undef ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎ˆ‘๎ˆ”
28 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚–๎‚๎‚‹๎‚–๎‚…๎‚”๎‚Œ๎‚— ๎ˆ‘๎ˆ•
29 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€•๎‚™๎‚‡๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚š๎‚‰ ๎‚•๎‚™๎‚‰๎‚—๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚…๎‚–๎‚๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎ˆ‘๎ˆ•
170 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
30 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€’๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚‰๎‚๎‚ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ‘๎ˆ–
31 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€๎‚…๎‚—๎‚˜๎‚‰๎‚– ๎‚…๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚”๎‚‰๎‚–๎‚Š๎‚“๎‚–๎‚‘๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚๎‚’๎‚๎‚‰๎‚– ๎ˆ‘๎ˆ—
32 ๎€‰๎€‘๎€‘๎€† ๎€’๎€”๎€ƒ๎€…๎€–๎€‹๎€…๎€‡ ๎‡ก ๎€๎‚…๎‚๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚†๎‚™๎‚๎‚๎‚ˆ ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰ ๎ˆ‘๎ˆ—
33 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€…๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚…๎‚’๎‚ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚’๎‚“๎‚– ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹ ๎‚›๎‚Œ๎‚๎‚๎‚‰ ๎‚‰๎‚’๎‚…๎‚†๎‚๎‚๎‚’๎‚‹ ๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’
๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ‘๎ˆ˜
34 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€‡๎‚’๎‚…๎‚†๎‚๎‚‰ ๎‚… ๎‚–๎‚‰๎‚…๎‚—๎‚“๎‚’๎‚…๎‚†๎‚๎‚ ๎‚Œ๎‚๎‚‹๎‚Œ ๎‚“๎‚”๎‚˜๎‚๎‚‘๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚‰๎‚š๎‚‰๎‚ ๎ˆ‘๎ˆ˜
35 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€—๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‚‰๎‚—๎‚˜ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ‘๎ˆ˜
36 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€๎‚…๎‚๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚ ๎‚Š๎‚‰๎‚…๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚“๎Šฎ๎‚‰๎‚–๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚–๎‚— ๎ˆ’๎ˆ
37 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€‡๎‚’๎‚…๎‚†๎‚๎‚‰ ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚Š๎‚“๎‚‡๎‚™๎‚— ๎‚“๎‚’ ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜๎‚๎‚’๎‚‹ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚๎‚˜๎‚ ๎‚†๎‚™๎‚‹๎‚— ๎‚…๎‚’๎‚ˆ ๎‚ˆ๎‚‰๎‚…๎‚ ๎‚›๎‚๎‚˜๎‚Œ ๎‚…๎‚’๎‚ ๎‚–๎‚‰๎‚”๎‚“๎‚–๎‚˜๎‚‰๎‚ˆ
๎‚๎‚—๎‚—๎‚™๎‚‰ ๎ˆ’๎ˆ
38 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€‡๎‚’๎‚…๎‚†๎‚๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚Œ๎‚…๎‚–๎‚ˆ๎‚‰๎‚’๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚™๎‚’๎‚—๎‚…๎‚Š๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ’๎ˆ
39 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€‡๎‚’๎‚…๎‚†๎‚๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚›๎‚…๎‚–๎‚’๎‚๎‚’๎‚‹๎‚— ๎‚–๎‚‰๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚™๎‚’๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎ˆ’๎ˆ‘
40 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€‡๎‚’๎‚…๎‚†๎‚๎‚‰ ๎‚Š๎‚“๎‚–๎‚‡๎‚‰๎‚ˆ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚…๎‚™๎‚˜๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎ˆ’๎ˆ‘
41 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€‡๎‚’๎‚…๎‚†๎‚๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚– ๎‚“๎‚”๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚๎‚๎‚“๎‚› ๎‚Š๎‚“๎‚– ๎‚ˆ๎‚‰๎‚˜๎‚‰๎‚‡๎‚˜๎‚๎‚’๎‚‹ ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚–
๎‚“๎‚š๎‚‰๎‚–๎Šท๎‚“๎‚›๎‚— ๎ˆ’๎ˆ’
42 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚…๎‚†๎‚๎‚‰ ๎‚—๎‚˜๎‚…๎‚‡๎‚ ๎ˆ’๎ˆ“
43 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€‡๎‚’๎‚…๎‚†๎‚๎‚‰ ๎‚—๎‚˜๎‚…๎‚‡๎‚ ๎‚‡๎‚…๎‚’๎‚…๎‚–๎‚๎‚‰๎‚— ๎ˆ’๎ˆ“
44 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€—๎‚—๎‚‰ ๎‚”๎‚‰๎‚–๎‡‚๎‚˜๎‚Œ๎‚–๎‚‰๎‚…๎‚ˆ ๎‚‡๎‚…๎‚’๎‚…๎‚–๎‚๎‚‰๎‚— ๎ˆ’๎ˆ“
45 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€’๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚‰ ๎‚”๎‚“๎‚—๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚๎‚’๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚…๎‚†๎‚๎‚‰๎‚— ๎ˆ’๎ˆ”
46 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€—๎‚—๎‚‰ relro ๎‚‘๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚Š ๎‚๎‚๎‚’๎‚๎‚‰๎‚–๎‚— ๎ˆ’๎ˆ”
47 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ lazy binding ๎ˆ’๎ˆ”
48 ๎€‰๎€‘๎€‘๎€† ๎€’๎€”๎€ƒ๎€…๎€–๎€‹๎€…๎€‡ ๎‡ก ๎€‡๎‚’๎‚—๎‚™๎‚–๎‚‰ ๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚๎‚†๎‚๎‚‰ ๎‚†๎‚™๎‚๎‚๎‚ˆ๎‚— ๎ˆ’๎ˆ•
49 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€ƒ๎‚๎‚ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’๎‡‚๎‚–๎‚‰๎‚…๎‚ˆ๎‚ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚๎‚‰๎‚ˆ ๎‚๎‚’ release ๎‚‘๎‚“๎‚ˆ๎‚‰ ๎ˆ’๎ˆ•
50 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€’๎‚…๎‚ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚…๎‚ ๎‚…๎‚˜๎‚˜๎‚‰๎‚’๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚“ debug ๎‚…๎‚’๎‚ˆ release ๎‚‘๎‚“๎‚ˆ๎‚‰๎‚— ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚†๎‚™๎‚๎‚๎‚ˆ๎‚๎‚’๎‚‹
๎‚… ๎‚”๎‚–๎‚“๎‚Ž๎‚‰๎‚‡๎‚˜ ๎ˆ’๎ˆ–
51 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€‘๎‚’๎‚๎‚ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚Š ๎‚—๎‚๎‚‘๎‚”๎‚๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚˜๎‚๎‚”๎‚‰
๎‚…๎‚–๎‚‰ ๎‚…๎‚™๎‚˜๎‚Œ๎‚“๎‚–๎‚๎‚—๎‚‰๎‚ˆ ๎ˆ’๎ˆ—
52 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‘๎‚…๎‚๎‚‰ ๎‚‘๎‚™๎‚๎‚˜๎‚๎‚”๎‚๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚…๎‚’ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎ˆ’๎ˆ—
53 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€‰๎‚–๎‚“๎‚™๎‚” ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚‰๎‚‹๎‚๎‚’๎‚’๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚๎‚“๎‚‡๎‚ ๎‚๎‚’ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ
๎‚˜๎‚Œ๎‚‰๎‚ ๎‚…๎‚–๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎ˆ’๎ˆ˜
54 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚Œ๎‚…๎‚–๎‚ˆ๎‡‚๎‚‡๎‚“๎‚ˆ๎‚‰๎‚ˆ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎ˆ“๎ˆ
55 ๎€‰๎€‘๎€‘๎€† ๎€’๎€”๎€ƒ๎€…๎€–๎€‹๎€…๎€‡ ๎‡ก ๎€…๎‚‰๎‚’๎‚˜๎‚–๎‚…๎‚๎‚๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚…๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚‰๎‚‹๎‚๎‚’๎‚’๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚๎‚‰ ๎ˆ“๎ˆ
56 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚๎‚’ ๎‚™๎‚”๎‚”๎‚‰๎‚– ๎‚‡๎‚…๎‚—๎‚‰ ๎ˆ“๎ˆ
57 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€…๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚ˆ๎‚“ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚•๎‚™๎‚๎‚–๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚๎‚’๎‚‹ ๎‚…๎‚–๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰
#define
๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‡‚
๎‚‡๎‚‰๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰ ๎ˆ“๎ˆ
58 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€…๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚–๎‚‰๎‚•๎‚™๎‚๎‚–๎‚๎‚’๎‚‹ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚๎‚’๎‚‹ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ
const ๎ˆ“๎ˆ
59 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€…๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚—๎‚™๎Šฒ๎‚œ ๎‚ˆ๎‚‰๎‚”๎‚‰๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎ˆ“๎ˆ
60 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€–๎‚Œ๎‚‰ ๎‚—๎‚๎‚ž๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚—๎‚™๎Šฒ๎‚‡๎‚๎‚‰๎‚’๎‚˜ ๎‚˜๎‚“
๎‚‡๎‚“๎‚’๎‚˜๎‚…๎‚๎‚’ ๎‚๎‚˜ ๎ˆ“๎ˆ‘
61 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€’๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜ ๎‚“๎‚‡๎‚˜๎‚…๎‚ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎ˆ“๎ˆ‘
62 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€Ž๎‚๎‚‘๎‚๎‚˜ ๎‚‹๎‚๎‚“๎‚†๎‚…๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚˜๎‚“ ๎‚›๎‚Œ๎‚…๎‚˜ ๎‚๎‚— ๎‚—๎‚˜๎‚–๎‚๎‚‡๎‚˜๎‚๎‚ ๎‚’๎‚‰๎‚‡๎‚‰๎‚—๎‚—๎‚…๎‚–๎‚ ๎ˆ“๎ˆ’
63 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€•๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ static ๎‚—๎‚”๎‚‰๎‚‡๎‚๎Šฑ๎‚‰๎‚– ๎‚Š๎‚“๎‚– ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ“๎ˆ”
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 171
64 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€‘๎‚’๎‚๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‡๎‚…๎‚’ ๎‚†๎‚‰ ๎‚‘๎‚“๎‚ˆ๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚“๎‚™๎‚˜๎‚—๎‚๎‚ˆ๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ
volatile ๎ˆ“๎ˆ”
65 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€‘๎‚’๎‚๎‚ volatile๎‡‚๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚ˆ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚‡๎‚…๎‚’ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— volatile ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎ˆ“๎ˆ”
66 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€๎‚“ ๎‚˜๎‚๎‚”๎‚‰ ๎‚“๎‚‘๎‚๎‚—๎‚—๎‚๎‚“๎‚’ ๎‚๎‚— ๎‚…๎‚‡๎‚‡๎‚‰๎‚”๎‚˜๎‚‰๎‚ˆ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚๎‚’๎‚‹ ๎‚… ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎ˆ“๎ˆ•
67 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€Ž๎‚๎‚‘๎‚๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š compound literals ๎ˆ“๎ˆ–
68 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‘๎‚๎‚œ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚…๎‚’๎‚ˆ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚…๎‚’๎‚˜๎‚— ๎‚๎‚’ ๎‚…๎‚’ ๎‚‰๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎ˆ“๎ˆ˜
69 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚…๎‚’๎‚“๎‚’๎‚๎‚‘๎‚“๎‚™๎‚— ๎‚‰๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ“๎ˆ˜
70 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€˜๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰๎‚ˆ ๎‚…๎‚˜ ๎‚“๎‚– ๎‚๎‚‘๎‚‘๎‚‰๎‚ˆ๎‚๎‚…๎‚˜๎‚‰๎‚๎‚ ๎‚…๎Šน๎‚‰๎‚– ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎ˆ”๎ˆ
71 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€—๎‚—๎‚‰ ๎‚“๎‚’๎‚๎‚ ๎‚“๎‚’๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚๎‚’๎‚˜๎‚…๎‚œ ๎‚Š๎‚“๎‚– ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎ˆ”๎ˆ
72 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€•๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚—๎‚”๎‚‰๎‚‡๎‚๎‚Š๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚…๎‚˜๎‚๎‚“๎‚’
๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚‰๎‚…๎‚‡๎‚Œ ๎Šฑ๎‚‰๎‚๎‚ˆ๎‡š๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰๎‚ˆ ๎ˆ”๎ˆ‘
73 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€‡๎‚š๎‚‰๎‚–๎‚ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎ˆ”๎ˆ’
74 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€—๎‚—๎‚‰ ๎‚—๎‚‰๎‚”๎‚…๎‚–๎‚…๎‚˜๎‚‰ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚Š๎‚“๎‚– ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚…๎‚’๎‚ˆ ๎‚’๎‚“๎‚’๎‡‚๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚… ๎ˆ”๎ˆ“
75 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€—๎‚—๎‚‰ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚†๎‚๎‚‰๎‚— ๎‚Š๎‚“๎‚– ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚…๎‚–๎‚‰ ๎‚”๎‚–๎‚“๎‚˜๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚๎‚’ ๎‚‡๎‚“๎‚’๎Šฑ๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎‚…๎‚๎‚๎‚˜๎‚ ๎‚…๎‚’๎‚ˆ๎‡š๎‚“๎‚–
๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚–๎‚๎‚˜๎‚ ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚“๎‚’๎‚‰๎‚— ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚™๎‚’๎‚”๎‚–๎‚“๎‚˜๎‚‰๎‚‡๎‚˜๎‚‰๎‚ˆ ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚… ๎ˆ”๎ˆ”
76 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€๎‚‰๎‚š๎‚‰๎‚– ๎‚Œ๎‚…๎‚–๎‚ˆ๎‡‚๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚… ๎ˆ”๎ˆ”
77 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€‘๎‚’๎‚๎‚ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚—๎‚๎‚ž๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚—๎‚๎‚‹๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎ˆ”๎ˆ–
78 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€‘๎‚’๎‚๎‚ signed char ๎‚…๎‚’๎‚ˆ unsigned char ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚‰ ๎‚’๎‚™๎‚‘๎‚‰๎‚–๎‚๎‚‡
๎‚š๎‚…๎‚๎‚™๎‚‰๎‚— ๎ˆ”๎ˆ–
79 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰ ๎‚˜๎‚๎‚”๎‚‰ ๎‚…๎‚๎‚๎‚…๎‚—๎‚‰๎‚— ๎ˆ”๎ˆ—
80 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚‰๎‚˜๎‚…๎‚๎‚๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚”๎‚–๎‚‰๎‚‡๎‚๎‚—๎‚‰ ๎‚™๎‚’๎‚ˆ๎‚‰๎‚–๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚–๎‚™๎‚๎‚‰๎‚— ๎ˆ•๎ˆ
81 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€‡๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚…๎‚’๎‚ˆ ๎‚™๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎ˆ•๎ˆ
82 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚˜๎‚๎‚”๎‚‰ ๎‚‡๎‚“๎‚’๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’ ๎‚“๎‚’ ๎‚˜๎‚๎‚”๎‚‰๎‚— ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚ˆ ๎‚ˆ๎‚๎Šฎ๎‚‰๎‚–๎‚‰๎‚’๎‚˜๎‚๎‚ ๎ˆ•๎ˆ‘
83 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€ƒ๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚“๎‚Š ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚›๎‚๎‚๎‚ ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚†๎‚‰ ๎‚†๎‚ ๎‚ˆ๎‚‰๎‚—๎‚๎‚‹๎‚’๎‚…๎‚˜๎‚๎‚’๎‚‹ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚–๎‚—๎‚˜ ๎‚…๎‚˜๎‚˜๎‚–๎‚๎‚†๎‚™๎‚˜๎‚‰
๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚…๎‚’๎‚ˆ ๎‚…๎‚— ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚‰๎‚‡๎‚“๎‚’๎‚ˆ ๎‚…๎‚˜๎‚˜๎‚–๎‚๎‚†๎‚™๎‚˜๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚ˆ๎‚‰๎‚œ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚‡๎‚“๎‚’๎‚‡๎‚‰๎‚–๎‚’๎‚‰๎‚ˆ ๎ˆ•๎ˆ•
84 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€ƒ๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚˜๎‚“ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚๎‚’ ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚™๎‚—๎‚๎‚’๎‚‹ ๎‚—๎‚•๎‚™๎‚…๎‚–๎‚‰ ๎‚†๎‚–๎‚…๎‚‡๎‚๎‚‰๎‚˜๎‚— ๎ˆ•๎ˆ•
85 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎€˜๎€Ž๎€ƒ๎‚— ๎ˆ•๎ˆ–
86 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚…๎‚’ ๎‚๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚—๎‚๎‚ž๎‚‰ ๎‚Š๎‚“๎‚– ๎‚…๎‚–๎‚–๎‚…๎‚๎‚— ๎ˆ•๎ˆ–
87 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€—๎‚—๎‚‰ ๎‚™๎‚’๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚‰๎‚–๎‚— ๎‚Š๎‚“๎‚– ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚—๎‚๎‚ž๎‚‰๎‚— ๎ˆ•๎ˆ—
88 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚…๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚‰๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚‡๎‚Œ๎‚‰๎‚‡๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚๎‚ˆ๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚๎‚’๎‚ˆ๎‚‰๎‚œ ๎ˆ•๎ˆ—
89 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€ƒ NULL ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚‘๎‚™๎‚—๎‚˜ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰๎‚ˆ ๎ˆ•๎ˆ˜
90 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€ƒ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚˜๎‚“ NULL ๎‚…๎Šน๎‚‰๎‚– ๎‚ˆ๎‚‰๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎ˆ–๎ˆ
91 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ restrict ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚•๎‚™๎‚…๎‚๎‚๎Šฑ๎‚‰๎‚– ๎ˆ–๎ˆ‘
92 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€–๎‚Œ๎‚‰ ๎‚’๎‚™๎‚‘๎‚†๎‚‰๎‚– ๎‚“๎‚Š ๎‚๎‚‰๎‚š๎‚‰๎‚๎‚— ๎‚“๎‚Š ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚๎‚’๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚๎‚๎‚‘๎‚๎‚˜๎‚‰๎‚ˆ ๎‚˜๎‚“
๎‚˜๎‚›๎‚“ ๎ˆ–๎ˆ’
93 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€‰๎‚๎‚š๎‚‰ ๎‚”๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚’๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– -> ๎ˆ–๎ˆ“
94 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€‘๎‚’๎‚๎‚ ๎‚๎‚’๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚๎‚’๎‚‹ ๎‚“๎‚– ๎‚ˆ๎‚‰๎‚‡๎‚–๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚๎‚’๎‚‹ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚๎‚— ๎‚…๎‚™๎‚˜๎‚Œ๎‚“๎‚–๎‚๎‚—๎‚‰๎‚ˆ ๎ˆ–๎ˆ“
95 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€๎‚“ ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚“๎‚’ void* ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚๎‚— ๎‚…๎‚™๎‚˜๎‚Œ๎‚“๎‚–๎‚๎‚—๎‚‰๎‚ˆ ๎ˆ–๎ˆ“
96 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€…๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚๎‚๎‚‰๎‚ˆ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎‚…๎‚–๎‚๎‚˜๎‚Œ๎‚‘๎‚‰๎‚˜๎‚๎‚‡ ๎‚“๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚๎‚— ๎ˆ–๎ˆ”
97 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€•๎‚™๎‚†๎‚˜๎‚–๎‚…๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚”๎‚…๎‚–๎‚๎‚—๎‚“๎‚’ ๎‚†๎‚‰๎‚˜๎‚›๎‚‰๎‚‰๎‚’ ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚“๎‚’๎‚๎‚ ๎ˆ–๎ˆ”
98 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€ƒ ๎Šฑ๎‚œ๎‚‰๎‚ˆ ๎‚…๎‚ˆ๎‚ˆ๎‚–๎‚‰๎‚—๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‰๎‚ˆ ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚ ๎‚˜๎‚“ ๎‚… ๎‚”๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚– ๎ˆ–๎ˆ”
99 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€ƒ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎‚˜๎‚“ ๎‚‹๎‚–๎‚“๎‚™๎‚” ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚–๎‚‰๎‚”๎‚–๎‚‰๎‚—๎‚‰๎‚’๎‚˜๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚…๎‚‘๎‚‰ ๎‚‰๎‚’๎‚˜๎‚๎‚˜๎‚ ๎ˆ–๎ˆ–
172 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
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 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€•๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚ˆ๎‚‰๎‚Š๎‚…๎‚™๎‚๎‚˜ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚๎‚’ switch ๎ˆ˜๎ˆ—
129 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€—๎‚—๎‚‰ ๎‚“๎‚Š break ๎‚๎‚’ ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚‡๎‚…๎‚—๎‚‰ ๎‚“๎‚Š switch ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎ˆ˜๎ˆ˜
130 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€๎‚“ ๎‚’๎‚‰๎‚—๎‚˜๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚‡๎‚“๎‚’๎‚˜๎‚–๎‚“๎‚ ๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰ ๎‚๎‚’ ๎‚… switch-case ๎ˆ˜๎ˆ˜
131 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚๎‚’๎‚—๎‚‰๎‚–๎‚˜ ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎Šฑ๎‚–๎‚—๎‚˜ ๎‚๎‚…๎‚†๎‚‰๎‚ ๎‚“๎‚Š ๎‚… switch-case ๎ˆ˜๎ˆ˜
132 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€…๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š for ๎‚๎‚“๎‚“๎‚”๎‚— ๎ˆ๎ˆ๎ˆ
133 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€…๎‚Œ๎‚…๎‚’๎‚‹๎‚‰ ๎‚˜๎‚“ ๎‚… ๎‚‡๎‚“๎‚™๎‚’๎‚˜๎‚‰๎‚– ๎‚“๎‚Š ๎‚… for ๎‚๎‚“๎‚“๎‚” ๎‚Š๎‚“๎‚–๎‚†๎‚๎‚ˆ๎‚ˆ๎‚‰๎‚’ ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚“๎‚ˆ๎‚ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚“๎‚“๎‚” ๎ˆ๎ˆ๎ˆ‘
134 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€๎‚“ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚†๎‚…๎‚‡๎‚๎‚›๎‚…๎‚–๎‚ˆ goto ๎ˆ๎ˆ๎ˆ“
135 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€Ž๎‚๎‚‘๎‚๎‚˜๎‚‰๎‚ˆ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚Š๎‚“๎‚–๎‚›๎‚…๎‚–๎‚ˆ goto ๎ˆ๎ˆ๎ˆ”
136 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€ƒ๎‚’๎‚ ๎‡ฆ๎‚’๎‚“๎‚’๎‡‚๎‚—๎‚˜๎‚…๎‚˜๎‚๎‚‡๎‡ง ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰๎‚ˆ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’๎‡š๎‚”๎‚–๎‚“๎‚˜๎‚“๎‚˜๎‚๎‚”๎‚‰ ๎ˆ๎ˆ๎ˆ–
137 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€–๎‚Œ๎‚‰ ๎‚”๎‚–๎‚“๎‚˜๎‚“๎‚˜๎‚๎‚”๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚“๎‚’๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚’๎‚˜ ๎‚›๎‚๎‚˜๎‚Œ ๎‚๎‚˜๎‚— ๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚๎‚˜๎‚๎‚“๎‚’ ๎ˆ๎ˆ๎ˆ–
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 173
138 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€‡๎‚š๎‚‰๎‚–๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚…๎‚’ ๎‚‰๎‚œ๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚˜๎‚๎‚”๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚๎‚๎‚—๎‚˜ ๎‚…๎‚—๎‚—๎‚“๎‚‡๎‚๎‚…๎‚˜๎‚‰๎‚ˆ
๎‚›๎‚๎‚˜๎‚Œ ๎‚๎‚˜ ๎ˆ๎ˆ๎ˆ—
139 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€†๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ๎ˆ๎ˆ˜
140 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€•๎‚”๎‚‰๎‚‡๎‚๎‚Š๎‚ ๎‚‡๎‚…๎‚๎‚ ๎‚‡๎‚“๎‚’๎‚ˆ๎‚๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚Š๎‚“๎‚– ๎‚‰๎‚…๎‚‡๎‚Œ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ๎ˆ๎ˆ˜
141 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€–๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚๎‚ˆ๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚…๎‚๎‚ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚—๎‚๎‚—๎‚˜๎‚‰๎‚‘๎‚…๎‚˜๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚†๎‚‰ ๎‚•๎‚™๎‚‰๎‚—๎‚˜๎‚๎‚“๎‚’๎‚‰๎‚ˆ ๎ˆ๎ˆ๎ˆ
142 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€’๎‚“๎‚๎‚’๎‚˜๎‚‰๎‚–๎‡‚๎‚˜๎‚๎‚”๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚˜๎‚“ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚๎‚— ๎‚’๎‚“๎‚˜ ๎‚˜๎‚“ ๎‚†๎‚‰ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰๎‚ˆ
๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚…๎‚— const ๎ˆ๎ˆ๎ˆ
143 ๎€”๎€—๎€Ž๎€‡ ๎‡ก inline ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚ˆ๎‚‰๎‚‡๎‚๎‚…๎‚–๎‚‰๎‚ˆ ๎‚…๎‚— static ๎ˆ๎ˆ๎ˆ‘
144 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚–๎‚‰๎‚ˆ๎‚‰๎Šฑ๎‚’๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚– ๎‚‘๎‚…๎‚‡๎‚–๎‚“๎‚— ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚“๎‚– ๎‚…๎‚’๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎ˆ๎ˆ๎ˆ’
145 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€–๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚…๎‚๎‚›๎‚…๎‚๎‚— ๎‚†๎‚‰ ๎‚˜๎‚‰๎‚—๎‚˜๎‚‰๎‚ˆ ๎ˆ๎ˆ๎ˆ“
146 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€‹๎‚‘๎‚”๎‚๎‚๎‚‡๎‚๎‚˜ ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚’๎‚“๎‚’๎‡‚void ๎‚˜๎‚๎‚”๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ๎ˆ๎ˆ”
147 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€•๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚”๎‚…๎‚—๎‚—๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰ ๎‚˜๎‚“ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ๎ˆ๎ˆ•
148 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€’๎‚…๎‚—๎‚—๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚…๎‚’ ๎‚…๎‚–๎‚–๎‚…๎‚ ๎‚…๎‚— ๎‚… ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚– ๎‚Š๎‚“๎‚– ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ๎ˆ๎ˆ–
149 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€๎‚…๎‚’๎‚ˆ๎‚…๎‚˜๎‚“๎‚–๎‚ ๎‚™๎‚—๎‚‰ ๎‚๎‚’ ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚…๎‚๎‚ ๎‚๎‚˜๎‚— ๎‚”๎‚…๎‚–๎‚…๎‚‘๎‚‰๎‚˜๎‚‰๎‚–๎‚— ๎ˆ๎ˆ๎ˆ—
150 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚…๎‚๎‚ ๎‚š๎‚…๎‚–๎‚๎‚…๎‚ˆ๎‚๎‚‡ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚›๎‚๎‚˜๎‚Œ NULL ๎‚…๎‚— ๎‚…๎‚’ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜ ๎ˆ๎ˆ๎ˆ˜
151 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€—๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚‘๎‚‘๎‚… ๎‚”๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ ๎‚Š๎‚“๎‚– ๎‚—๎‚˜๎‚…๎‚˜๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰๎‚— ๎ˆ๎ˆ‘๎ˆ
152 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€–๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎Šฑ๎‚œ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ++ ๎‚…๎‚’๎‚ˆ -- ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚’๎‚“๎‚˜ ๎‚†๎‚‰ ๎‚™๎‚—๎‚‰๎‚ˆ ๎ˆ๎ˆ‘๎ˆ‘
153 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€๎‚“ ๎‚‡๎‚“๎‚‘๎‚†๎‚๎‚’๎‚‰๎‚ˆ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚”๎‚“๎‚—๎‚˜๎Šฑ๎‚œ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎ˆ๎ˆ‘๎ˆ‘
154 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€ƒ๎‚š๎‚“๎‚๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚‡๎‚“๎‚‘๎‚†๎‚๎‚’๎‚‰๎‚ˆ ๎‚…๎‚—๎‚—๎‚๎‚‹๎‚’๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚–๎‚— ๎ˆ๎ˆ‘๎ˆ‘
155 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€๎‚“ ๎‚’๎‚‰๎‚—๎‚˜๎‚‰๎‚ˆ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚–๎‚ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ?: ๎ˆ๎ˆ‘๎ˆ“
156 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€…๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚‡๎‚“๎‚’๎‚—๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎‚›๎‚๎‚˜๎‚Œ ๎‚˜๎‚Œ๎‚‰ ๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚–๎‚ ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ?: ๎ˆ๎ˆ‘๎ˆ“
157 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰ ๎‚—๎‚™๎Šฒ๎‚‡๎‚๎‚‰๎‚’๎‚˜ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚—๎‚”๎‚…๎‚‡๎‚‰ ๎‚Š๎‚“๎‚– ๎‚˜๎‚Œ๎‚‰ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚“๎‚†๎‚Ž๎‚‰๎‚‡๎‚˜ ๎ˆ๎ˆ‘๎ˆ”
158 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€ˆ๎‚–๎‚‰๎‚‰ ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡๎‚…๎‚๎‚๎‚๎‡‚๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚— ๎‚—๎‚“๎‚“๎‚’ ๎‚…๎‚— ๎‚”๎‚“๎‚—๎‚—๎‚๎‚†๎‚๎‚‰ ๎ˆ๎ˆ‘๎ˆ”
159 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€•๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚–๎‚‰๎‚…๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚–๎‚‰๎‚—๎‚‰๎‚˜ ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚†๎‚‰๎‚๎‚’๎‚‹ ๎‚Š๎‚–๎‚‰๎‚‰๎‚ˆ ๎ˆ๎ˆ‘๎ˆ”
160 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚Š๎‚–๎‚‰๎‚‰ ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚’๎‚“๎‚˜ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚‰๎‚ˆ ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡๎‚…๎‚๎‚๎‚ ๎ˆ๎ˆ‘๎ˆ•
161 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚ˆ๎‚๎‚’๎‚…๎‚‘๎‚๎‚‡ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚š๎‚๎‚… realloc ๎ˆ๎ˆ‘๎ˆ•
162 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€…๎‚“๎‚–๎‚–๎‚‰๎‚‡๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ sizeof ๎‚“๎‚”๎‚‰๎‚–๎‚…๎‚˜๎‚“๎‚– ๎ˆ๎ˆ‘๎ˆ—
163 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€๎‚…๎‚’๎‚ˆ๎‚…๎‚˜๎‚“๎‚–๎‚ ๎‚š๎‚‰๎‚–๎‚๎Šฑ๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚™๎‚‡๎‚‡๎‚‰๎‚—๎‚— ๎‚“๎‚Š ๎‚… ๎‚‘๎‚‰๎‚‘๎‚“๎‚–๎‚ ๎‚…๎‚๎‚๎‚“๎‚‡๎‚…๎‚˜๎‚๎‚“๎‚’ ๎ˆ๎ˆ‘๎ˆ˜
164 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€•๎‚‰๎‚’๎‚—๎‚๎‚˜๎‚๎‚š๎‚‰ ๎‚ˆ๎‚…๎‚˜๎‚… ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚๎‚—๎‚“๎‚๎‚…๎‚˜๎‚‰๎‚ˆ ๎ˆ๎ˆ’๎ˆ
165 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€‹๎‚’๎‚๎‚˜๎‚๎‚…๎‚๎‚๎‚—๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚š๎‚๎‚‰๎‚› ๎‚˜๎‚Œ๎‚‰ ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎‚“๎‚Š errno ๎‚†๎‚‰๎‚Š๎‚“๎‚–๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚…๎Šน๎‚‰๎‚– ๎‚…๎‚’๎‚ ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚… ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ
๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚‡๎‚Œ๎‚…๎‚’๎‚‹๎‚‰๎‚— ๎‚๎‚˜๎‚— ๎‚š๎‚…๎‚๎‚™๎‚‰ ๎ˆ๎ˆ’๎ˆ’
166 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€ƒ๎‚๎‚ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚‰๎‚ˆ ๎ˆ๎ˆ’๎ˆ“
167 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€‡๎‚–๎‚–๎‚“๎‚– ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎ˆ๎ˆ’๎ˆ”
168 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€•๎‚˜๎‚–๎‚™๎‚‡๎‚˜๎‚™๎‚–๎‚๎‚’๎‚‹ ๎‚“๎‚Š ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚— ๎ˆ๎ˆ’๎ˆ•
169 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€”๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚Š ๎‚… ๎€… ๎‚”๎‚–๎‚“๎‚‹๎‚–๎‚…๎‚‘ ๎‚…๎‚‡๎‚‡๎‚“๎‚–๎‚ˆ๎‚๎‚’๎‚‹ ๎‚˜๎‚“ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚‰๎‚—๎‚™๎‚๎‚˜ ๎‚“๎‚Š ๎‚๎‚˜๎‚— ๎‚‰๎‚œ๎‚‰๎‚‡๎‚™๎‚˜๎‚๎‚“๎‚’ ๎ˆ๎ˆ’๎ˆ•
170 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€‰๎‚๎‚š๎‚‰ ๎‚”๎‚–๎‚‰๎‚Š๎‚‰๎‚–๎‚‰๎‚’๎‚‡๎‚‰ ๎‚˜๎‚“ ๎‚‰๎‚–๎‚–๎‚“๎‚– ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’๎‚— ๎‚š๎‚๎‚… ๎‚–๎‚‰๎‚˜๎‚™๎‚–๎‚’ ๎‚‡๎‚“๎‚ˆ๎‚‰๎‚— ๎‚๎‚’ ๎‚˜๎‚Œ๎‚‰ ๎‚‘๎‚…๎‚๎‚’ ๎‚Š๎‚™๎‚’๎‚‡๎‡‚
๎‚˜๎‚๎‚“๎‚’ ๎ˆ๎ˆ’๎ˆ–
171 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ abort() ๎‚“๎‚– _Exit() ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ๎ˆ’๎ˆ–
172 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€Ž๎‚๎‚‘๎‚๎‚˜ ๎‚‡๎‚…๎‚๎‚๎‚— ๎‚˜๎‚“ exit() ๎ˆ๎ˆ’๎ˆ–
173 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ setjmp() ๎‚…๎‚’๎‚ˆ longjump() ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ๎ˆ’๎ˆ—
174 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ setjmp.h ๎‚…๎‚’๎‚ˆ stdarg.h ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‚‰๎‚— ๎ˆ๎ˆ“๎ˆ
174 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
175 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€Ž๎‚๎‚‘๎‚๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚™๎‚—๎‚‰ ๎‚“๎‚Š ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚๎‚‰๎‚— ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚๎‚’๎‚‹ ๎Šท๎‚“๎‚…๎‚˜๎‚๎‚’๎‚‹๎‡‚๎‚”๎‚“๎‚๎‚’๎‚˜ ๎‚’๎‚™๎‚‘๎‡‚
๎‚†๎‚‰๎‚–๎‚— ๎ˆ๎ˆ“๎ˆ
176 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— atoi(), atol(), atof() ๎‚…๎‚’๎‚ˆ atoll() ๎‚Š๎‚–๎‚“๎‚‘ ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚
stdlib.h ๎ˆ๎ˆ“๎ˆ
177 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ rand() ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎ˆ๎ˆ“๎ˆ
178 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€—๎‚—๎‚‰ ๎‚˜๎‚Œ๎‚‰ ๎‡Œ๎‚‘๎‚“๎‚–๎‚‰ ๎‚—๎‚‰๎‚‡๎‚™๎‚–๎‚‰ ๎‡ ๎‚š๎‚‰๎‚–๎‚—๎‚๎‚“๎‚’๎‚— ๎‚Š๎‚“๎‚– ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎ˆ๎ˆ“๎ˆ‘
179 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚“๎‚†๎‚—๎‚“๎‚๎‚‰๎‚˜๎‚‰ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚“๎‚– ๎‚˜๎‚Œ๎‚“๎‚—๎‚‰ ๎‚›๎‚Œ๎‚๎‚‡๎‚Œ ๎‚†๎‚‰๎‚‡๎‚“๎‚‘๎‚‰ ๎‚“๎‚†๎‚—๎‚“๎‚๎‚‰๎‚˜๎‚‰ ๎‚๎‚’ ๎‚—๎‚™๎‚†๎‚—๎‚‰๎‡‚
๎‚•๎‚™๎‚‰๎‚’๎‚˜ ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‚— ๎ˆ๎ˆ“๎ˆ‘
180 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎‚๎‚๎‚†๎‚–๎‚…๎‚–๎‚ ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚˜๎‚Œ๎‚…๎‚˜ ๎‚Œ๎‚…๎‚’๎‚ˆ๎‚๎‚‰ ๎‚†๎‚™๎Šฎ๎‚‰๎‚–๎‚— ๎‚›๎‚๎‚˜๎‚Œ๎‚“๎‚™๎‚˜ ๎‚˜๎‚…๎‚๎‚๎‚’๎‚‹ ๎‚˜๎‚Œ๎‚‰ ๎‚†๎‚™๎Šฎ๎‚‰๎‚– ๎‚—๎‚๎‚ž๎‚‰ ๎‚…๎‚—
๎‚…๎‚’ ๎‚…๎‚–๎‚‹๎‚™๎‚‘๎‚‰๎‚’๎‚˜ ๎ˆ๎ˆ“๎ˆ‘
181 ๎€‰๎€‘๎€‘๎€† ๎€’๎€”๎€ƒ๎€…๎€–๎€‹๎€…๎€‡ ๎‡ก ๎€ƒ๎‚๎‚ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚†๎‚‰ ๎‚”๎‚–๎‚“๎‚“๎‚Š๎‚–๎‚‰๎‚…๎‚ˆ ๎ˆ๎ˆ“๎ˆ“
182 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€‹๎‚’๎‚ˆ๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚๎‚“๎‚’๎‚‹ ๎‚‰๎‚œ๎‚”๎‚–๎‚‰๎‚—๎‚—๎‚๎‚“๎‚’๎‚— ๎ˆ๎ˆ“๎ˆ“
183 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€‹๎‚ˆ๎‚‰๎‚’๎‚˜๎‚๎‚Š๎‚ ๎‚…๎‚’๎‚ˆ ๎‚–๎‚‰๎‚‘๎‚“๎‚š๎‚‰ ๎‚…๎‚’๎‚ ๎‚ˆ๎‚‰๎‚…๎‚ˆ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎ˆ๎ˆ“๎ˆ”
184 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€–๎‚Œ๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚‘๎‚™๎‚—๎‚˜ ๎‚Œ๎‚…๎‚š๎‚‰ ๎‚’๎‚“ ๎‚™๎‚’๎‚–๎‚‰๎‚…๎‚‡๎‚Œ๎‚…๎‚†๎‚๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚“๎‚˜๎‚Œ๎‚‰๎‚– ๎‚˜๎‚Œ๎‚…๎‚’ ๎‚ˆ๎‚‰๎‚Š๎‚‰๎‚’๎‚—๎‚๎‚š๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚…๎‚’๎‚ˆ ๎‚๎‚’๎‚˜๎‚‰๎‚–๎‚Š๎‚…๎‚‡๎‚‰
๎‚‡๎‚“๎‚ˆ๎‚‰ ๎ˆ๎ˆ“๎ˆ”
185 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€–๎‚“๎‚“๎‚๎‡‚๎‚†๎‚…๎‚—๎‚‰๎‚ˆ ๎‚‰๎‚š๎‚…๎‚๎‚™๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚—๎‚“๎‚™๎‚–๎‚‡๎‚‰ ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚˜๎‚“ ๎‚๎‚๎‚‘๎‚๎‚˜ ๎‚˜๎‚Œ๎‚‰ ๎‚–๎‚๎‚—๎‚ ๎‚“๎‚Š ๎‚‰๎‚œ๎‚‰๎‡‚
๎‚‡๎‚™๎‚˜๎‚๎‚“๎‚’ ๎‚‰๎‚–๎‚–๎‚“๎‚–๎‚— ๎ˆ๎ˆ“๎ˆ•
186 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€Ž๎‚๎‚‘๎‚๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚‡๎‚๎‚‡๎‚๎‚“๎‚‘๎‚…๎‚˜๎‚๎‚‡ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ๎‚๎‚˜๎‚ ๎ˆ๎ˆ“๎ˆ–
187 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€Ž๎‚๎‚‘๎‚๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚Š ๎‚˜๎‚Œ๎‚‰ ๎‚๎‚‰๎‚’๎‚‹๎‚˜๎‚Œ ๎‚…๎‚’๎‚ˆ ๎‚‡๎‚“๎‚‘๎‚”๎‚๎‚‰๎‚œ๎‚๎‚˜๎‚ ๎‚“๎‚Š ๎‚… ๎‚Š๎‚™๎‚’๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ๎ˆ“๎ˆ–
188 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€†๎‚“ ๎‚’๎‚“๎‚˜ ๎‚™๎‚—๎‚‰ ๎€…๎ŠŠ๎ŠŠ ๎‚๎‚‰๎‚๎‚›๎‚“๎‚–๎‚ˆ๎‚— ๎ˆ๎ˆ“๎ˆ–
189 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€’๎‚–๎‚“๎‚Œ๎‚๎‚†๎‚๎‚˜๎‚‰๎‚ˆ ๎‚‡๎‚Œ๎‚…๎‚–๎‚…๎‚‡๎‚˜๎‚‰๎‚– ๎‚—๎‚‰๎‚•๎‚™๎‚‰๎‚’๎‚‡๎‚‰๎‚— ๎‚๎‚’ ๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎ˆ๎ˆ“๎ˆ˜
190 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€๎‚…๎‚’๎‚™๎‚…๎‚๎‚๎‚ ๎‚๎‚‘๎‚”๎‚๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚… ๎‡Œ๎‚‡๎‚…๎‚’๎‚…๎‚–๎‚ ๎‡ ๎‚‘๎‚‰๎‚‡๎‚Œ๎‚…๎‚’๎‚๎‚—๎‚‘ ๎‚›๎‚Œ๎‚‰๎‚’ ๎‚’๎‚“๎‚˜ ๎‚…๎‚๎‚–๎‚‰๎‚…๎‚ˆ๎‚ ๎‚—๎‚™๎‚”๎‚”๎‚“๎‚–๎‚˜๎‚‰๎‚ˆ ๎‚†๎‚ ๎‚˜๎‚Œ๎‚‰
๎‚˜๎‚“๎‚“๎‚๎‚‡๎‚Œ๎‚…๎‚๎‚’ ๎ˆ๎ˆ”๎ˆ
191 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€๎‚“ ๎‚ˆ๎‚‰๎‚š๎‚‰๎‚๎‚“๎‚”๎‚‘๎‚‰๎‚’๎‚˜ ๎‚…๎‚—๎‚—๎‚‰๎‚–๎‚˜๎‚๎‚“๎‚’ ๎‚“๎‚’ ๎‚… ๎‚‡๎‚“๎‚ˆ๎‚‰ ๎‚๎‚’ ๎‚”๎‚–๎‚“๎‚ˆ๎‚™๎‚‡๎‚˜๎‚๎‚“๎‚’ ๎ˆ๎ˆ”๎ˆ
192 ๎€”๎€‡๎€…๎€‘๎€๎€๎€‡๎€๎€†๎€ƒ๎€–๎€‹๎€‘๎€ ๎‡ก ๎€๎‚…๎‚’๎‚…๎‚‹๎‚‰๎‚‘๎‚‰๎‚’๎‚˜ ๎‚“๎‚Š ๎‚๎‚’๎‚˜๎‚‰๎‚‹๎‚–๎‚๎‚˜๎‚ ๎‚…๎‚—๎‚—๎‚‰๎‚–๎‚˜๎‚๎‚“๎‚’๎‚— ๎‚—๎‚Œ๎‚“๎‚™๎‚๎‚ˆ ๎‚๎‚’๎‚‡๎‚๎‚™๎‚ˆ๎‚‰ ๎‚‰๎‚‘๎‚‰๎‚–๎‚‹๎‚‰๎‚’๎‚‡๎‚
๎‚ˆ๎‚…๎‚˜๎‚… ๎‚ˆ๎‚‰๎‚๎‚‰๎‚˜๎‚๎‚“๎‚’ ๎ˆ๎ˆ”๎ˆ
193 ๎€”๎€—๎€Ž๎€‡ ๎‡ก ๎€ƒ๎‚๎‚ ๎‚’๎‚“๎‚’๎‡‚๎‚‰๎‚‘๎‚”๎‚˜๎‚ ๎Šฑ๎‚๎‚‰๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚‰๎‚’๎‚ˆ ๎‚›๎‚๎‚˜๎‚Œ ๎‚… ๎‚๎‚๎‚’๎‚‰ ๎‚†๎‚–๎‚‰๎‚…๎‚ ๎‚…๎‚’๎‚ˆ ๎‚˜๎‚Œ๎‚‰ ๎‚”๎‚–๎‚‰๎‚”๎‚–๎‚“๎‚‡๎‚‰๎‚—๎‚—๎‚“๎‚– ๎‚ˆ๎‚๎‚–๎‚‰๎‚‡๎‚˜๎‚๎‚š๎‚‰๎‚— ๎‚…๎‚’๎‚ˆ
๎‚‡๎‚“๎‚‘๎‚‘๎‚‰๎‚’๎‚˜๎‚— ๎‚‘๎‚™๎‚—๎‚˜ ๎‚†๎‚‰ ๎‚‡๎‚๎‚“๎‚—๎‚‰๎‚ˆ ๎ˆ๎ˆ”๎ˆ
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 175
Bibliography
๎‡ฌ๎Šท๎‚“๎‚…๎‚˜๎‡ญ IEEE Standard for Floating-Point Arithmetic๎‡€
๎€•๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎†ฝ ๎€‹๎€‡๎€‡๎€‡๎‡€
๎‡ฌ๎€ƒ๎‚’๎‚—๎‚๎€…๎ˆ˜๎ˆ๎‡ญ ISO/IEC 9899:1990, Programming Languages - C๎‡€
๎€•๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎†ฝ ๎€‹๎‚’๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎€‘๎‚–๎‚‹๎‚…๎‚’๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚“๎‚– ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€ƒ๎‚’๎‚—๎‚๎€…๎ˆ˜๎ˆ˜๎‡ญ ISO/IEC 9899:1999, Programming Languages - C๎‡€
๎€•๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎†ฝ ๎€‹๎‚’๎‚˜๎‚‰๎‚–๎‚’๎‚…๎‚˜๎‚๎‚“๎‚’๎‚…๎‚ ๎€‘๎‚–๎‚‹๎‚…๎‚’๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’ ๎‚Š๎‚“๎‚– ๎‚—๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎‚๎‚ž๎‚…๎‚˜๎‚๎‚“๎‚’๎‡€
๎‡ฌ๎€…๎‚‰๎‚–๎‚˜๎‡ญ SEI CERT C Coding Standard๎‡€
๎€•๎‚˜๎‚…๎‚’๎‚ˆ๎‚…๎‚–๎‚ˆ๎†ฝ ๎€…๎‚…๎‚–๎‚’๎‚‰๎‚‹๎‚๎‚‰ ๎€๎‚‰๎‚๎‚๎‚“๎‚’ ๎€—๎‚’๎‚๎‚š๎‚‰๎‚–๎‚—๎‚๎‚˜๎‚๎‡€
๎‡ฌ๎€…๎‚๎‚…๎‚’๎‚‹๎€”๎‚‰๎‚Š๎‡ญ CLANGโ€™S Documentation๎‡€
๎€’๎‚™๎‚†๎‚๎‚๎‚‡ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚Œ๎‚˜๎‚˜๎‚”๎‚—๎†ฟ๎‡š๎‡š๎‚‡๎‚๎‚…๎‚’๎‚‹๎‡€๎‚๎‚๎‚š๎‚‘๎‡€๎‚“๎‚–๎‚‹๎‡š๎‚ˆ๎‚“๎‚‡๎‚—๎‡š๎‡€
๎‡ฌ๎€…๎‚›๎‚‰๎‡ญ CWE Common Weakness Enumeration๎‡€
๎€–๎‚‰๎‚‡๎‚Œ๎‚’๎‚๎‚‡๎‚…๎‚ ๎‚–๎‚‰๎‚”๎‚“๎‚–๎‚˜๎†ฝ ๎€๎€‹๎€–๎€”๎€‡๎‡€
๎‡ฌ๎€‡๎€–๎€ƒ๎€Ž๎€ƒ๎€„๎‡ญ Licence ouverte / Open Licence v2.0๎‡€
๎€’๎‚…๎‚‹๎‚‰ ๎‚›๎‚‰๎‚†๎†ฝ ๎€๎‚๎‚—๎‚—๎‚๎‚“๎‚’ ๎€‡๎‚˜๎‚…๎‚๎‚…๎‚†๎†ฝ ๎‚…๎‚š๎‚–๎‚๎‚ ๎ˆ‘๎ˆ๎ˆ๎ˆ–๎‡€
https://www.etalab.gouv.fr/licence-ouverte-open-licence๎‡€
๎‡ฌ๎€‰๎‚‡๎‚‡๎€”๎‚‰๎‚Š๎‡ญ GCC: Reference Documentation๎‡€
๎€’๎‚™๎‚†๎‚๎‚๎‚‡ ๎‚ˆ๎‚“๎‚‡๎‚™๎‚‘๎‚‰๎‚’๎‚˜๎‚…๎‚˜๎‚๎‚“๎‚’๎†ฝ ๎‚Œ๎‚˜๎‚˜๎‚”๎†ฟ๎‡š๎‡š๎‚›๎‚›๎‚›๎‡€๎‚‹๎‚’๎‚™๎‡€๎‚“๎‚–๎‚‹๎‡š๎‚—๎‚“๎Šน๎‚›๎‚…๎‚–๎‚‰๎‡š๎‚‹๎‚‡๎‚‡๎‡š๎‚“๎‚’๎‚๎‚๎‚’๎‚‰๎‚ˆ๎‚“๎‚‡๎‚—๎‡€
๎‡ฌ๎€‹๎‚—๎‚“๎€•๎‚‰๎‚‡๎‚™๎‡ญ ISO/IEC TS 17961 Information Technology - Programming languages, their envrionments
and system so๎Šนware interfaces - C Secure Coding Rules๎‡€
๎€–๎‚‰๎‚‡๎‚Œ๎‚’๎‚๎‚‡๎‚…๎‚ ๎‚–๎‚‰๎‚”๎‚“๎‚–๎‚˜๎†ฝ ๎€•๎‚›๎‚๎‚˜๎‚ž๎‚‰๎‚–๎‚๎‚…๎‚’๎‚ˆ๎†ฝ ๎€‰๎‚‰๎‚’๎‚ฒ๎‚š๎‚‰๎‡€
๎‡ฌ๎€๎‚๎‚—๎‚–๎‚…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡ญ MISRA-C:2012 Guidelines for the use of the C language in critical systems๎‡€
๎€‰๎‚™๎‚๎‚ˆ๎‚‰๎‚๎‚๎‚’๎‚‰๎‚—๎†ฝ ๎‚Œ๎‚˜๎‚˜๎‚”๎‚—๎†ฟ๎‡š๎‡š๎‚›๎‚›๎‚›๎‡€๎‚‘๎‚๎‚—๎‚–๎‚…๎‡€๎‚“๎‚–๎‚‹๎‡€๎‚™๎‚๎‡š๎€๎€‹๎€•๎€”๎€ƒ๎€Š๎‚“๎‚‘๎‚‰๎‡š๎€๎€‹๎€•๎€”๎€ƒ๎€…๎ˆ‘๎ˆ๎ˆ๎ˆ‘๎‡€
176 ๎‡ž RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT
RULES FOR SECURE C LANGUAGE SOFTWARE DEVELOPMENT ๎‡ž 177
Version 1.4 - 24/03/2022 - ANSSI-PA-073
Licence ouverte / Open Licence (ร‰talab - v2.0)
AGENCE NATIONALE DE LA Sร‰CURITร‰ DES SYSTรˆMES D'INFORMATION
ANSSI - 51, boulevard de La Tour-Maubourg, 75700 PARIS 07 SP
www.ssi.gouv.fr / [email protected]