å®éãããã¯æŠåšã§ããã°ã¢ã»ã³ãã©ãŒã®ããã«èŠããŸãããCã§ã¯ãéåžžã«æ³šæããå¿ èŠããããŸã
翻蚳è ããïŒ
ãã®åºçç©ã¯ãå¿çèšäºããHow to C in 2016ããšããããã¹ãã«ç¿»èš³ãããã®ã§ãã åŸè ã®ç¿»èš³ã¯ãéææ¥ã«ç§ã«ãã£ãŠå ¬éãã ãããã€ãã®å Žæã§ãã³ãã¥ããã£ããã®æ··ååå¿ãåŒãèµ·ãããŸããã Habrã®ãã¬ãŒã ã¯ãŒã¯å ã§æ¢ã«åé¡ã®è°è«ãç¶æããããã®ãã®ãçãããžã®ãã³ãã¯ã CodeRushãŠãŒã¶ãŒããäžããããŸããã
以åã«ã ãProgramming C in 2016ããšããèšäºããªã³ã©ã€ã³ã§å ¬éãããå€ãã®æçšãªãã³ããæ²èŒãããŸããããæ®å¿µãªããããŸãè¯ãã¢ã€ãã¢ã§ã¯ãããŸããã§ããã ãã®ãããé¢é£ããç¹ã«ã€ããŠã³ã¡ã³ãããããšã«ããŸããã ç§ãæ°ããè³æãæºåããŠããéã誰ããCã§ä»äºãããã®ã¯è²¬ä»»ããããã°ã©ããŒã ãã§ãããä»ã®èšèªã¯æ¢åã®ã¹ãã«ãæ¹åããæ©äŒããã£ãšããç¡è²¬ä»»ã§ãªããã°ãªããªãããšã«æ°ä»ããŸããã ãã®åéã®å°é家ã®ç§å¯ãç解ããŸãããã
ãããã¬ãŒã䜿çšãã
ãã€ã³ã1ã¯ããããç¡èŠããŸãããç¡é§ã«ãªããŸãããå·ç段éã§ã³ãŒãã®åè¡ã«å¯ŸããŠã«ãŒãã«ã¬ãã«ã®ãããã¬ãŒãèµ·åããããšã§ãã ãã®ããŒã«ã®å¯èœæ§ãç¹ã«è€éãªåé¡ã解決ããããã ãã«äœ¿çšãããšãééããç¯ãããšã«ãªããŸãã
ã€ãŸããVisual StudioãXcodeãEclipseãªã©ã®IDEã䜿çšãããšããããšã§ãã ãã®å ŽåãïŒãããã°æ©èœãªãã§ïŒãšãã£ã¿ãŒã®ã¿ã§äœæ¥ããå Žåãããªãã¯è²§åŒ±ãªä»äºãããŠããŸãã éåžžã«å€ãã®äººããããã°æ©èœãæããªããšãã£ã¿ãŒã§ã³ãŒããæžãããããã®ãã¥ã¢ã³ã¹ã«èšåããŸãã ãããŠç§ãäŸå€ã§ã¯ãããŸããã
ããã¯ããã¹ãŠã®èšèªãç¹ã«Cã§ããã°ã©ãã³ã°ããå Žåã«éèŠã§ãã ã¡ã¢ãªãç ŽæããŠããå Žåã¯ããšã©ãŒãæ€åºããããã«ã¡ã¢ãªã®æ§é ãšå 容ããã³ãããå¿ èŠããããŸãã Xãå¥åŠãª37653ãåºåããã®ã¯ãªãã§ããïŒ printfïŒïŒã¹ã¿ã€ã«ã®ãããã°ã³ãã³ãã§ã¯ç¶æ³ãæ確ã«ãªããŸããããã¹ã¿ãã¯äžã®hexdumpãŠãŒãã£ãªãã£ã®ããŒã¿ãèŠãã°ãç¹å®ã®éã®æ å ±ãã©ã®ããã«æžã蟌ãŸãããã確å®ã«ç解ã§ããŸãã
ã³ãŒãã®ãããã°ãå¿ããªãã§ãã ãã
Cã¯ã¡ã¢ãªä¿è·ãæäŸããªããããã³ãŒãã®ãã®éšåãç Žæããéšåã«é¢é£ããŠããªãå Žåã§ããããå Žæã§çºçãããšã©ãŒãå¥ã®å Žæã§çºçããå ŽåããããŸãã ããããã³ãŒãã®åé¡ã®ããã»ã¯ã·ã§ã³ããããã°ããããšãéåžžã«é£ããçç±ã§ãã ãã®ãããªå Žåãå€ãã®ããã°ã©ããŒã髪ãåŒãè£ãããä¿®æ£ã§ããªãããšå«ã³ãååã«å©ããæ±ããŸãã
åãçæãèžãŸãªãã§ãã ããã ãã®ãããªã解決äžå¯èœãªãã¿ã¹ã¯ã«äœåºŠãçŽé¢ãããšãããè¯ãã³ãŒããæžãæ¹æ³ãåŠã³ãŸãã ããã¯ããã°ãè¿ éã«æ€åºããèªå·±ãã¹ãã³ãŒãã§ãããŸãã¯ãæžã蟌ã¿ããã»ã¹äžã«ãå¢çç·ã®ã±ãŒã¹ãã«ããŒãããããŒã¿ãããã¯å šäœã®å¹æçãªæ€èšŒãŠãŒãã£ãªãã£ã䜿çšãããŸãã
ã³ãŒããç©æ¥µçã«ä¿è·ãã
ãããŒãžã£ãŒãcatchïŒ...ïŒïŒC ++ã§ïŒãããã°ã©ã ã®ã¯ã©ãã·ã¥ãé¿ããããã«ã©ãã«ã§ã眮ãããšã«ãããããžã§ã¯ãã«ããŸããŸåãçµã¿ãŸããã äŸå€ãã¡ã¢ãªç Žæã®ã±ãŒã¹ã§ããä»®è£ ããŠãã¹ã¯ãããããã°ã©ã ã¯æ©èœãç¶ããŸããã éçºè ã¯ãã³ãŒãã®è匱æ§ãæžãããããã«èŠããŸããã 圌ãã¯ãé²åŸ¡çãªããã°ã©ãã³ã°ã¹ã¿ã€ã«ãåªãããœãªã¥ãŒã·ã§ã³ã§ãããšèããŸããã
ããããããã¯ä¿è·ã§ã¯ãªããæãã§ãã åŸã§æ€åºããã®ãããé£ãããšã©ãŒãé ãããžãã¯ã¯ã©ãã«ãããŸããïŒ
ç°ãªãæ¹æ³ã§è¡ããŸãã æ»æã®ååãã€ãŸãæçæéã§æ¬ é¥ãç¹å®ããã³ãŒããå¿ èŠã§ãã
ãã®ã¢ãããŒããçŸå®ã«ããæ¹æ³ã®1ã€ã¯assertïŒïŒã§ã-äºéã®ãã¹ãã®åæãããã«ããããã¹ãŠãæ£ããè¡ãããšãã§ããŸãã ãã®é¢æ°ã¯ãã¡ã¢ãªã«å®³ãäžããåã«ãã°ãæ€åºããŸãã ç§ã¯çå£ã§ãïŒCã®ç®ç«ããªããšã©ãŒããããã°ããã«ã¯ãã¯ã©ãã·ã¥ãåŒãèµ·ããå¯èœæ§ã®ããå Žæã«assertïŒïŒãæ¿å ¥ããŸãïŒææ¡ãããã³ãã³ãã«å€¢äžã«ãªããªãã§ãã ããïŒã
ã³ãŒãã«ã€ããŠè©±ããŠããå Žåããäºåã«æ»æãããããã®æè¯ã®æ¹æ³ã¯ãåäœãã¹ãã䜿çšããããšã§ãã çåãããå Žåã¯ããã«ããããŸããªãã©ã¡ãŒã¿ãŒããã§ãã¯ããåäœãã¹ããäœæããŠãã ããã Cã§äœæ¥ããŠãããšãããã¹ãŠãæåã«èšç»ã©ããã«é²ãã ãšãã«æ··ä¹±ããããããã®åŸå¥åŠãªããšãèµ·ãããšããæèŠããããŸãããã®ãããªå Žåãé©åãªãã¹ããçšæããŠããã®ãè¯ãããšã§ãã
ã³ãŒãã¯å質ã§ãªããã°ãªããŸãã
ãã®ç¹ã«é¢ããŠãåäœãã¹ããååž°ãã¹ããããã«ã¯ãã¡ãžã³ã°ã®ãããªãã®ãåŸã ã«æšæºã«ãªãã€ã€ãããŸãã ãªãŒãã³ãœãŒã¹ãããžã§ã¯ããããå Žåã¯ãå®æ§åæãå®æœããããã«make testãªãã·ã§ã³ãèŠæ±ããã ãã§ãã ããã«ãããé«ãã«ãã¬ããžã§åäœãã¹ãã³ãŒããå®è¡ã§ããŸãã ãã®ã¢ãããŒãã¯ã倧èŠæš¡ãªãªãŒãã³ãœãŒã¹ãããžã§ã¯ãã®æšæºãšèããããŠããŸãã ç§ã¯åè«ã§ã¯ãããŸãããé«ãã³ãŒãã«ãã¬ããžã§ã®ãŠããããã¹ãã¯ããã¹ãŠã®æ°ãããããžã§ã¯ãã®åºçºç¹ãšãªãã¯ãã§ãã ããã¯ãç§ã®æ·±å»ãªãªãŒãã³ãœãŒã¹éçºã®ãã¹ãŠã§æ°ã¥ãã§ãããã ããã§ãç§ã¯ãã§ã«åæ段éã§ã¢ãžã¥ãŒã«åŒã®ããã¹ããæžãå§ãã次ã ãšæžãå§ããŸãïŒãã ããç§ã¯æ ãè ãªã®ã§ãé«ãã³ãŒãã«ãã¬ããžãèªãããšã¯ã§ããŸããïŒã
AFLãä»ãããã¡ãžã³ã°ã¯æ¯èŒçæ°ããçŸè±¡ã§ããããã®ã¡ã«ããºã ããã¹ãããããšã«ãããããŸããŸãªãªãŒãã³ãœãŒã¹ãããžã§ã¯ãã®ãã°ãèå¥ããã®ã«ã©ãã»ã©å¹æçããç解ã§ããŸãã Cããã°ã©ãã³ã°èšèªã¯ãå€éšããã®è§£æã«é¢ããŠã¯ãã¹ãŠã®èšé²ãç ŽããŸãã éå»ã«ã¯ãäžé©åãªãã©ãŒãããã®ãã¡ã€ã«ãŸãã¯ç¡çšãªãããã¯ãŒã¯ãã±ãããåå ã§ããœãããŠã§ã¢ã¯ã©ãã·ã¥ãé »ç¹ã«çºçããŠããŸããã ãããã2016幎ã«ã¯ã誰ããã®ãããªãã³ã»ã³ã¹ã容èªããŸããã å ¥åããããŒã¿ã«é¢ä¿ãªããããã°ã©ã ãããããæ£ããåäœãããã©ããããããªãå Žåã¯ãã©ããã§ééããããŠããŸãã
ãããŠãä»ã®èª°ããå質ã倧äºã«ãã¹ãã ãšæãããå Žåãããªãã¯åã³ééã£ãŠããŸãã
ã°ããŒãã«å€æ°ãå¿ãã
ç§ãC / C ++ã§ãªãŒãã³ãœãŒã¹ãããžã§ã¯ãã«åãçµãã§ãããšããããã¯ç§ãçããããšã劚ããã°ããŒãã«å€æ°ã§ãã ã°ããŒãã«å€æ°ã®å®éã®ãããããããäœæããããããããžã§ã¯ãããããã°ããŠãã«ãã¹ã¬ãããã©ã¡ãŒã¿ãèšå®ããããšã¯å°é£ã§ãã ãããã®äœ¿çšãæå°éã«æãã-ãããŠãã³ãŒãã®ãªãã¡ã¯ã¿ãªã³ã°ã¯ã¯ããã«å¹ççã«ãªããŸãã
ã¯ãããããã°/ã¹ããŒã¿ã¹ç»é²ã·ã¹ãã ã«ã€ããŠè©±ããŠããå Žåãã°ããŒãã«å€æ°ã«ã¢ã¯ã»ã¹ããå¿ èŠãããããŸããä»ã®å Žåã«ã¯ããããã®ååšãå¿ããŠãã ããã
OOPãå°ããé¢æ°åããã°ã©ãã³ã°ãšJavaãå°ãè¿œå
圌ãã¯ãã°ãã°ãç§ã¯ä»»æã®èšèªã§FORTRANã§ããã°ã©ã ã§ããŸãããšåè«ãèšããããã°ã©ããŒã¯ãã1ã€ãŸãã¯å¥ã®èšèªãä»ã®ç®çã«äœ¿çšãã䜿ãæ £ããããŒã«ã«å€æããããšããããšãèšåããŸãã ããããããã¯ãã©ã®ãããªããã°ã©ãã³ã°èšèªã«ééããŠããæããªããã°ã©ããŒã¯æããªãŸãŸã§ãããšèšãæ¹æ³ã§ãã å Žåã«ãã£ãŠã¯ãããŸããŸãªããã°ã©ãã³ã°ã·ã¹ãã ã«åºæã®ãŠãããŒãµã«ããããã£ãæ£åžžã«æ©èœããŸãã
ãªããžã§ã¯ãæåããã°ã©ãã³ã°ã§ã¯ãæ§é ã®æŠå¿µããæ å ±ã®åŠçã«å¿ èŠãªããŒã¿ãšã¡ãœãããã©ã®ããã«çµåãããã«é¢å¿ããããŸãã struct Foobaã®å Žåãfoo_xxxxïŒïŒã«èŠçŽãããäžé£ã®é¢æ°ãäœæããŸãã ã³ã³ã¹ãã©ã¯ã¿foo_createïŒïŒããã¹ãã©ã¯ã¿foo_destroyïŒïŒãããã³æ§é ãå®çŸ©ããé¢æ°ã®ãã¹ãã¯èªç±ã«äœ¿çšã§ããŸãã
æãéèŠãªã®ã¯ãããããŒãã¡ã€ã«ã§ã¯ãªããCãã¡ã€ã«ã§struct Foobarãèšè¿°ããããšã§ãã é¢æ°ãå ¬éããŸãããããã§ã¯æ§é ã®æ£ç¢ºãªåœ¢åŒãé衚瀺ã«ããããšãæãŸããã§ãã ååãšããŠãæ§é ãžã®çŽæ¥ãªã³ã¯ãæäŸãããŸããããã¯ãããããŒã®ãšã¯ã¹ããŒãããã€ããªã¢ããªã±ãŒã·ã§ã³ã€ã³ã¿ãŒãã§ã€ã¹ã®äºææ§ã«åœ±é¿ããã©ã€ãã©ãªã«ãšã£ãŠç¹ã«éèŠã§ãïŒæ§é ã®ãµã€ãºãå€æŽãããããïŒã æ§é ããšã¯ã¹ããŒãããå Žåã¯ãããŒãžã§ã³ãŸãã¯ãµã€ãºãæåã®ãã©ã¡ãŒã¿ãŒãšããŠæå®ããŸãã
ããããç¶æ¿ãšããªã¢ãŒãã£ãºã ã«è³ããŸã§ãOOPã®è©³çŽ°ã«ã¯è§ŠããŸããã ããã©ããããã¢ãžã¥ã©ãŒããã°ã©ãã³ã°ã®å©ç¹ã«æ³šç®ããŠãã ãããååãšããŠOOPã«äŒŒãŠããŸãã
ããã«ãé¢æ°åããã°ã©ãã³ã°ã®ããã€ãã®åªããã¢ã€ãã¢ãã€ãŸããå¯äœçšããæããªãåºæã®æ©èœã®ã¢ã€ãã¢ããã£ã³ã»ã«ãã人ã¯ããŸããã ãããã¯ããœãŒã¹ããŒã¿ãšç¹å®ã®åºåæ å ±ãæã€ããã«èšèšããããªãã·ã§ã³ã§ãã ã¢ããã¥ã¢å ¬æŒã¯ãããŸããã äœæããæ©èœã®ã»ãšãã©ã¯ããã®ããã«èŠããã¯ãã§ãã void foobarïŒvoidïŒã®ãããªãã®ãäœæããå Žåã -ãã©ãã«ãäºæããŸãã
ç掻ãèããè€éã«ããããŒã«ã®ãªã¹ãã«ã¯ãã°ããŒãã«å€æ°ãèšèŒãããŠããŸãã ããã©ãŒãã³ã¹ãäœäžãããã·ã¹ãã ã³ãŒã«ã¯åãã«ããŽãªã«åé¡ãããŸãã ã°ããŒãã«å€æ°ã¯ãint foobarïŒstruct Xyz * pïŒãä»ããŠåŒã³åºãæ§é äœã®å¥¥æ·±ãã«é ãããå€æ°ãšæ¬è³ªçã«äŒŒãŠããŸãã ãã®çµæãå¿ èŠãªãã©ã¡ãŒã¿ãŒãèŠã€ããããã«pã®æ·±ããæãäžããå¿ èŠããããŸãã ããããã¹ãŠè¡šé¢ã«ããå Žåã¯ããç°¡åã«ãªãããªã¯ãšã¹ãã¯foobaråïŒp-> lengthãp-> socket-> statusãp-> bbbïŒã§åœ¢æãããŸãã ã¯ãããã®å Žåãé·ããŠåä»ãªãã©ã¡ãŒã¿ã®ãªã¹ããæäœããå¿ èŠããããŸãããè€éãªæ§é ã®ä»£ããã«ãfoobarïŒïŒé¢æ°ã¯åçŽãªåã«äŸåããŸãã
äžéšã«ã¯ãããã°ã©ãã³ã°ãžã®åæ§ã®æ©èœçã¢ãããŒãã¯å®æ°ã®äœ¿çšã«ãããã®ã§ããå®æ°ã¯ãã€ã³ã¿ãŒãconstã§æå®ãããããé¢æ°ã¯ããããå€æŽã§ããŸããã ããããçµæãã©ãã«ãããïŒæ»ãå€ãšéå®æ°ãã€ã³ã¿ãŒïŒãããã³ãœãŒã¹ããŒã¿ãã©ãã«ãããã¯æããã§ãã
Cã¯äœã¬ãã«ã·ã¹ãã ã®ããã°ã©ãã³ã°èšèªã§ãããæããã«è€éãªå Žåãé€ããŠãCèšèªãä¹±çšããªãããã«ããŠãã ããã ç¹å®ã®Cã®ææ³ã®ä»£ããã«ãã³ãŒããCã®æ¡ä»¶ã«äŒŒãŠããŠããã³ãŒããããåºãç°å¢ã§å®è¡å¯èœã«ããããŒã«ã䜿çšããŠãJavaScriptãJavaãCïŒãªã©ãšçµ±åã§ããŸãã
ãã€ã³ã¿ãŒæŒç®ãªãã§åŠçããå¿ èŠããããŸãã ã¯ãã1980幎代ã«ã¯ã³ãŒãã®å€§å¹ ãªé«éåãå¯èœã«ãªããŸãããã1990幎代以éã¯ãç¹ã«çŸä»£ã®æé©åã³ã³ãã€ã©ã®å Žåã«ã¯åœ¹ã«ç«ããªããªããŸããã ãã€ã³ã¿ãŒæŒç®ã¯ãã³ãŒãã®å¯èªæ§ãäœäžãããã ãã§ãã ãªãŒãã³ãœãŒã¹ãããžã§ã¯ãããµã€ããŒè©æ¬ºïŒHearbleedïŒæããã«Hear t bleedãïŒãShellshockãªã©ïŒã«ããããããã³ã«ããã€ã³ã¿ãŒæŒç®ã䜿çšããŠã³ãŒãå ã®çç±ãæ¢ããŠãã ããã ãŸãã¯ãæŽæ°ã€ã³ããã¯ã¹å€æ°ãèŠãŠãJavaã§ãããèšè¿°ããŠãããã®ããã«ã察å¿ããããŒã¿é åã解æããŸãã
ã³ãŒããèšè¿°ããããã®ãã®ãããªçæ³çãªã¢ãããŒãã¯ãæ§é /æŽæ°ã®ãããã¯ãŒã¯ãããã³ã«/ãã¡ã€ã«åœ¢åŒã®è§£æãæåŠããããšãæå³ããŸãã ã¯ãããããã¯ãŒã¯ãã®ã³ã°ã®ããã¥ã¢ã«ã§ã¯ãnothsïŒ*ïŒshort *ïŒpïŒã®ãããªãã®ã䜿çšããããšãæšå¥šããŠããŸããããã®ã¢ããã€ã¹ã¯å·çæç¹ã§ã¯æãæåããŠããŸããã§ããããä»æ¥ã§ã¯å®å šã«äžé©åã§ãã Javaã®ããã«æŽæ°ãã©ã¡ãŒã¿ãŒãåæããŸãïŒp [0] * 256 + p [1]ã è©°ã蟌ãŸããæ§é ãè¡šé¢ã«åºå®ãããšãåæãããããªããšæãããŸã-ãããŸããã§ããã
å®å šã§ãªãæ©èœããããã¯ãã
å»æ¢ãããstrcpyïŒïŒããã³sprintfïŒïŒé¢æ°ã®äœ¿çšãåæ¢ããŸãã è匱æ§ãèŠã€ããå Žåãããããããã«ãããŸãã ããã«ããã®ãããªã»ããã§ã¯ãäžèšã®åé¢æ°ã調ã¹ãŠãããã¡ãŒããã£ã±ãã«ãªã£ãŠããªãããšã確èªããå¿ èŠããããããã³ãŒãã®ç£æ»ã¯ã¯ããã«é«äŸ¡ã§ãã ãããã¡ã¯å€§äžå€«ã ãšç¢ºä¿¡ããŠãããããããŸããããé·ããŠéå±ãªãã®ããªããã©ãã確èªããå¿ èŠããããŸãã ããããstrlcpyïŒïŒ/ strcpy_sïŒïŒããã³snprintfïŒïŒ/ sprintf_sïŒïŒãèšè¿°ããŸãã
äžè¬ã«ããããã¡ãªãŒããŒãããŒãšå¯å€ãµã€ãºãªãŒããŒãããŒã®æå³ãå®éã«èªèããå¿ èŠããããŸãã OpenBSDã§ã®reallocarrayïŒïŒã®äœ¿çšæ¹æ³ããã®ãªãã·ã§ã³ãå€æ°ãµã€ãºã®ãªãŒããŒãããŒã®åé¡ã解決ããçç±ã確èªããmallocïŒïŒã®ä»£ããã«ãã¹ãŠã®ã³ãŒãã§äœ¿çšããŠã¿ãŠãã ããã å¿ èŠã«å¿ããŠãå ã®reallocarrayïŒïŒãOpenBSDããã³ããŒããããã°ã©ã ã®ãã®é¢æ°ã«åºå·ããŸãã
ç¹å®ã®ããŒã¿ãå ¥åãããšãã«ã³ãŒããçªç¶ã¯ã©ãã·ã¥ããçç±ãç¥ã£ãŠããŸããïŒ ãã¶ãããã¯åœŒãã®å®å šã ã ãšããã§ãããã«ãŒãã³ãŒããã¯ã©ãã¯ããã®ã¯ãªãã§ããïŒ ããªãã¯ãã¹ãŠãæ£ããè¡ããŸã-ãã®ãããªåé¡ãããå¿é ããå¿ èŠã¯ãããŸããã
ã2016幎ã®Cã䜿çšããããã°ã©ãã³ã°ãã®èšäºã§ã¯ãã©ãã§ãcallocïŒïŒã䜿çšããããã®ãã³ãããããŸããã æ¥ãã§åŸããªãã§ãã ããããã®å Žåãå€ãã®ãã©ãããã©ãŒã ã§å€æ°ãµã€ãºã®ãªãŒããŒãããŒãçºçããããã§ãã ãŸããreallocïŒïŒãªã©ã®é¢æ°ã«æ £ãããšåæã«ãreallocarrayïŒïŒã䜿çšããŸãã
å®å šãªã³ãŒããæžãããã®å€ãã®ã«ãŒã«ããããŸãããç§ãèšã£ãããšã«åŸãã°ããã®åéã®ã»ãšãã©ã®åé¡ã解決ã§ããŸãã ãããŠãã¯ããããšããããããŒã«ã«ãã¡ã€ã«ãŸãã¯USBããŒãã§ãããšããŠãããœãŒã¹ããŒã¿ã«ã€ããŠã¯æççã§ãã
å¥åŠãªã³ãŒãã§ããŠã³
ãœãããŠã§ã¢éçºãå°éãšãããã¹ãŠã®äŒæ¥ã«æ¬ ããŠããã®ã¯ãä»äºã®åŸã®äŒè°ã§ãã誰ã§ãåå ã§ããçµ±äžãããã¹ã¿ã€ã«ã®çæã³ãŒãã®ææ¡ãçºå£°ã§ããŸãã ãã®åŸãèªç€ºãããã¹ãŠã®äººã解éããŸãã 銬鹿ããŠãããã
æ£ãããã¹ã¿ã€ã«ããšåŒã¶ããšãã§ããå¯äžã®ããšã¯ãã³ãŒããã€ã³ã¿ãŒãããã®ååãšé¡äŒŒããŠããããšã§ãã ããã¯ãå人ã³ãŒããšéåžžäœæ¥ãããªãŒãã³ãœãŒã¹ãããžã§ã¯ãã®äž¡æ¹ã«é©çšãããŸãã LinuxãBSDãWebKitãGnuãªã©ãæäŸããæ¢åã®æåãªã¹ã¿ã€ã«ã®ãããããéžæããã ãã§ãã
ä»ã®ããã°ã©ãã³ã°èšèªãç¹ã«Pythonã®å©ç¹ã®ãã¡ãéåžžã«å°æ°ã®äžè¬çã«åãå ¥ããããŠããã¹ã¿ã€ã«ã«æ³šç®ããããšãã§ããŸãããããã¯Cã«ã€ããŠã¯èšããŸããããŸãã§å¥åŠã«èŠããŸãã LibreSSLã¯BSD圢åŒã«å€æããŸããã éåžžã«è¯ã解決çïŒCã®ã¹ã¿ã€ã«ãããŸãã«ãè¯ãããããŠå€ããªã£ãããå€åãããäžè¬çã§éŠŽæã¿ã®ãããã®ã«å€ããæãæ¥ãã®ãããããŸããã
ããªãã®ã³ãŒãã§ã©ãã ãã¯ãŒã«ã«èŠããããããããšããã«ã誰ããããªããšåãã¯ãŒã«ãªãã®ã䜿çšãããšç¢ºä¿¡ããŠããŸããïŒ ããããããããåãé€ããŸãã圌ãã¯äººã ãæ©ãŸãã ãã§ãã ãŸãã¯ããã®ãããªããŒã«ãéèŠãªå ŽåïŒãããèµ·ããããšããããŸãïŒãçµéšãææžåããŸãã
ãã«ãã³ã¢ããã»ããµã¯æªæ¥ã§ã
ããã»ããµãé«éã«ãªãå¯èœæ§ã¯äœãã§ãã ã芧ã®ãšããããŸããŸãå€ãã®ã³ã¢ã衚瀺ãããŠããŸãã ããããããã¯ãã«ãã¹ã¬ããã³ãŒããèšè¿°ããå¿ èŠããªããšããæå³ã§ã¯ãããŸãããããããããããã®å°æ¥ã«ã€ããŠèããå¿ èŠããããŸãã
ãã¥ãŒããã¯ã¹ãšã¯ãªãã£ã«ã«ã»ã¯ã·ã§ã³ã«ã¯ããŒãšèšããŸã-ã³ãŒããè€éã«ããã ãã§ãã ã¯ããããã«ãã補åã®å®å šæ§ã¯åäžããŸãããããã©ãŒãã³ã¹ãäœäžããŸãã ãããã£ãŠãã³ãŒãã¯2ã³ã¢ãŸãã¯3ã³ã¢ã§é£ã¶ããšãã§ããŸããããã以äžãããšãããã°ã©ã ã®é床ãäœäžãå§ããŸãã Cã§ã®ããã°ã©ãã³ã°ã®ãã¬ãŒã ã¯ãŒã¯ã§ç°ãªãæ°ã®ã³ã¢ã®ã¹ã±ãŒã©ããªãã£ãããéèŠãªã®ã¯ã補åã®ã»ãã¥ãªãã£ãé©åãªã¬ãã«ã«ç¢ºä¿ããããšã ãã§ãã
ããã«ãããŸããŸãªã·ã¹ãã ã®æ¡åŒµæ§ã«ç¹åãã巚倧ãªèšäºã«ç²Ÿéããããšã«ãªããšæããŸããããã£ããèšã£ãããã«ãçµã¿èŸŒã¿ã®æ§é ã®ããŒã¿äº€æã®ã°ããŒãã«å€æ°ãšé ãããé¢æ°ãåãé€ããŸãã 次ã«ãã³ãŒããšãã®ã¹ã±ãŒã«ããªãã¡ã¯ã¿ãªã³ã°ããå¿ èŠãããå Žåãäœæ¥ãã¯ããã«ç°¡åã«ãªããŸãã
æå/倱æã®true / falseãå¿ãã
èšäºã2016幎ã®Cã«ããããã°ã©ãã³ã°ãã¯ãæåã¯åžžã«çå®ã§ãããšè¿°ã¹ãŠããŸãã ãã³ã»ã³ã¹ã çå®ã¯çå®ã§ãããæåã¯æåã§ãã ãããã®éã«çå·ãå ¥ããªãã§ãã ããã çµæãæåããå Žåã«0ãååŸããé¢æ°ã倱æããå Žåã«å¥ã®å€ãååŸããã«ã¯ãè€éãªã³ãŒããèšè¿°ããå¿ èŠããããŸãã
ã¯ããããã¯éåžžã«ãã³ã»ã³ã¹ã§ããæšæºã¯ãããŸããã ã2016幎ã®Cã§ã®ããã°ã©ãã³ã°ãããã®æªãã¢ããã€ã¹ã«è³ãåŸãã代ããã«ããã®ããã¥ã¡ã³ãããå¥åŠãªã³ãŒãã®ããŠã³ããšããçŽ æŽãããä»äºãã©ã®ããã«è¡ã£ãŠããããèŠãŠãã ããã èè ã¯ãåãããšãä»ã®äººã«æããŠããããèªåã®ã³ãŒããå°ç¡ãã«ããã«è¯ãäŸãèšå®ãããªããåé¡ã®çè·¡ã¯ãªããšä¿¡ããŠããŸãã çŽ æŽã§ãã ããã°ã©ãã¯ãã®æšæºã決ããŠåãå ¥ããŸããã ããªãã®ã³ãŒãã¯ãããŸããã«æºã¡ãäžçã§çãæ®ãå¿ èŠããããŸããæåãšå察ã®å€ã«ãããããããtrueãš0ã¯æåãæå³ããŸãã æšæºã®äœæã¯ãSUCCESSããã³FAILUREã€ã³ãžã±ãŒã¿ã®æ確ãªæ±ºå®ã«ãã£ãŠã®ã¿å¯èœã§ãã
ã³ãŒãã次ã®ããã«ãªã£ãŠããå ŽåïŒ
if (foobar(x,y)) { ...; } else { ...; } , , success, failure. . : if (foobar(x,y) == Success) { ...; } else { ...; }
æŽæ°ã«ã€ããŠå°ã
ãProgramming with C in 2016ããšããèšäºã®èè ã¯ãå€å žçãªintãŸãã¯unsignedã䜿çšããçç±ã¯ãªããšäž»åŒµããŠããã代ããã«int32_tããã³uint32_tãåç §ããæ¹ãè¯ããšäž»åŒµããŠããŸãã ãã³ã»ã³ã¹ïŒ intããã³longã³ãã³ãã¯ãã»ãšãã©ã®ã©ã€ãã©ãªé¢æ°ã§åæããŒã¿ãå ¥åããããã«äžè¬çã«åãå ¥ããããåãçš®é¡ã®ç°ãªãã¿ã€ããèŠæ±ããå Žåã§ããäžçš®ã®ã¿ã€ãä¿è·ãæäŸãããŠãŒã¶ãŒã«éç¥ããŸãã
æ£çŽãªãšããã64ãããããã³32ãããã·ã¹ãã ãå«ããæŽæ°ã®èª€ã£ãå€ãèšå®ããããšã¯é£ãããããŸããã ã¯ããintã䜿çšããŠãã€ã³ã¿ãŒãå¶åŸ¡ãããšã64ãããã³ãŒããç ŽæããŸãïŒä»£ããã«intptr_tãptrdiff_tãŸãã¯size_tãèšè¿°ããŸãïŒããå®éã«ãããã©ã®ããã«çºçãããã¯ä¿¡ããããŸããã æåã®4ã®ã¬ãã€ãã«mmapïŒïŒãèšå®ããã ãã§ãããŒãæã«ãããã®ããŒãžãç¡å¹ã§ããããšã«æ³šæããŠããŠããã/ååž°ãã¹ããå®è¡ãããšãåé¡ããã°ãã解決ã§ããŸãã ãããŠãããããã£ãšè¯ãããæ¹æ³ã説æããå¿ èŠã¯ãããŸããã
ã³ãŒãã§æãåä»ãªã®ã¯ãããã°ã©ããŒãæŽæ°åã®åå®çŸ©ãæ¥ãã§ãããšããããšã§ãã ãããçžããŸãã u32ã¯ã³ãŒãã«ç¹å¥ãªé åãäžããããšãç解ããŠããŸããããã®èŠçŽ ã¯ç§ãé©ãããŸãã ããããã³ãŒããèªãå¿ èŠããããŸãã uint32_tãunsigned intãªã©ã®æšæºçãªãã®ã«çœ®ãæããŠãã ããã ãããŠãæãããããšã«ãfilesizeã®ãããªæŽæ°åãä»»æã«äœæããã ãã§ååã§ãã éžæããå šäœã«æ°ããæå³ãäžããããšæã£ãŠããŸãããCããã°ã©ãã³ã°ããäœãã«ãªãããã«èšèšãããŠããããšãå¿ããªãã§ãã ããããããã£ãŠãããã°ã©ããŒã¯ãã®ãããªçç ããã§ãã¯ããéçšã§æ»ã«ãŸãã
éçããã³åçåæã䜿çšãã
以åã®Cä»æ§ã¯ãèŠåã¬ãã«ããšããªã³ãããªãã·ã§ã³ã«èŠçŽãããŠããŸããããä»æ¥ã§ã¯ãéçåæãã代ããã«ãªããŸããã ã³ã³ãã€ã©ã¯Clangã䜿çšããŠããŠãŒã¶ãŒã«æ°ããã¡ãã»ãŒãžããŸããŸãåãã§ããŸããgccã¯ããã«è¿œãã€ããŠããŸãã ãŸããå€ãã¯ææ°ã§ã¯ãããŸããããMicrosoftã³ã³ãã€ã©ãŒã¯Clangã¬ãã«ã§éçåæãæäŸããŸãã ã¡ã«ããºã ã¯Clangèªäœãšåãã§ãããClangåæã®Xcodeã®èŠèŠåæ©èœã¯æ¬åœã«å°è±¡çã§ãã
ãã ããããã¯äžè¬çãªéçåæã«ãããŸããã ããããéçåæãããé«ãã¬ãã«ã«åŒãäžããã»ãã¥ãªãã£ããŒã«ã¯ä»ã«ããããããããŸã-CoverityãVeracodeãHP Fortifyã
ããã«ã¯ã誀ã£ãå¯å®¹ãã®ååããªãããã§ã¯ãããŸããããããã¯ééã£ãå®çŸ©ã§ãã ãã®ãããªã³ãã³ããã³ãŒãã«èšè¿°ããããšã§ããã¯ãªãŒã³ã¢ãããã§ããŸããã€ãŸããããä¿¡é Œæ§ã®é«ãçµæãåŸãããŸãã èšãæããã°ããããã¢ããã¹ããŒã ã䜿çšãããšãã³ãŒããå®å šãªãã®ã«ããäžèŠãªèŠçŽ ãåé€ã§ããŸãã éçã¢ãã©ã€ã¶ãŒã®å³æ Œãªç£ç£ã®äžã§ã³ãŒããèšè¿°ãããšãããã°ã©ãã³ã°ã¹ãã«ãåäžããŸãã
ãããã®æãããäžæ¯
æ°å¹Žã«ãããäŒæ¥ã®ååšã®åŸããããžã§ã¯ãã¯çŸåšã®ã·ã¹ãã ã«ã®ã¿è¡šç€ºãããŸãã ãããŠãããã¯èšå€§ãªæ°ã®ã·ã¹ãã åãããŠããªãäŸåé¢ä¿ãèç©ããããã§ãã ç§ãåããŠããäŒç€Ÿã®1ã€ã§ã圌ãã¯ææã競åä»ç€Ÿãšå ±æããªããã°ãªããªããšåè«ãèšããŸããã
ãããŠãéåžžã«æ£åœãªçç±ã«ããããã®æ £è¡ã¯æ°žä¹ ã«ä¿®æ£ãããŸãã å¥ã®äŒç€Ÿã¯ãããŒã ãç°ãªãã³ã³ãã€ã©ã䜿çšãããšãã«çºçããçµ±åã®åé¡ãåé¿ããããã«ãã³ã³ãã€ã©ã®ããŒãžã§ã³ãçµ±äžããããšãææ¡ããŸããã ãããããã®ã¢ãããŒãã¯æ¯èŒçå°ããªåé¡ã解決ããããã¯ã¯ããã«æ·±å»ãªåé¡ã«çœ®ãæããããŠããŸãã çµ±åã«é¢ããåé¡ãæé€ããäžçš®ã®ã³ãŒãåç·šæãå®è¡ããŸãã
ãŸãããªãŒãã³ãœãŒã¹ã«ã¯ç¹å®ã®å°é£ã䌎ããŸãã äŸåé¢ä¿ãå®å šã«ææžåãããããšã¯ãã£ãã«ãããŸãããããããã«ååãªæ¬ é¥ããããŸãã ãã®çµæãã»ãšãã©ã®å Žåãå¿ èŠãªã³ãŒããå¥è·¡çã«ã³ã³ãã€ã«ããããã«ãåãäŸåé¢ä¿ã®2ã€ã®äºææ§ã®ãªãããŒãžã§ã³ãã€ã³ã¹ããŒã«ããã®ã«å€ãã®æéãè²»ãããŸãã
äŸåé¢ä¿ãå°ãªãã»ã©ãã³ãŒãã®äººæ°ãé«ãŸããŸãã ãããè¡ãã«ã¯ã次ã®ãã®ã®ã¿ãå¿ èŠã§ãã
- äŸåé¢ä¿ãåé€ããŸãã ååãšããŠãããã°ã©ããŒã«ãšã£ãŠæçãªäŸåé¢ä¿ã¯1ïŒ ã®ã¿ã§ãæ®ãã®99ïŒ ã¯äœæ¥ãå°é£ã«ããŸãã
- å¿ èŠãªãœãŒã¹ãã¡ã€ã«ã®ã¿ã䜿çšããŠãã ããã ãã¡ãããä»ã®OpenSSLé¢æ°ãå¿ èŠã§ãªãéããOpenSSLã©ã€ãã©ãªå šäœïŒããã³ãã®äŸåé¢ä¿ïŒã®ãã¹ãŠã®ããã»ã¹ãçµã³ä»ãã代ããã«ãsha2.cãã¡ã€ã«ãè¿œå ããã ãã§ãã
- ãã¹ãŠã®äŸåé¢ä¿ã®ãœãŒã¹ãããªãŒå ã«ããããã«ããŸãã ããšãã°ãLuaã¯æŽæ°ãå¿ èŠãªåªãã25klocã¹ã¯ãªããèšèªã§ãã 察å¿ããlua-deväŸåé¢ä¿ãè¿œæ±ããŠãŠãŒã¶ãŒãèªåã®ããã€ã¹ã«ä»»ãã代ããã«ãããªãŒã§LuaãœãŒã¹ãæå®ããŸãã
- ãããžã§ã¯ãã®ãœãŒã¹ã®äžéšã§ããã€ã³ã¿ãŒãã§ã€ã¹.hã®ãã¡ã€ã«ãå¿ããã«ãdlopenïŒïŒã䜿çšããŠããã°ã©ã ã®èµ·åäžã«ã©ã€ãã©ãªãããŒãããŸãã ããã¯ãã©ã€ãã©ãªã®æ©èœã«å¿ èŠãªãéãã1ã€ãŸãã¯å¥ã®äŸåé¢ä¿ã«åé¡ããªãããšãæå³ããŸãã ãŸãã¯ããããªãã§ã¯ã§ããªãå Žåã¯ãäŸåé¢ä¿ã®åé¡ãä¿®æ£ããããã®æ瀺ãšãšãã«ãšã©ãŒéç¥ãæºåã§ããŸãã
Cã®æªå®çŸ©ã®åäœã«å¯ŸåŠãã
ã»ãšãã©ã®å ŽåãCèšèªãã©ã®ããã«æ©èœããããããç解ããŠããªããããåŒïŒx + 1 <xïŒãæ€èšããŠãã ããã ãã®å Žåã®å®éã®çµæã¯5ã§ãããŸãããã®å€æ°ãæ倧æŽæ°å€ãååŸãã1ãè¿œå ãããšãCãxã®ã¢ã¯ã·ã§ã³ãæå®ããªãããããªãŒããŒãããŒãçºçããŸãã å€ãã®ã³ã³ãã€ã©ã¯ãä»ã®ããã°ã©ãã³ã°èšèªïŒJavaãªã©ïŒãšåæ§ã«ããã®ãããªçŸè±¡ããã€ããªãªãŒããŒãããŒãšããŠèå¥ããŸãã ãããããåãã®ãšãããäžéšã®ã³ã³ãã€ã©ãŒã¯ãã®çš®ã®ç¶æ³ãèªåçã«äžå¯èœãšåé¡ããé¢é£ãããã¹ãŠã®ã³ãŒããåçŽã«åé€ããŸãã
ãããã£ãŠãCã®ã³ã³ãã€ã©ã®çŸåšã®ããŒãžã§ã³ã®åäœããã§ã¯ãªããCã®ä»ã®ã³ã³ãã€ã©ãã³ãŒãã«ã©ã®ããã«åå¿ããããåæããããšã«ãããè¿œå ã®åŽé¢ãæãäžããå¿ èŠããããŸãã
ãããã«
責任ãè² ãããšã«æ £ããŠããªãéããCã§ããã°ã©ãã³ã°ãè¡ããªãã§ãã ããã ãããã¡ãªãŒããŒãããŒãå¯å€ãµã€ãºãªãŒããŒãããŒãã¹ã¬ããåæãæªå®çŸ©ã®åäœãªã©ã®æŠå¿µã培åºçã«åŠçããããšãéèŠã§ãã責任ã«ã¯ããã°ã®æ©ææ€åºçšã«èšèšãããé«å質ã³ãŒãã®äœæãå«ãŸããŸãã Cããã°ã©ãã³ã°ã2016幎ã«éçºããã®ã¯ããããã®æ¡ä»¶ã§ãã