I have found on Stack Overflow a 10-lines block of code that does exactly what I want.

There is no license information in the post nor on the answerer's profile page, so the CC BY-SA 3.0 license of Stack Overflow applies.

To separate concerns, let's suppose the answerer has written the code by themselves, not copy-pasted from somewhere else.

QUESTION: Can I copy/paste this snippet into my GNU-GPLv3-licensed project?


Stack Overflow contributions (and all of Stack Exchange's too) are licensed under CC-BY-SA 3.0.

That basically means that

You are free to

  • Share — copy and redistribute the material in any medium or format
  • Adapt — remix, transform, and build upon the material for any purpose, even commercially. The licensor cannot revoke these freedoms as long as you follow the license terms.

Under the following terms:

  • Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original.

The problematic bit is the ShareAlike clause. You have to release your work under the same license as the original.

That means that, assuming the work is original enough (depends on the laws in your country), you will not be able to add it as is to your project.

Nothing is stopping you from understanding the snippet and writing one on your own, that has nothing to do with the original one.

  • 1
    There are several CC licenses. I think it's important to state that it's CC-BY-SA. – svick Jun 24 '15 at 12:57
  • 4
    How and why does this information differ from the following thread, which implies that adaptations under 3.0 can be taken under any later version, and taking them under 4.0 gives one way compatibility with GPL v3? opensource.stackexchange.com/a/2236/3570 – user3570 Dec 27 '15 at 17:14

The CC-BY-SA 3.0 is not compatible with the GNU GPL.

There are two options under which you can use code from SO in your GNU GPL licensed code:

  • The SO member has licensed the code under an additional license that is compatible with the GNU GPL. One such license is the Apache License 2.0. (I do this, see my profile).
  • The code is trivial enough to not be covered by copyright.

The answer is that it depends.

According the just the licensing: no, you can't, the licenses aren't compatible. But that's not the entirety of the story.

That only applies if the snippet itself is eligible to be copyrighted in the first place. When that applies differs from country to country. Some take in to account the effort that was needed to create something - the "sweat of the brow" doctrine, as is used for example in the UK, but not in the US.

In other places the threshold is originality and creativity. This is rather vague, and it can only be tested in a court of law if the snippet meets the requirements.

In Google vs Oracle, Google was found to have infringed on Oracles copyright through a 9 line method rangeCheck that was as follows:

private static void rangeCheck(int arrayLen, int fromIndex, int toIndex {
     if (fromIndex > toIndex)
          throw new IllegalArgumentException("fromIndex(" + fromIndex +
               ") > toIndex(" + toIndex+")");
     if (fromIndex < 0) 
          throw new ArrayIndexOutOfBoundsException(fromIndex);
     if (toIndex > arrayLen) 
          throw new ArrayIndexOutOfBoundsException(toIndex);

so apparently a snippet like that is sufficient to satisfy that criterion. I think there are few code snippets that demonstrate less originality and creativity than this snippet does, but the details is eventually for a court to decide.

You do have the option however to ask the original creator to license the snippet to you under your preferred license. Or you could take the presented algorithm, and re-implement it yourself. Ideas, and algorithms are ideas, are not eligible for copyright.

tl;dr. According to the license: no. Unless the work doesn't meet the threshold of originality, which based on precedent seems to be very low for software in the US. It's probably not worth the legal minefield to try it out. But you can re-implement the algorithm yourself.

  • 1
    Did you just illegally copy Oracles IP here? It seems to me that you don't have the rights to distribute that code under the CC-BY-SA 3.0 license, but by posting here, you effectively did just that? – Josef Jul 27 '15 at 7:46
  • 2
    @Josef No, I pasted Oracles IP here under fair use provisions. This post falls squarely within the four-factor balancing test. Particularly, its use is strongly transformative; this post doesn't aim to execute the code, but rather discuss it, which has been historically upheld as a valid fair use defense. – Martijn Jul 27 '15 at 8:42
  • 2
    But you agreed to grant a CC-BY-SA 3.0 license by posting it here. stackexchange.com/legal#3SubscriberContent Subscriber warrants, represents and agrees Subscriber has the right to grant Stack Exchange and the Network the rights set forth above. – Josef Jul 27 '15 at 12:25
  • 2
    @Josef yes. The answer is my own creative work, and licensed to SE (and you, and anyone else) under CC-BY-SA 3.0. The post contains a fragment that is, in this context, fair use. As such, I have the right to grant Stack Exchange and the Network rights to this post as a whole. Oracle retains the copyright to the snippet, which is licensed under the GPL2 + classpath exception. – Martijn Jul 27 '15 at 12:48

Unfortunately most of the answers to this question so far are largely incorrect.

The short answer is yes you can include CC-BY-SA 3.0 content in a GPL project, providing you've adapted the content and the project is GPLv3.

This answer deals generically regarding using code posted on any forum. It answers some of the side issues and other things to think through when taking public code (such as whether it is copyrightable at all).

And this answer and this answer cover the specific CC-BY-SA 3.0 -> GPLv3 path.

  • No, you can't include CC-BY-SA 3.0 content in GPL project. This is because a CC-BY-SA 4.0 adaption of a CC-BY-SA 3.0 is not solely licensed under just the CC-BY-SA 4.0 license but instead by a license stack of both the CC-BY-SA 3.0 and CC-BY-SA 4.0 licenses and the CC-BY-SA 3.0 license is incompatible with the GPLv3 license. The right to comply with just the final contributor license is in CC-BY-SA 4.0, that right does not retroactively apply to 3.0. – Trevor Apr 27 '18 at 4:06

Whether you copy someone else's code into an open source or a proprietary project, you obviously need to check the licenses that you see, as was discussed.

However, just because it is on stackoverflow, and stackoverflow requires a certain license, doesn't mean the code is actually licensed. If an employee takes copyrighted code of his employer and publishes it on stackoverflow without permission, then there is no license!

By far the best method is to take the code, study it, try it out, until you really understand it, and then write your own code. This has three advantages: You learned something. It is legally safe. And the code quality is now up to your own high standards.

  • 1
    Putting aside license issue for a second, I see several disadvantages though: 1) High probability of introducing bugs 2) Time taken to reimplement something that has already been implemented. – Nicolas Raoul Jul 7 '15 at 10:25

No. Code on StackOverflow is licensed under CC-BY-SA (which, incidentally, is a bad choice of license for any software or code). The problem with this is the SA part - the Share-Alike clause.

This means that any direct copies of the work have to be released under the same license as the original. Thus, you cannot use the GPL.

However, you don't have to directly copy the snippet. Instead, use it to understand the principles behind why the author has done that, then write your own snippet that does the same thing. You get the same benefit out of it, and can use your own choice of license.

  • 1
    This is not quite correct. As of the 3.0 SA license you are allowed to release derived works under compatible licenses. The issue is that the GPL isn't one of them. – curiousdannii Jun 30 '15 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.