vsprintf()

3 posts / 0 new
Last post
mritter0
mritter0's picture
Offline
Last seen: 2 years 6 months ago
Joined: 2014-04-21 21:15
vsprintf()

I have been using this Requster on OS3.9 for a long time. Now I am moving to OS4.1FE. It crashes whenever there are args:

Works:

  1. SAK_EasyRequest(Objects[OID_WINDOW],MainWindow,RequesterTitle,0,NULL,"Name:Mark\nCountry:USA",0,SAK_LocaleString(MSG_TERMINATE));

Crashes:

  1. SAK_EasyRequest(Objects[OID_WINDOW],MainWindow,RequesterTitle,0,NULL,"Name:%s\nCountry:%s",0,SAK_LocaleString(MSG_TERMINATE),"Mark","USA");

Is the vsprintf() not GCC/OS4.1 friendly?

  1. VARARGS68K int32
  2. SAK_EasyRequest(Object *WinObject,struct Window *Window,STRPTR Title,int32 ImageType,struct Image *Image,STRPTR Text,uint32 Timer,STRPTR Gadgets,...)
  3. {
  4. Object *Requester;
  5. int32 Result;
  6. char VABuffer[1024];
  7. va_list VarArgs;
  8.  
  9. va_startlinear(VarArgs,Gadgets);
  10. vsprintf(VABuffer,Text,va_getlinearva(VarArgs,void*));
  11. va_end(VarArgs);
  12.  
  13. if (!(Requester=RequesterObject,
  14. REQ_TitleText, Title,
  15. REQ_Type, 0,
  16. REQ_Image, ImageType ? REQIMAGE_ERROR : REQIMAGE_INFO,
  17. REQ_BodyText, VABuffer,
  18. REQ_WrapBorder, 35,
  19. REQ_TimeOutSecs, Timer,
  20. REQ_GadgetText, Gadgets,
  21. REQ_StayOnTop, TRUE,
  22. TAG_END)))
  23. return(-99);
  24. Result=DoMethod(Requester,RM_OPENREQ,NULL,Window,NULL);
  25. DisposeObject(Requester);
  26.  
  27. // Timer? Result==-1
  28.  
  29. return(Result);
  30. }
salass00
salass00's picture
Offline
Last seen: 6 months 2 weeks ago
Joined: 2011-02-03 11:27
You're using linear varargs

You're using linear varargs but vsprintf() expects normal varargs.

Also you should use vsnprintf() instead of vsprintf() to protect against buffer overflows.

  1. int32 SAK_EasyRequest(Object *WinObject, struct Window *Window, STRPTR Title,
  2. int32 ImageType, struct Image *Image, STRPTR Text, uint32 Timer,
  3. STRPTR Gadgets, ...)
  4. {
  5. Object *Requester;
  6. int32 Result;
  7. char VABuffer[1024];
  8. va_list VarArgs;
  9.  
  10. va_start(VarArgs, Gadgets);
  11. vsnprintf(VABuffer, sizeof(VABuffer), Text, VarArgs);
  12. va_end(VarArgs);
  13.  
  14. if (!(Requester=RequesterObject,
  15. REQ_TitleText, Title,
  16. REQ_Type, 0,
  17. REQ_Image, ImageType ? REQIMAGE_ERROR : REQIMAGE_INFO,
  18. REQ_BodyText, VABuffer,
  19. REQ_WrapBorder, 35,
  20. REQ_TimeOutSecs, Timer,
  21. REQ_GadgetText, Gadgets,
  22. REQ_StayOnTop, TRUE,
  23. TAG_END)))
  24. return(-99);
  25. Result=DoMethod(Requester,RM_OPENREQ,NULL,Window,NULL);
  26. DisposeObject(Requester);
  27.  
  28. // Timer? Result==-1
  29.  
  30. return Result;
  31. }
mritter0
mritter0's picture
Offline
Last seen: 2 years 6 months ago
Joined: 2014-04-21 21:15
That is the way I originally

That is the way I originally had it; it worked that way. I was looking around here and everyone was using the linear code so I switched to that thinking it was "the new OS4 way to do it."

What is the difference? When do you use that?

Thanks

Log in or register to post comments