Bug 77393 - [7 Regression] Revision r237735 changed the behavior of F0.0
Summary: [7 Regression] Revision r237735 changed the behavior of F0.0
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libfortran (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: 7.0
Assignee: Jerry DeLisle
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-08-26 19:17 UTC by Dominique d'Humieres
Modified: 2016-09-16 05:30 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2016-08-26 00:00:00


Attachments
Patch for testing, fixes comment #2? (361 bytes, patch)
2016-09-06 20:35 UTC, Jerry DeLisle
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dominique d'Humieres 2016-08-26 19:17:11 UTC
This PR is motivated by the thread at https://groups.google.com/forum/#!topic/comp.lang.fortran/lH-Pi0MbZZ4

Before revision r237735 the output of the following test

print "(f0.0)", huge(1.0)
print "(f0.0)", huge(1.0_8)
print "(f0.0)", huge(1.0_10)
print "(f0.0)", huge(1.0_16)
end

was

340282346638528859811704183484516925440.
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.
1189731495357231765021263853030970205169063322294624200440323733891737005522970722616410290336528882853545697807495577314427443153670288434198125573853743678673593200706973263201915918282961524365529510646791086614311790632169778838896134786560600399148753433211454911160088679845154866512852340149773037600009125479393966223151383622417838542743917838138717805889487540575168226347659235576974805113725649020884855222494791399377585026011773549180099796226026859508558883608159846900235645132346594476384939859276456284579661772930407806609229102715046085388087959327781622986827547830768080040150694942303411728957777100335714010559775242124057347007386251660110828379119623008469277200965153500208474470792443848545912886723000619085126472111951361467527633519562927597957250278002980795904193139603021470997035276467445530922022679656280991498232083329641241038509239184734786121921697210543484287048353408113042573002216421348917347174234800714880751002064390517234247656004721768096486107994943415703476320643558624207443504424380566136017608837478165389027809576975977286860071487028287955567141404632615832623602762896316173978484254486860609948270867968048078702511858930838546584223040908805996294594586201903766048446790926002225410530775901065760671347200125846406957030257138960983757998926954553052368560758683179223113639519468850880771872104705203957587480013143131444254943919940175753169339392366881856189129931729104252921236835159922322050998001677102784035360140829296398115122877768135706045789343535451696539561254048846447169786893211671087229088082778350518228857646062218739702851655083720992349483334435228984751232753726636066213902281264706234075352071724058665079518217303463782631353393706774901950197841690441824738063162828586857741432581165364040218402724913393320949219498422442730427019873044536620350262386957804682003601447291997123095530057206141866974852846856186514832715974481203121946751686379343096189615107330065552421485195201762858595091051839472502863871632494167613804996319791441870254302706758495192008837915169401581740046711477877201459644461175204059453504764721807975761111720846273639279600339670470037613374509553184150073796412605047923251661354841291884211340823015473304754067072818763503617332908005951896325207071673904547777129682265206225651439919376804400292380903112437912614776255964694221981375146967079446870358004392507659451618379811859392049544036114915310782251072691486979809240946772142727012404377187409216756613634938900451232351668146089322400697993176017805338191849981933008410985993938760292601390911414526003720284872132411955424282101831204216104467404621635336900583664606591156298764745525068145003932941404131495400677602951005962253022823003631473824681059648442441324864573137437595096416168048024129351876204668135636877532814675538798871771836512893947195335061885003267607354388673368002074387849657014576090349857571243045102038730494854256702479339322809110526041538528994849203991091946129912491633289917998094380337879522093131466946149705939664152375949285890960489916121944989986384837022486672249148924678410206183364627416969576307632480235587975245253737035433882960862753427740016333434055083537048507374544819754722228975281083020898682633020285259923084168054539687911418297629988964576482765287504562854924265165217750799516259669229114977788962356670956627138482018191348321687995863652637620978285070099337294396784639879024914514222742527006363942327998483976739987154418554201562244154926653014515504685489258620276085761837129763358761215382565129633538141663949516556000264159186554850057052611431952919918807954522394649627635630178580896692226406235382898535867595990647008385687123810329591926494846250768992258419305480763620215089022149220528069842018350840586938493815498909445461977893029113576516775406232278298314033473276603952231603422824717528181818844304880921321933550869873395861276073670866652375555675803171490108477320096424318780070008797346032906278943553743564448851907191616455141155761939399690767415156402826543664026760095087523945507341556135867933066031744720924446513532366647649735400851967040771103640538150073486891798364049570606189535005089840913826869535090066783324472578712196604415284924840041850932811908963634175739897166596000759487800619164094854338758520657116541072260996288150123144377944008749301944744330784388995701842710004808305012177123560622895076269042856800047718893158089358515593863176652948089031267747029662545110861548958395087796755464137944895960527975209874813839762578592105756284401759349324162148339565350189196811389091843795734703269406342890087805846940352453479398080674273236297887100867175802531561302356064878709259865288416350972529537091114317204887747405539054009425375424119317944175137064689643861517718849867010341532542385911089624710885385808688837777258648564145934262121086647588489260031762345960769508849149662444156604419552086811989770240.
1189731495357231765085759326628007016196469052641694045529698884212163579755312392324974012848462073525902033564749126859755265433573804462672698751945261490853461958725021262845865799405404493574681566096686172574953791792292256220777095858112702436475442537092608935138247345677279593806773692330094615746119725784172889892521939920757654204864565673356452247278152288867700638935595456496699511441752909606878513250948311396886100526833092128683974752192266386791880873694343077348155564101669971138512786874753496996549221727686770196551512812712488289469952298031867469924683981576664562667786719061499639630341657098305425237220876664630087808767256182803220212219924852375903049520911395910918921205273496768588119030111593018789368039232011671404175845108854706965215605777113516257404818817695075025715299705916714352103671782759119316034498392169720631800164034124698918142227577300459309880454715179606299895507583075851195185857971173167676966057998899352631885417716295302014668802384075846036226606480142977595407135050379808649130157164024060311786908796372510335873512774795275748595417572920936651398752709055215663939505589207804914540432978557623565645991208599669097180808881920063722771431218489011922209679053545963628417326002439732802939524313786668514027381434321036636571171670423586472759561231970793967839279147282720195377060602122638457883204809341717526809639253539447730280863675704796054050525162959099932535265586464682793821550087166946662209865086040990507131145474267411042839542322762994938759613112743837192839682676257555388372814490845395747128162065871588219108887240116651361962050800029176299938826082417547516732269930473133261258921845516815235455354310458114528303607394526100730578774092094736822286015459361126642549541799645333882549670764145955017051330800061253865140180153211929361456500343514792890205532021760061882232615736553377294980974059590520187961459799386741513028505934410453603480192383349321115171811051004108592830991811382552909064873029533418691087118107895004426881765865961841419267486232005929789956207494587649901662172318722999484512325826087031561936383689740686505279775296789331613683822798597040651600524129025149894873153196942095056670847466927644812596506700129443579512479230621373978088731257089799622902183824105412930483065603459863120371744282301377070153823878609951218937542956964157950988060608985782910656238116142203574104757451828170804875257446204128348513829082731722364189380493588338947664370623279820755831646205417488393062838201789547219543194450902113699925965376908192792152122212824578879336506875288617303469517112245451315447164280392523574962804175375927948971096983905242318797695347043690474223813266505639761164438844266531364626851219633994434154098562127395936184421821444273431534507860161614287022720984061569660333372788241037131538077377480152670583257920535569973318188112685673318997967497786786001251403873023920127717626858627038170562807276699687356274072773403132694104831615879354395811585825112837841563222761623334459188131537882355732483030085976890382969734476214593428191212717141333047577867552218517431064848760373196290310124466145087078377140528533048684204278799596652514009368964527494988719996088230065668196236298805733689960371306226158464997243490564472254071897564144128539839986096045563264771285585066304177995720101744844387158329767375560416207800878830072072413908657855667239546369357775781344288195989176313356856417845434232814886744226746707066979755577121788798468777700116472954103621810567107869855646414713502627836321256957407217461738363552424248762436478085351810995749293238174081331905048144612700905541425702220302537611494824228765324577933778519818778697340282580912780674979058938062556856001076057705982166686824756037569615760497619819482052758118532729333127733603742149847001463931981340719681330844408263017545241644293372483217234561694263937855759294448662979095419227451801588425977869694026601427919655168415895923043115191751872713346095752634608254475988154162254952597853199039645883742199236387610395830948074365988397707849632252080920941206268114832425403540515474312327876180802357701527842702008781378306569508588571830140611098042683009530862797403015355464377406249853964481000402231771665700893607521804084523668568649103258862666293372472441435563520595461701042390500795615834505944837326652542467444364861499184275097485253621979537504128523848241127715641240965261646703516395599407360083455079665191393229410544185167999099787655424462558900874388405649169453726739312260234815543297842308646072190147948072928456725835039546121182133640777769925841807579051735838823112759622714067509669913645288281894558925612972425252452248453502562347348900936766966136332741088135837550717443838484760651019872222926016920811114616937143207743488504602012776364256746872315205952601072228970686460932435222754496341763535189105548847634608972381760403137363968.

Starting at that revision the output became

340282346638528859811704183484516925440.
a.out(42020,0x7fff78eb2000) malloc: *** error for object 0x7fff5f902ed0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

Program received signal SIGABRT: Process abort signal.

The only relevant part of the standard I have found is

> 10.7.2.3.2 F editing
> 1 The Fw.d edit descriptor indicates that the field occupies w positions,
> except when w is zero in which case the processor selects the field width.
> The fractional part of the field consists of d digits.
> On input, w shall not be zero.

I am not 100% sure about the interpretation of "the processor selects the field width", but I understand it as "the minimal width to output the number without *". If I am correct, the expected behavior is the one before r237735.

Note that the following code

print "(f8.0)", huge(1.0)
print "(f18.0)", huge(1.0_8)
print "(f20.0)", huge(1.0_10)
print "(f40.0)", huge(1.0_16)
end

gives

********
******************
********************
****************************************

before r237735 and

********
******************

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

after it.
Comment 1 Jerry DeLisle 2016-08-31 17:45:58 UTC
Author: jvdelisle
Date: Wed Aug 31 17:45:26 2016
New Revision: 239900

URL: https://gcc.gnu.org/viewcvs?rev=239900&root=gcc&view=rev
Log:
2016-08-31  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR libgfortran/77393
	* io/write.c (kind_from_size): New function to calculate required buffer
	size based on kind type. (select_buffer, select_string): Use new
	function. (write_float_0, write_real, write_real_g0, write_complex):
	Adjust calls to pass parameters needed by new function.

Modified:
    trunk/libgfortran/ChangeLog
    trunk/libgfortran/io/write.c
Comment 2 Jerry DeLisle 2016-08-31 17:55:04 UTC
Author: jvdelisle
Date: Wed Aug 31 17:54:32 2016
New Revision: 239901

URL: https://gcc.gnu.org/viewcvs?rev=239901&root=gcc&view=rev
Log:
2016-08-31  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR libgfortran/77393
	* gfortran.dg/fmt_f0_2.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/fmt_f0_2.f90
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 3 Dominique d'Humieres 2016-09-01 08:55:19 UTC
At revision r239908 the second test in comment 0 still gives

********
******************

Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

at run time.
Comment 4 Jerry DeLisle 2016-09-01 20:18:29 UTC
(In reply to Dominique d'Humieres from comment #3)
> At revision r239908 the second test in comment 0 still gives
> 
> ********
> ******************
> 
> Program received signal SIGSEGV: Segmentation fault - invalid memory
> reference.
> 
> at run time.

Works for me here!
Comment 5 Jerry DeLisle 2016-09-01 21:18:14 UTC
Author: jvdelisle
Date: Thu Sep  1 21:17:42 2016
New Revision: 239945

URL: https://gcc.gnu.org/viewcvs?rev=239945&root=gcc&view=rev
Log:
2016-09-01  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR libgfortran/77393
	* gfortran.dg/fmt_f0_2.f90: Update tests for available kinds.

Modified:
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/fmt_f0_2.f90
Comment 6 Jerry DeLisle 2016-09-01 21:23:04 UTC
The update to the test case should correct any platform specific KIND issues.

May need to upsize the bufers a little for Dominique's issue.  I set the buffers tight so we could catch different behaviors on platforms I do not have access for testing.
Comment 7 Dominique d'Humieres 2016-09-01 22:00:52 UTC
> Works for me here!

Are you sure that you have tested

print "(f8.0)", huge(1.0)
print "(f18.0)", huge(1.0_8)
print "(f20.0)", huge(1.0_10)
print "(f40.0)", huge(1.0_16)
end

?
Comment 8 Jerry DeLisle 2016-09-02 16:09:25 UTC
(In reply to Dominique d'Humieres from comment #7)
> > Works for me here!
> 
> Are you sure that you have tested
> 
> print "(f8.0)", huge(1.0)
> print "(f18.0)", huge(1.0_8)
> print "(f20.0)", huge(1.0_10)
> print "(f40.0)", huge(1.0_16)
> end
> 
> ?
$ cat pr77393-b.f90 
print "(f8.0)", huge(1.0)
print "(f18.0)", huge(1.0_8)
print "(f20.0)", huge(1.0_10)
print "(f40.0)", huge(1.0_16)
end
$ gfc pr77393-b.f90 
$ ./a.out 
********
******************
********************
****************************************
$ gfc -v
Using built-in specs.
COLLECT_GCC=gfc
COLLECT_LTO_WRAPPER=/home/jerry/dev/usr/libexec/gcc/x86_64-pc-linux-gnu/7.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../trunk/configure --prefix=/home/jerry/dev/usr --enable-languages=c,c++,fortran --disable-libmudflap --enable-libgomp --disable-bootstrap
Thread model: posix
gcc version 7.0.0 20160831 (experimental) (GCC) 

Yes, as posted. I suspect, assuming you are on Darwin, that the implementation of snprintf emits a larger number of bytes. I found on the system here that it emits the number of characters I used in the routine with the adders.

Can you test by changing the define in write.c line 1349:

#define BUF_STACK_SZ 256

to something absurd like 6000.

Then run this and see how large the strings are:

program testbigf0 ! Can enormous numbers be printed with F0.0 format?
  implicit none
  character(10000) :: str
  
  write(str,"(f0.0)") -huge(1.0)
  print *, len(trim(str))
  write(str,"(f0.0)") -huge(1.0_8)
  print *, len(trim(str))
  write(str,"(f0.0)") -huge(1.0_10)
  print *, len(trim(str))
  write(str,"(f0.0)") -huge(1.0_16)
  print *, len(trim(str))
end program testbigf0

Also using gdb can you see where the segfault occurs with the pr77393-b.f90  version above.
Comment 9 Dominique d'Humieres 2016-09-02 22:38:05 UTC
> Can you test by changing the define in write.c line 1349:
>
> #define BUF_STACK_SZ 256

On x86_64-apple-darwin15 the threshold (minimal value without SIGSEGV) is 385 with -m64 and 596 with -m32.
Comment 10 Rainer Orth 2016-09-05 11:32:06 UTC
The test FAILs on Solaris (both sparc and x86, both 32 and 64-bit):

FAIL: gfortran.dg/fmt_f0_2.f90   -O0  execution test
FAIL: gfortran.dg/fmt_f0_2.f90   -O1  execution test
FAIL: gfortran.dg/fmt_f0_2.f90   -O2  execution test
FAIL: gfortran.dg/fmt_f0_2.f90   -O3 -fomit-frame-pointer -funroll-loops -fpeel-loops -ftracer -finline-functions  execution test
FAIL: gfortran.dg/fmt_f0_2.f90   -O3 -g  execution test
FAIL: gfortran.dg/fmt_f0_2.f90   -Os  execution test

Note: The following floating-point exceptions are signalling: IEEE_OVERFLOW_FLAG
ERROR STOP FAILED AT LINE 23

Error termination. Backtrace:

  Rainer
Comment 11 Jerry DeLisle 2016-09-05 15:26:56 UTC
(In reply to Rainer Orth from comment #10)
> The test FAILs on Solaris (both sparc and x86, both 32 and 64-bit):
> 
> FAIL: gfortran.dg/fmt_f0_2.f90   -O0  execution test
> FAIL: gfortran.dg/fmt_f0_2.f90   -O1  execution test
> FAIL: gfortran.dg/fmt_f0_2.f90   -O2  execution test
> FAIL: gfortran.dg/fmt_f0_2.f90   -O3 -fomit-frame-pointer -funroll-loops
> -fpeel-loops -ftracer -finline-functions  execution test
> FAIL: gfortran.dg/fmt_f0_2.f90   -O3 -g  execution test
> FAIL: gfortran.dg/fmt_f0_2.f90   -Os  execution test
> 
> Note: The following floating-point exceptions are signalling:
> IEEE_OVERFLOW_FLAG
> ERROR STOP FAILED AT LINE 23
> 
> Error termination. Backtrace:
> 
>   Rainer

The test case also fails on PowerPC and other architectures because it makes assumptions about the number of digits being generated by the snprintf functions. I will be changing that shortly which will resolve the ERROR STOP.

Can you confirm whether or not you get a segfault with the following from comment 2.

print "(f8.0)", huge(1.0)
print "(f18.0)", huge(1.0_8)
print "(f20.0)", huge(1.0_10)
print "(f40.0)", huge(1.0_16)
end
Comment 12 Jerry DeLisle 2016-09-06 20:35:20 UTC
Created attachment 39577 [details]
Patch for testing, fixes comment #2?

This patch for testing.  I have tested on x86_64-unknown-freebsd12.0 which also exhibited the issue with an invalid memory reference.
Comment 13 Jerry DeLisle 2016-09-06 23:22:59 UTC
Author: jvdelisle
Date: Tue Sep  6 23:22:26 2016
New Revision: 240018

URL: https://gcc.gnu.org/viewcvs?rev=240018&root=gcc&view=rev
Log:
2016-09-06  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR libgfortran/77393
	* io/write_float.def (build_float_string): Recognize when the
	result will not fit in the user provided, star fill, and exit
	early.

	* gfortran.dg/fmt_f0_2.f90: Update test.
	* gfortran.dg/fmt_f0_3.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/fmt_f0_3.f90
Modified:
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/fmt_f0_2.f90
    trunk/libgfortran/ChangeLog
    trunk/libgfortran/io/write_float.def
Comment 14 ro@CeBiTec.Uni-Bielefeld.DE 2016-09-07 12:41:57 UTC
> --- Comment #13 from Jerry DeLisle <jvdelisle at gcc dot gnu.org> ---
> Author: jvdelisle
> Date: Tue Sep  6 23:22:26 2016
> New Revision: 240018
>
> URL: https://gcc.gnu.org/viewcvs?rev=240018&root=gcc&view=rev
> Log:
> 2016-09-06  Jerry DeLisle  <jvdelisle@gcc.gnu.org>
>
>         PR libgfortran/77393
>         * io/write_float.def (build_float_string): Recognize when the
>         result will not fit in the user provided, star fill, and exit
>         early.
>
>         * gfortran.dg/fmt_f0_2.f90: Update test.
>         * gfortran.dg/fmt_f0_3.f90: New test.

I've only now managed to test the patch: it passes on both
i386-pc-solaris2.12 and sparc-sun-solaris2.12.

Thanks.
	Rainer
Comment 15 Jerry DeLisle 2016-09-16 05:30:23 UTC
Closing, Fixed